mirror of
https://github.com/arthur-pbty/gestion.git
synced 2026-06-09 01:01:47 +02:00
90 lines
3.8 KiB
JavaScript
90 lines
3.8 KiB
JavaScript
const { ChannelType, EmbedBuilder } = require('discord.js');
|
|
const sqlite3 = require('sqlite3').verbose();
|
|
|
|
module.exports = {
|
|
name: 'rvocal',
|
|
description: 'Enregistre les rôles associés à un salon vocal',
|
|
emote: '🔊',
|
|
utilisation: '+rvocal <salon ou id de salon> <role ou id de role>',
|
|
category: 'gestion',
|
|
|
|
async execute(message, args) {
|
|
const db = new sqlite3.Database('myDatabase.db');
|
|
const botId = message.client.user.id;
|
|
const guildId = message.guild.id;
|
|
let mentionedChannel = message.mentions.channels.first();
|
|
if (!mentionedChannel) {
|
|
const channelId = args[0];
|
|
mentionedChannel = message.guild.channels.cache.get(channelId);
|
|
if (!mentionedChannel || mentionedChannel.type !== ChannelType.GuildVoice) {
|
|
return message.channel.send("Veuillez mentionner un salon vocal valide ou fournir un ID de salon vocal valide.");
|
|
}
|
|
}
|
|
if (mentionedChannel.type !== ChannelType.GuildVoice) {
|
|
return message.channel.send("Veuillez mentionner un salon vocal valide ou fournir un ID de salon vocal valide.");
|
|
}
|
|
|
|
if (args.length === 1 ) {
|
|
const roles = await new Promise((resolve, reject) => {
|
|
db.get('SELECT value FROM gestion WHERE id = ?', [botId], (err, row) => {
|
|
if (err) {
|
|
console.error(err.message);
|
|
reject(err);
|
|
}
|
|
resolve(row ? JSON.parse(row.value) : {});
|
|
});
|
|
});
|
|
|
|
const channelRoles = roles.rolevocal && roles.rolevocal[guildId] && roles.rolevocal[guildId][mentionedChannel.id] || [];
|
|
|
|
const embed = new EmbedBuilder()
|
|
.setTitle(`Rôles associés au salon vocal <#${mentionedChannel.id}>`)
|
|
.setDescription(channelRoles.length > 0 ? channelRoles.map(roleId => `<@&${roleId}>`).join(', ') : 'Aucun rôle associé.')
|
|
.setColor('#0099ff');
|
|
|
|
return message.channel.send({ embeds: [embed] });
|
|
}
|
|
if (mentionedChannel && args.length > 1) {
|
|
const roles = [];
|
|
for (let i = 1; i < args.length; i++) {
|
|
const roleId = args[i].match(/^<@&(\d+)>$/) || args[i].match(/^(\d+)$/);
|
|
if (roleId) {
|
|
const role = message.guild.roles.cache.get(roleId[1]);
|
|
if (role) {
|
|
roles.push(role.id);
|
|
} else {
|
|
return message.channel.send("Un ou plusieurs rôles mentionnés sont invalides.");
|
|
}
|
|
} else {
|
|
return message.channel.send("Un ou plusieurs rôles mentionnés sont invalides.");
|
|
}
|
|
}
|
|
|
|
let data = await new Promise((resolve, reject) => {
|
|
db.get('SELECT value FROM gestion WHERE id = ?', [botId], (err, row) => {
|
|
if (err) {
|
|
console.error(err.message);
|
|
reject(err);
|
|
}
|
|
resolve(row ? JSON.parse(row.value) : {});
|
|
});
|
|
});
|
|
|
|
if (!data.rolevocal) {
|
|
data.rolevocal = {};
|
|
}
|
|
if (!data.rolevocal[guildId]) {
|
|
data.rolevocal[guildId] = {};
|
|
}
|
|
data.rolevocal[guildId][mentionedChannel.id] = roles;
|
|
|
|
db.run('INSERT OR REPLACE INTO gestion (id, value) VALUES (?, ?)', [botId, JSON.stringify(data)], (err) => {
|
|
if (err) {
|
|
console.error(err.message);
|
|
}
|
|
});
|
|
|
|
message.channel.send(`Les rôles ont été enregistrés pour le salon vocal <#${mentionedChannel.id}>.`);
|
|
}
|
|
},
|
|
}; |