Command "connect" ready but deprecated, waiting for new API to async work on role event

This commit is contained in:
RoyaLProg 2022-02-16 19:20:47 +01:00
parent 01e10e4506
commit ab3b8dff43
1 changed files with 73 additions and 10 deletions

81
bot.js
View File

@ -2,21 +2,51 @@ const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents} = require('discord.js');
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
const https = require('https');
const GUILD = '42ctf';
const TOKEN = "OTQzNTA2NTgxMjkzNzc2OTQ3.Yg0C-g.XOjFinUc-Gt1gwtrSILPb0Mlspo";
const GUILD = "42CTFDEV";
const rank = async (token) => {
var url = `https://www.42ctf.org/en/accounts/rank/${token}`;
var r;
return new Promise(function(resolve, reject)
{
https.get(url,(res) => {
let body = "";
res.on("data", (chunk) => {
body += chunk;
});
res.on("end", () => {
try {
var json = JSON.parse(body);
r = json.rank;
resolve(r);
} catch (error) {
console.error(error.message);
resolve(0);
};
});
}).on("error", (error) => {
reject(error.message);
});
});
}
const commands = [{
name: 'connect',
description: 'connect to discord',
options:
[{
type: 3, //string
name: 'token',
description: 'token given at your 42ctf profile',
required: true
}]
}];
const roles = new Map([
['top1', "798638767359524875"],
['top10', "801787467064672286"],
['top50', "803729539145924649"]
]);
const rest = new REST({ version: '9' }).setToken(TOKEN);
(async () => {
@ -34,7 +64,6 @@ const rest = new REST({ version: '9' }).setToken(TOKEN);
}
})();
var guild;
CLIENT.on('ready', () => {
console.log(`Logged in as ${CLIENT.user.tag}!`);
guild = CLIENT.guilds.resolve(GUILD);
@ -43,8 +72,42 @@ CLIENT.on('ready', () => {
CLIENT.on('interactionCreate', async interaction => {
if (!interaction.isCommand()) return;
const roles = new Map([
['top1', interaction.guild.roles.cache.get("943545814758854686")],
['top10', interaction.guild.roles.cache.get("943545892030517278")],
['top50', interaction.guild.roles.cache.get("943545946309029958")],
['42', interaction.guild.roles.cache.get("943554331439951952")]
]);
if (interaction.commandName === 'connect') {
await interaction.reply('OK!');
var token = interaction.options.getString("token");
var v = await rank(token);
if(v == 1)
{
await interaction.reply("Bien joué tu es premier !")
await interaction.member.roles.add(roles.get('top1'));
await interaction.member.roles.remove(roles.get('top10'));
await interaction.member.roles.remove(roles.get('top50'));
}
else if( v <= 10)
{
await interaction.reply("Bien joué tu es dans le top 10 !");
await interaction.member.roles.remove(roles.get('top1'));
await interaction.member.roles.add(roles.get('top10'));
await interaction.member.roles.remove(roles.get('top50'));
}
else if(v <= 50)
{
await interaction.reply("Bien joué tu es dans le top 50 !");
await interaction.member.roles.remove(roles.get('top1'));
await interaction.member.roles.remove(roles.get('top10'));
await interaction.member.roles.add(roles.get('top50'));
}
else
{
await interaction.reply("Il n'y a aucun role pour toi !");
await interaction.member.roles.add(roles.get('42'));
}
}
});