botJS #1

Merged
Starthur merged 12 commits from botJS into main 2022-05-27 09:49:52 +02:00
1 changed files with 60 additions and 6 deletions
Showing only changes of commit 639bd04d75 - Show all commits

66
bot.js
View File

@ -1,5 +1,5 @@
const { Client, Intents } = require('discord.js'); const { Client, Intents } = require('discord.js');
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] }); const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
const https = require('https'); const https = require('https');
const D_TOKEN = process.env.DISCORD_TOKEN; const D_TOKEN = process.env.DISCORD_TOKEN;
@ -12,14 +12,68 @@ async function get_guildID()
return v; return v;
} }
async function remove_notlogged(){
let guild = CLIENT.guilds.resolve(await get_guildID());
let token = O_TOKEN;
let url = `https://preprod.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;
console.log("---------------------\nkey : %s\nvalue : %s", key, value);
v.forEach((jkey, jvalue) => {
if (jvalue == key)
r = true;
})
console.log(r + "\n---------------------");
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);
});
}
async function rank(token = O_TOKEN){ async function rank(token = O_TOKEN){
let url = `https://preprod.42ctf.org/api/bot/discord?token=${token}`; let url = `https://preprod.42ctf.org/api/bot/discord?token=${token}`;
let guild = CLIENT.guilds.resolve(await get_guildID()); let guild = CLIENT.guilds.resolve(await get_guildID());
const rolesCollection = new Map([ const rolesCollection = new Map([
['top1', guild.roles.cache.get("943545814758854686")], ['top1', guild.roles.cache.get("798638767359524875")],
['top10', guild.roles.cache.get("943545892030517278")], ['top10', guild.roles.cache.get("801787467064672286")],
['top50', guild.roles.cache.get("943545946309029958")] ['top50', guild.roles.cache.get("803729539145924649")]
]); ]);
https.get(url,(res) => { https.get(url,(res) => {
@ -71,8 +125,8 @@ async function rank(token = O_TOKEN){
CLIENT.on('ready', () => { CLIENT.on('ready', () => {
console.log(`Logged in as ${CLIENT.user.tag}!`); console.log(`Logged in as ${CLIENT.user.tag}!`);
setInterval(rank, 5000); setInterval(rank, 900000);
setInterval(remove_notlogged, 5000); setInterval(remove_notlogged, 900000);
console.log('started rank() interval !'); console.log('started rank() interval !');
}); });