Compare commits

...

2 Commits

Author SHA1 Message Date
Nemo 61798a6067 minor file changes 2021-11-12 13:05:35 +01:00
Nemo 93922cca75 fonctionnalité de wordwar prête pour l'utilisation 2021-11-12 13:03:46 +01:00
4 changed files with 172 additions and 106 deletions

View File

@ -1,5 +1,5 @@
const { SlashCommandBuilder, codeBlock } = require('@discordjs/builders');
const { userMention } = require("@discordjs/builders");
module.exports = {
data: new SlashCommandBuilder()

View File

@ -1,6 +1,8 @@
const { SlashCommandBuilder, blockQuote, userMention } = require('@discordjs/builders');
const { MessageActionRow, MessageButton, Message } = require('discord.js');
const ongoingww = [];
const { SlashCommandBuilder, blockQuote, userMention, roleMention } = require('@discordjs/builders');
const { MessageActionRow, MessageButton, Collection } = require('discord.js');
const ongoingww = new Collection();
const intTable = new Collection();
var wwId = 1;
/**
* This function aims to do word wars inside a discord channel.
@ -23,110 +25,179 @@ module.exports = {
.setRequired(false)
),
async execute(interaction) {
if (ongoingww.includes(interaction.user.id)) {
interaction.channel.send("<@" + interaction.user.id + "> a déjà une wordwar en cours, merci de patienter pour en relancer une nouvelle !")
// Vérifie que l'utilisateur ne participe pas déjà à une ww en regardant ses rôles
if (ongoingww.has(interaction.user.id)) {
interaction.reply({ content: "Vous avez déjà une wordwar en cours.", ephemeral: true });
return;
}
// On vérifie que la commande a correctement été entrée
var wwtimer;
var wwdelay;
if (interaction.options.get('duree') == null) {
wwtimer = 15;
} else {
var wwtimer;
var wwdelay;
if (interaction.options.get('duree') == null) {
wwtimer = 20;
} else {
wwtimer = interaction.options.get('duree').value;
if (isNormalInteger(wwtimer)) {
if (wwtimer > 30) {
interaction.channel.send("Le temps d'une wordwar ne peux pas excéder **30 minutes** sur ce channel. Merci de réessayer !");
return;
} else if (wwtimer < 1) {
return;
}
} else {
interaction.channel.send("Le temps d'une wordwar doit être un entier positif compris entre **1 minute** et **30 minutes**. Merci de réessayer !")
wwtimer = interaction.options.get('duree').value;
if (isNormalInteger(wwtimer)) {
if (wwtimer > 30) {
interaction.reply({ content: "Le temps d'une wordwar ne peux pas excéder **30 minutes** sur ce channel. Merci de réessayer !", ephemeral: true });
return;
} // && Number.isInteger(args[1])
}
if (interaction.options.get('attente') == null) {
wwdelay = 2;
} else {
wwdelay = interaction.options.get('attente').value;
if (isNormalInteger(wwdelay)) {
if (wwdelay > 15) {
interaction.channel.send("Le temps d'attente d'une wordwar ne peux pas excéder **15 minutes** sur ce channel. Merci de réessayer !");
return;
} else if (wwdelay < 1) {
interaction.channel.send("Le temps d'attente d'une wordwar ne peux pas être inférieur à **1 minute** sur ce channel. Merci de réessayer !")
return;
}
} else {
interaction.channel.send("Le temps d'attente d'une wordwar doit être un entier positif compris entre **1 minute** et **15 minutes**. Merci de réessayer !")
} else if (wwtimer < 1) {
return;
}
}
memberid = interaction.user.id;
ongoingww.push(memberid);
commandReply = "Une wordwar a été initiée par " + userMention(memberid) + " . Elle commencera dans " + wwdelay + " minutes et durera " + wwtimer + " minutes.";
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('primary')
.setLabel('Participer')
.setStyle('SUCCESS')
)
.addComponents(
new MessageButton()
.setCustomId('secondary')
.setLabel('Annuler')
.setStyle('DANGER')
.setDisabled(true)
);
message = await interaction.reply({ content: commandReply, components: [row] })
.then(function (message) {
/* const collector = message.createMessageComponentCollector({
time: (1000 * 60 * (wwtimer + wwdelay))
})
collector.on('collect', (buttonClick) => {
buttonClick.reply({
content: 'Enregistré pour la wordwar',
ephemeral: true
})
});
*/
/* interaction.react('');
setTimeout(() => {
reaction = interaction.reactions.cache.find(r => r.name = '✅');
users = reaction.users.cache;
var participantStart = "";
users.forEach(element => {
participantStart += userMention(element.id);
});
interaction.channel.send("\nLa wordwar initiée par " + userMention(memberid) + " commence. Vous pouvez toujours la rejoindre si vous le désirez. Bonne chance à tou.te.s !!\n" + participantStart)
var participantTab = [];
setTimeout(function () {
participantTab += 'La wordwar initiée par ' + userMention(memberid) + ' est maintenant terminée, merci d\'y avoir participé \!\n';
reaction = interaction.reactions.cache.find(r => r.name = '✅');
users = reaction.users.cache;
users.forEach(element => {
participantTab += userMention(element.id);
});
interaction.channel.send(participantTab);
}, wwtimer * 60000);
ongoingww.splice(ongoingww.indexOf(memberid), 1);
}, wwdelay * 60000); */
});
} else {
interaction.reply({ content: "Le temps d'une wordwar doit être un entier positif compris entre **1 minute** et **30 minutes**. Merci de réessayer !", ephemeral: true });
return;
} // && Number.isInteger(args[1])
}
},
};
if (interaction.options.get('attente') == null) {
wwdelay = 15;
} else {
wwdelay = interaction.options.get('attente').value;
if (isNormalInteger(wwdelay)) {
if (wwdelay > 15) {
interaction.reply({ content: "Le temps d'attente d'une wordwar ne peux pas excéder **15 minutes** sur ce channel. Merci de réessayer !", ephemeral: true });
return;
} else if (wwdelay < 1) {
interaction.reply({ content: "Le temps d'attente d'une wordwar ne peux pas être inférieur à **1 minute** sur ce channel. Merci de réessayer !", ephemeral: true });
return;
}
} else {
interaction.reply({ content: "Le temps d'attente d'une wordwar doit être un entier positif compris entre **1 minute** et **15 minutes**. Merci de réessayer !", ephemeral: true });
return;
}
}
//Si la commande a bien été entrée, on continue
//Create a role with a specific ID for this ww
var newRole = {
name: "WordWar#" + wwId,
color: "RANDOM",
mentionable: true,
reason: "Rôle temporaire pour les wordwar"
};
var wwRole = await interaction.guild.roles.create(newRole);
wwId++;
ongoingww.set(interaction.user.id, wwRole.name);
interaction.member.roles.add(wwRole.id);
commandReply = "Une wordwar a été initiée. Elle commencera dans " + wwdelay + " minutes et durera " + wwtimer + " minutes.";
const row = new MessageActionRow()
.addComponents(
new MessageButton()
.setCustomId('primary')
.setLabel('Participer')
.setStyle('SUCCESS')
)
.addComponents(
new MessageButton()
.setCustomId('secondary')
.setLabel('Annuler')
.setStyle('DANGER')
);
interaction.reply({ content: commandReply, components: [row] })
.then(() => {
const filterParticipate = (btnInt) => btnInt.customId === 'primary';
const collectorParticipate = interaction.channel.createMessageComponentCollector({
filter: filterParticipate,
});
collectorParticipate.on('collect', async i => {
if (i.member.roles.cache.has(wwRole.id)) {
OnlyReply('Vous êtes déjà enregistré pour cette wordwar', i, wwRole);
} else if (ongoingww.has(i.user.id)) {
OnlyReply('Vous êtes déjà enregistré dans la wordwar ' + ongoingww.get(i.user.id), i, wwRole);
} else {
i.member.roles.add(wwRole);
ongoingww.set(i.user.ida, wwRole);
OnlyReply('Participation est enregistrée', i, wwRole);
}
});
const filterCancel = (btnInt) => btnInt.customId === 'secondary';
const collectorCancel = interaction.channel.createMessageComponentCollector({
filter: filterCancel,
});
collectorCancel.on('collect', async i => {
console.log("pouet on passe combien de fois ici ?");
if (!i.member.roles.cache.has(wwRole.id)) {
OnlyReply('Vous n\'êtes pas enregistré pour cette wordwar', i, wwRole);
} else {
OnlyReply('Participation est annulée', i, wwRole);
i.member.roles.remove(wwRole);
ongoingww.delete(i.user.id);
// Si l'utilisateur qui annule est le dernier dans cette wordwar
if (typeof ongoingww.find(wordWar => wordWar == wwRole.name) == "undefined") {
row.components.forEach(element => {
element.setDisabled(true);
});
i.message.edit({ content: "La " + wwRole.name + " a été annulée par manque de participants.", components: [row] });
wwRole.delete();
collectorCancel.stop();
collectorParticipate.stop();
return;
}
}
});
if (wwRole.deleted != true) {
setTimeout(async () => {
console.log('Wordwar launched');
var participantStart = '\nLa wordwar ' + wwRole.name + ' commence. Vous pouvez toujours la rejoindre si vous le désirez. Bonne chance à tou.te.s !! ' + roleMention(wwRole.id) + '\n';
var sentMsg = await interaction.channel.send({ content: participantStart, fetchReply: true });
if (wwRole.deleted != true) {
setTimeout(async () => {
if (wwRole.deleted != true) {
console.log('Wordwar ended');
var participantEnd = 'La wordwar ' + wwRole.name + ' est maintenant terminée, merci d\'y avoir participé \! ' + roleMention(wwRole.id) + '\n';
interaction.channel.send(participantEnd);
ongoingww.forEach(element => {
if (element.value == wwRole.name) {
element.delete();
}
});
setTimeout(() => {
wwRole.delete();
collectorCancel.stop();
collectorParticipate.stop();
}, 30 * 1000); // Wait 30 seconds to delete the role
} else {
sentMsg.delete();
console.log('Wordwar canceled');
}
}, wwtimer * 1000);
} else {
sentMsg.delete();
console.log('Wordwar canceled');
}
}, wwdelay * 1000);
}
});
/* }
*/ }
}
function isNormalInteger(str) {
var n = Math.floor(Number(str));
return n !== Infinity && String(n) === str && n >= 0;
}
function OnlyReply(str, int, ww) {
if (!intTable.has(ww.name + int.user.id)) {
intTable.set(ww.name + int.user.id, int);
int.reply({ content: str, ephemeral: true });
} else {
intTable.get(ww.name + int.user.id).editReply({ content: str, ephemeral: true });
int.deferUpdate();
}
}

View File

@ -1,11 +1,6 @@
module.exports = {
name: 'interactionCreate',
async execute(interaction) {
if(interaction.isButton()){
console.log(interaction);
interaction.reply({ content: `${interaction.user.tag} clicked me`, ephemeral : true });
}
if (!interaction.isCommand()) return;

View File

@ -10,7 +10,7 @@ const client = new Client({ intents: [Intents.FLAGS.GUILDS] });
client.commands = new Collection();
const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));
for (const file of commandFiles) {
for (const file of commandFiles ) {
const command = require(`./commands/${file}`);
// Set a new item in the Collection
// With the key as the command name and the value as the exported module