bot/bot.js

115 lines
3.0 KiB
JavaScript
Raw Normal View History

2022-02-16 11:32:42 +01:00
const { REST } = require('@discordjs/rest');
const { Routes } = require('discord-api-types/v9');
const { Client, Intents} = require('discord.js');
2022-02-16 12:28:10 +01:00
const CLIENT = new Client({ intents: [Intents.FLAGS.GUILDS] });
const https = require('https');
2022-02-16 12:28:10 +01:00
const GUILD = '42ctf';
2022-02-16 16:06:25 +01:00
const TOKEN = "OTQzNTA2NTgxMjkzNzc2OTQ3.Yg0C-g.XOjFinUc-Gt1gwtrSILPb0Mlspo";
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);
});
});
}
2022-02-16 11:32:42 +01:00
const commands = [{
2022-02-16 12:28:10 +01:00
name: 'connect',
2022-02-16 16:06:25 +01:00
description: 'connect to discord',
options:
[{
type: 3, //string
name: 'token',
description: 'token given at your 42ctf profile',
required: true
}]
2022-02-16 11:32:42 +01:00
}];
const rest = new REST({ version: '9' }).setToken(TOKEN);
(async () => {
2022-02-16 12:28:10 +01:00
try {
console.log('Started refreshing application (/) commands.');
2022-02-16 11:32:42 +01:00
await rest.put(
2022-02-16 16:06:25 +01:00
Routes.applicationGuildCommands('943506581293776947', '943459216901955604'),
2022-02-16 12:28:10 +01:00
{ body: commands },
2022-02-16 11:32:42 +01:00
);
console.log('Successfully reloaded application (/) commands.');
} catch (error) {
console.error(error);
}
})();
2022-02-16 16:06:25 +01:00
CLIENT.on('ready', () => {
console.log(`Logged in as ${CLIENT.user.tag}!`);
guild = CLIENT.guilds.resolve(GUILD);
2022-02-16 11:32:42 +01:00
});
2022-02-16 16:06:25 +01:00
CLIENT.on('interactionCreate', async interaction => {
2022-02-16 12:28:10 +01:00
if (!interaction.isCommand()) return;
2022-02-16 11:32:42 +01:00
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")]
]);
2022-02-16 12:28:10 +01:00
if (interaction.commandName === 'connect') {
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'));
}
2022-02-16 12:28:10 +01:00
}
2022-02-16 11:32:42 +01:00
});
2022-02-16 16:06:25 +01:00
CLIENT.login(TOKEN);