-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathindex.js
65 lines (51 loc) · 1.61 KB
/
index.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
const { Client, Intents } = require("discord.js");
const {
token,
serverId,
channelImageId,
maxImages,
timeChange,
} = require("./config.json");
const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
// At the start of the bot
client.once("ready", () => {
console.log("Ready!");
// Change image at start
executeChange();
// Change image every x minutes (from config)
setInterval(function () {
executeChange();
}, timeChange);
});
// Login to Discord
client.login(token);
// Command interface
client.on("interactionCreate", async (interaction) => {
if (!interaction.isCommand()) return;
const { commandName } = interaction;
// Refresh command - Change the image
if (commandName === "refresh") {
await executeChange();
await interaction.reply("Image changé!");
}
});
function getRandom(min, max) {
return Math.floor(Math.random() * (max - min) + min);
}
function getMessagesOfChannel(channelId, amount = 100) {
let channel = client.channels.cache.get(channelId);
return channel.messages.fetch({ limit: amount });
}
async function changeIcon(url) {
let guild = client.guilds.cache.get(serverId);
await guild.setIcon(url, ["Bot change icon"]);
}
async function executeChange() {
const messages = await getMessagesOfChannel(channelImageId, maxImages);
const images = messages.filter((message) => message.attachments.size > 0);
const imageUrls = images.map((image) => image.attachments.first().url);
console.log(`Found ${imageUrls.length} image urls`);
const imageUrl = imageUrls[getRandom(0, imageUrls.length)];
console.log(imageUrl);
await changeIcon(imageUrl);
}