started redoing the bot

This commit is contained in:
Camille CAMBIUM 2022-10-27 05:41:34 +02:00
parent f364e545cd
commit e101c9718d
4 changed files with 41 additions and 4425 deletions

View File

@ -1,147 +0,0 @@
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);

41
src/bot.py Normal file
View File

@ -0,0 +1,41 @@
import discord
import asyncio
import time
import json
import requests
intents = discord.Intents.default()
client = discord.Client(intents=intents)
async def get_json():
url = 'https://preprod.42ctf.org/api/bot/discord?token=test'
params = dict(
origin='Chicago,IL',
destination='Los+Angeles,CA',
waypoints='Joplin,MO|Oklahoma+City,OK',
sensor='false'
)
resp = requests.get(url=url, params=params)
data = resp.json()
return data
async def routine():
while 1:
print("doing something here")
data = await get_json()
# print(data, type(data))
for key in data:
print(f'{key}, {data[key]}')
time.sleep(5000)
@client.event
async def on_ready():
print(f'We have logged in as {client.user}')
await client.change_presence(activity=discord.Activity(type=discord.ActivityType.playing, name='etre refait en python'))
await routine()
client.run('OTQzNTA2NTgxMjkzNzc2OTQ3.GQ2l77.IyrlZv8-ESoXfL1MfSQw_ec3sXqOV1eh9PdNh4')

4270
src/package-lock.json generated

File diff suppressed because it is too large Load Diff

View File

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