bot/bot.js

132 lines
3.6 KiB
JavaScript
Raw Normal View History

2022-02-18 12:25:28 +01:00
const { Client, Intents } = require('discord.js');
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
const https = require('https');
2022-02-16 12:28:10 +01:00
2022-02-17 16:32:45 +01:00
const D_TOKEN = process.env.DISCORD_TOKEN;
const O_TOKEN = process.env.AUTH_TOKEN;
2022-02-17 16:32:45 +01:00
async function get_guildID()
{
let Guilds = CLIENT.guilds;
let v = (await Guilds.fetch()).keyAt(0);
return v;
}
async function remove_notlogged(){
let guild = CLIENT.guilds.resolve(await get_guildID());
let token = O_TOKEN;
2022-05-01 14:25:42 +02:00
let url = `https://www.42ctf.org/api/bot/discord?token=${token}`;
await (guild.members.fetch()).then(() => {
console.log("Cache reloaded !");
});
const rolesCollection = new Map([
['top1', guild.roles.cache.get("798638767359524875")],
['top10', guild.roles.cache.get("801787467064672286")],
['top50', guild.roles.cache.get("803729539145924649")]
]);
https.get(url,(res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
var json = JSON.parse(body);
let v = new Map(Object.entries(json))
let members = guild.members.cache;
members.forEach(async (key, value) => {
var r = false;
v.forEach((jkey, jvalue) => {
if (jvalue == key)
r = true;
})
if (r == false)
{
let user = await guild.members.fetch(key);
await user.roles.remove(rolesCollection.get('top1'));
await user.roles.remove(rolesCollection.get('top10'));
await user.roles.remove(rolesCollection.get('top50'));
}
})
}
catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.log(error.message);
});
}
2022-02-18 12:25:28 +01:00
async function rank(token = O_TOKEN){
2022-05-01 14:25:42 +02:00
let url = `https://www.42ctf.org/api/bot/discord?token=${token}`;
2022-02-18 12:25:28 +01:00
let guild = CLIENT.guilds.resolve(await get_guildID());
const rolesCollection = new Map([
['top1', guild.roles.cache.get("798638767359524875")],
['top10', guild.roles.cache.get("801787467064672286")],
['top50', guild.roles.cache.get("803729539145924649")]
2022-02-18 12:25:28 +01:00
]);
2022-02-17 15:04:39 +01:00
https.get(url,(res) => {
2022-02-18 12:25:28 +01:00
var body = "";
2022-02-17 15:04:39 +01:00
res.on("data", (chunk) => {
body += chunk;
});
2022-02-17 15:04:39 +01:00
res.on("end", () => {
try {
2022-02-18 12:25:28 +01:00
var json = JSON.parse(body);
2022-02-17 15:04:39 +01:00
let v = new Map(Object.entries(json))
v.forEach(async (key, value) => {
2022-02-17 16:32:45 +01:00
let user = guild.members.fetch(value);
2022-02-17 15:04:39 +01:00
let rank = parseInt(key);
if(rank == 1)
{
2022-02-18 12:25:28 +01:00
await (await user).roles.add(rolesCollection.get('top1'));
await (await user).roles.remove(rolesCollection.get('top10'));
await (await user).roles.remove(rolesCollection.get('top50'));
2022-02-17 15:04:39 +01:00
}
else if(rank <= 10)
{
2022-02-18 12:25:28 +01:00
await (await user).roles.remove(rolesCollection.get('top1'));
await (await user).roles.add(rolesCollection.get('top10'));
await (await user).roles.remove(rolesCollection.get('top50'));
2022-02-17 15:04:39 +01:00
}
else if(rank <= 50)
{
2022-02-18 12:25:28 +01:00
await (await user).roles.remove(rolesCollection.get('top1'));
await (await user).roles.remove(rolesCollection.get('top10'));
await (await user).roles.add(rolesCollection.get('top50'));
}
else
{
await (await user).roles.remove(rolesCollection.get('top1'));
await (await user).roles.remove(rolesCollection.get('top10'));
await (await user).roles.remove(rolesCollection.get('top50'));
2022-02-17 15:04:39 +01:00
}
})
} catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.log(error.message);
});
}
2022-02-16 11:32:42 +01:00
2022-02-16 16:06:25 +01:00
CLIENT.on('ready', () => {
console.log(`Logged in as ${CLIENT.user.tag}!`);
setInterval(rank, 900000);
setInterval(remove_notlogged, 900000);
2022-02-17 15:04:39 +01:00
console.log('started rank() interval !');
2022-02-16 11:32:42 +01:00
});
2022-02-17 16:32:45 +01:00
CLIENT.login(D_TOKEN);