Compare commits

..

13 Commits

Author SHA1 Message Date
Starthur 1b1049acd2 Merge pull request 'botJS' (#1) from botJS into main
Reviewed-on: #1
2022-05-27 09:49:51 +02:00
CAMBIUM Camille 20970bc0a6 changed url 2022-05-01 14:25:42 +02:00
RoyaLProg 09c555ec78 dockerfiles + env lice 2022-02-18 20:54:27 +01:00
RoyaLProg d4127f25ce removed useless logs ^^' 2022-02-18 14:44:55 +01:00
RoyaLProg 639bd04d75 Finished bot, should work properly (coffee time !) 2022-02-18 14:43:40 +01:00
RoyaLProg 283fbf76ec rage quit 2022-02-18 12:25:28 +01:00
RoyaLProg 4340d0814d Working fine! (but ugly) 2022-02-17 16:32:45 +01:00
RoyaLProg e19bb4f64d removed packages + bot 2022-02-17 15:04:39 +01:00
RoyaLProg ab3b8dff43 Command "connect" ready but deprecated, waiting for new API to async work on role event 2022-02-16 19:20:47 +01:00
RoyaLProg 01e10e4506 Bot core (operationel) 2022-02-16 16:07:03 +01:00
RoyaLProg 5c3a40598e Bot core (non operationel) 2022-02-16 16:07:03 +01:00
RoyaLProg 3e512a0f75 Starting bot dev 2022-02-16 11:32:42 +01:00
RoyaLProg 3660d51355 Init branch 2022-02-16 11:06:45 +01:00
7 changed files with 4604 additions and 0 deletions

5
.dockerignore Normal file
View File

@ -0,0 +1,5 @@
bot.py
README.md
node_modules
env.list
.git

5
.gitignore vendored Normal file
View File

@ -0,0 +1,5 @@
.vscode
node_modules
node_modules/npm/node_modules/pacote/lib/util

10
Dockerfile Normal file
View File

@ -0,0 +1,10 @@
FROM debian:9
RUN apt-get update -yq \
&& apt-get install curl gnupg -yq \
&& curl -sL https://deb.nodesource.com/setup_16.x |bash -\
&& apt-get install nodejs -yq \
&& apt-get clean -y
ADD . /app/
WORKDIR /app
CMD npm i && npm remove discord-api-types && npm i discord-api-types@0.27.2 && node bot.js

131
bot.js Normal file
View File

@ -0,0 +1,131 @@
const { Client, Intents } = require('discord.js');
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MEMBERS] });
const https = require('https');
const D_TOKEN = process.env.DISCORD_TOKEN;
const O_TOKEN = process.env.AUTH_TOKEN;
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;
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);
});
}
async function rank(token = O_TOKEN){
let url = `https://www.42ctf.org/api/bot/discord?token=${token}`;
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")]
]);
https.get(url,(res) => {
var body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
var json = JSON.parse(body);
let v = new Map(Object.entries(json))
v.forEach(async (key, value) => {
let user = guild.members.fetch(value);
let rank = parseInt(key);
if(rank == 1)
{
await (await user).roles.add(rolesCollection.get('top1'));
await (await user).roles.remove(rolesCollection.get('top10'));
await (await user).roles.remove(rolesCollection.get('top50'));
}
else if(rank <= 10)
{
await (await user).roles.remove(rolesCollection.get('top1'));
await (await user).roles.add(rolesCollection.get('top10'));
await (await user).roles.remove(rolesCollection.get('top50'));
}
else if(rank <= 50)
{
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'));
}
})
} catch (error) {
console.error(error.message);
};
});
}).on("error", (error) => {
console.log(error.message);
});
}
CLIENT.on('ready', () => {
console.log(`Logged in as ${CLIENT.user.tag}!`);
setInterval(rank, 900000);
setInterval(remove_notlogged, 900000);
console.log('started rank() interval !');
});
CLIENT.login(D_TOKEN);

3
env.list Normal file
View File

@ -0,0 +1,3 @@
DISCORD_TOKEN=OTQzNTA2NTgxMjkzNzc2OTQ3.Yg0C-g.XOjFinUc-Gt1gwtrSILPb0Mlspo
AUTH_TOKEN=test

4442
package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

8
package.json Normal file
View File

@ -0,0 +1,8 @@
{
"dependencies": {
"@discordjs/rest": "^0.3.0",
"discord-api-types": "^0.27.2",
"discord.js": "^13.6.0",
"npm": "^8.5.0"
}
}