Compare commits

..

No commits in common. "e6b2b312ee4235aa27b2c7cbb6d406bc8ebe8c72" and "f364e545cd508ed51144a016319507a2a768c436" have entirely different histories.

5 changed files with 4431 additions and 85 deletions

View File

@ -4,14 +4,12 @@ ADD . /app/
WORKDIR /app
RUN apt-get update -yq \
&& apt-get install libffi-dev \
&& apt-get install libnacl-dev \
&& apt-get install python3-dev \
&& apt-get install python3-pip \
&& python3 -m pip install -U discord.py \
&& python3 -m pip install -U asyncio \
&& python3 -m pip install -U requests \
&& 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 \
&& npm i
CMD python3 bot.py
CMD node bot.js

147
src/bot.js Normal file
View File

@ -0,0 +1,147 @@
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.local/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")]
]);
const options = {
path: '/',
method: 'GET',
rejectUnauthorized: false,
requestCert: true,
agent: false
};
https.get(url, options, (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.local/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")]
]);
const options = {
path: '/',
method: 'GET',
rejectUnauthorized: false,
requestCert: true,
agent: false
};
https.get(url,options,(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);

View File

@ -1,77 +0,0 @@
import discord
import asyncio
import time
import requests
import colorama
intents = discord.Intents.all()
client = discord.Client(intents=intents)
def logger(type, message):
color = colorama.Fore.WHITE
if type == 'info':
color = colorama.Fore.GREEN
if type == 'warn':
color = colorama.Fore.YELLOW
if type == 'error':
color = colorama.Fore.RED
print(f"{colorama.Fore.BLUE}[{color}{type.upper()}{colorama.Fore.BLUE}]{colorama.Fore.WHITE}{message}")
async def get_json():
url = 'https://preprod.42ctf.org/api/bot/discord?token=test'
resp = requests.get(url=url)
data = resp.json()
return data
async def put_role(guild, userid, rank):
logger("info", f"userid {userid} has rank {rank}")
member = guild.get_member(user_id=userid)
if not member:
logger("warn", f"member with userid {userid} not found")
return
if rank < 1: # if rank is not in the top 50 or invalid
logger("error", f"rank {rank} is invalid")
return
if rank > 50:
await member.remove_roles(guild.get_role(role_id=943545946309029958))
await member.remove_roles(guild.get_role(role_id=943545892030517278))
await member.remove_roles(guild.get_role(role_id=943545814758854686))
return
if rank > 10: # if rank is not in the top 10
role = guild.get_role(role_id=943545946309029958)
await member.remove_roles(guild.get_role(role_id=943545892030517278))
await member.remove_roles(guild.get_role(role_id=943545814758854686))
await member.add_roles(role)
return
if rank > 1: # if rank is not top 1
role = guild.get_role(role_id=943545892030517278)
await member.remove_roles(guild.get_role(role_id=943545814758854686))
await member.remove_roles(guild.get_role(role_id=943545946309029958))
await member.add_roles(role)
return
if rank == 1: # if rank is top 1
role = guild.get_role(role_id=943545814758854686)
await member.remove_roles(guild.get_role(role_id=943545946309029958))
await member.remove_roles(guild.get_role(role_id=943545892030517278))
await member.add_roles(role)
return
async def routine(guild):
logger("info", "routine started")
while 1:
data = await get_json()
for key in data:
await put_role(guild, int(key), int(data[key]))
time.sleep(5000)
@client.event
async def on_ready():
logger("info", "logged as " + client.user.name)
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name='etre refait en python'))
guild = client.get_guild(943459216901955604)
await routine(guild)
client.run('OTQzNTA2NTgxMjkzNzc2OTQ3.GQ2l77.IyrlZv8-ESoXfL1MfSQw_ec3sXqOV1eh9PdNh4')

4270
src/package-lock.json generated Normal file

File diff suppressed because it is too large Load Diff

8
src/package.json Normal file
View File

@ -0,0 +1,8 @@
{
"dependencies": {
"@discordjs/rest": "*",
"discord-api-types": "*",
"discord.js": "*",
"npm": "*"
}
}