From 728d65421af1e8a7d50864d756851be64ee1ee9d Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 01:47:00 +0200 Subject: [PATCH 01/21] installed discord-modals --- package-lock.json | 15 +++++++++++++++ package.json | 1 + 2 files changed, 16 insertions(+) diff --git a/package-lock.json b/package-lock.json index d358676..57fe7ba 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,6 +12,7 @@ "@discordjs/builders": "^0.13.0", "@discordjs/rest": "^0.4.1", "discord-api-types": "^0.32.1", + "discord-modals": "^1.3.6", "discord.js": "^13.7.0", "dotenv": "^16.0.1" }, @@ -392,6 +393,14 @@ "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, + "node_modules/discord-modals": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/discord-modals/-/discord-modals-1.3.6.tgz", + "integrity": "sha512-1RorghCu9LFU7qhmxppgz9U498Re9OGvGQ6NaRKwg4X9GxH9Ymj1kQNWu4274vlWL0AbYO2ua9o1kiK7BjD+lg==", + "peerDependencies": { + "discord.js": "*" + } + }, "node_modules/discord.js": { "version": "13.7.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.7.0.tgz", @@ -1525,6 +1534,12 @@ "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, + "discord-modals": { + "version": "1.3.6", + "resolved": "https://registry.npmjs.org/discord-modals/-/discord-modals-1.3.6.tgz", + "integrity": "sha512-1RorghCu9LFU7qhmxppgz9U498Re9OGvGQ6NaRKwg4X9GxH9Ymj1kQNWu4274vlWL0AbYO2ua9o1kiK7BjD+lg==", + "requires": {} + }, "discord.js": { "version": "13.7.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.7.0.tgz", diff --git a/package.json b/package.json index 25f48cc..62fa450 100644 --- a/package.json +++ b/package.json @@ -22,6 +22,7 @@ "@discordjs/builders": "^0.13.0", "@discordjs/rest": "^0.4.1", "discord-api-types": "^0.32.1", + "discord-modals": "^1.3.6", "discord.js": "^13.7.0", "dotenv": "^16.0.1" }, From dee010b9e5a384dca27d45e7d2d1f4a293b5be3a Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 01:47:16 +0200 Subject: [PATCH 02/21] added giveaway command --- src/commands/giveaway.js | 45 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 45 insertions(+) create mode 100644 src/commands/giveaway.js diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js new file mode 100644 index 0000000..176aa4f --- /dev/null +++ b/src/commands/giveaway.js @@ -0,0 +1,45 @@ +const { SlashCommandBuilder } = require('@discordjs/builders'); +const modal = require('discord-modals'); + +module.exports = { + data: new SlashCommandBuilder() + .setName('giveaway') + .setDescription('An admin command for creating giveaways'), + async execute(interaction) { + modal(interaction.client); + let m = new modal.Modal() + .setCustomId('giveaway') + .setTitle('Create a giveaway') + .addComponents([ + new modal.TextInputComponent() + .setCustomId('gamekey') + .setLabel('Game key') + .setStyle('SHORT') + .setRequired(true), + new modal.TextInputComponent() + .setCustomId('description') + .setLabel('Description') + .setStyle('SHORT') + .setRequired(true), + new modal.TextInputComponent() + .setCustomId('timelimit') + .setLabel('Time limit (minutes)') + .setStyle('SHORT') + .setRequired(true), + new modal.TextInputComponent() + .setCustomId('emoji') + .setLabel('Emoji to react with (without ::)') + .setStyle('SHORT') + .setRequired(true), + new modal.TextInputComponent() + .setCustomId('image') + .setLabel('Image') + .setStyle('SHORT') + .setRequired(false) + ]); + return await modal.showModal(m, { + interaction: interaction, + client: interaction.client + }) + } +}; \ No newline at end of file From dee5e4bbc9df6295c08623e90292c82988402de7 Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 01:47:44 +0200 Subject: [PATCH 03/21] modalSubmit event --- src/events/modalSubmit.js | 106 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 106 insertions(+) create mode 100644 src/events/modalSubmit.js diff --git a/src/events/modalSubmit.js b/src/events/modalSubmit.js new file mode 100644 index 0000000..769c931 --- /dev/null +++ b/src/events/modalSubmit.js @@ -0,0 +1,106 @@ +const { time } = require('@discordjs/builders'); +const { MessageEmbed, Message } = require('discord.js'); + +function createGiveawayEmbed(description, timelimit, emoji, image, winner) { + let standardEmbedFields = [{ + name: "🎁 Item", + value: description + }, + { + name: "➡️ How to enter", + value: "React to this message with " + emoji + }, + { + name: "🏆 Winners", + value: "Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!" + }] + if (winner != "") { + let embed = new MessageEmbed() + .setColor('GREEN') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + { + name: "🏁 Giveaway ended", + value: "Winner: " + winner + } + ) + .setImage(image); + return embed; + } + let embed = new MessageEmbed() + .setColor('YELLOW') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2] + ) + .setImage(image) + .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?"hour":"hours"}`}); + return embed; +} + +module.exports = { + name: "modalSubmit", + async execute (modal) { + try { + if (modal.customId == 'giveaway') { + + //const channel = '838711380076396574'; + const channel = '975791268741406800'; + const gamekey = modal.getTextInputValue('gamekey'); + const description = modal.getTextInputValue('description'); + let timelimit = modal.getTextInputValue('timelimit'); + const emoji = `${modal.client.emojis.cache.find(emoji => emoji.name == modal.getTextInputValue("emoji"))}`; + if (emoji == 'undefined') { + reply = await modal.reply({content:"Emoji not found. Only custom server-emojis work.", ephemeral:true}); + return; + } + let image = ''; + if (modal.getTextInputValue('image') != null) { + image = modal.getTextInputValue('image'); + } + + let embed = createGiveawayEmbed(description,timelimit,emoji,image,""); + + let msg = await modal.client.channels.cache.get(channel).send({embeds: [embed]}); + msg.react(emoji); + + let interval = setInterval(() => { + timelimit -= 1; + embed = createGiveawayEmbed(description,timelimit,emoji,image,""); + msg.edit({embeds: [embed]}); + if (timelimit < 1) { + clearInterval(interval); + let winner; + msg.reactions.cache.map(async (reaction) => { + let emojiName = emoji.split(":")[1]; + if (reaction.emoji.name !== emojiName) return; + let usersThatReacted = []; + let reactedUsers = await reaction.users.fetch(); + reactedUsers.map((user) => { + if (user != msg.author) { + usersThatReacted.push(user); + } + }); + if (usersThatReacted.length == 0) { + winner = "No one reacted! :(" + } else { + winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; + winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); + } + + embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); + msg.edit({embeds: [embed]}); + }); + + } + }, 60000); + await modal.reply({content: "Done!", ephemeral: true}); + }; + } catch (e) { + console.log(e); + } + } +} \ No newline at end of file From 87fadd71c29f7ecf5b12bfede8b1ff3fdd55bb32 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Fri, 20 May 2022 01:48:40 +0200 Subject: [PATCH 04/21] added giveaway command --- src/commands/giveaway.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js index 176aa4f..ca6b29e 100644 --- a/src/commands/giveaway.js +++ b/src/commands/giveaway.js @@ -37,9 +37,9 @@ module.exports = { .setStyle('SHORT') .setRequired(false) ]); - return await modal.showModal(m, { + await modal.showModal(m, { interaction: interaction, client: interaction.client }) } -}; \ No newline at end of file +}; From 5887b032f402b67df0dc8ed3e8af106c4fe30cc1 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Fri, 20 May 2022 01:49:53 +0200 Subject: [PATCH 05/21] added modalSubmit event --- src/events/modalSubmit.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/events/modalSubmit.js b/src/events/modalSubmit.js index 769c931..308b747 100644 --- a/src/events/modalSubmit.js +++ b/src/events/modalSubmit.js @@ -54,7 +54,7 @@ module.exports = { let timelimit = modal.getTextInputValue('timelimit'); const emoji = `${modal.client.emojis.cache.find(emoji => emoji.name == modal.getTextInputValue("emoji"))}`; if (emoji == 'undefined') { - reply = await modal.reply({content:"Emoji not found. Only custom server-emojis work.", ephemeral:true}); + await modal.reply({content:"Emoji not found. Only custom server-emojis work.", ephemeral:true}); return; } let image = ''; @@ -103,4 +103,4 @@ module.exports = { console.log(e); } } -} \ No newline at end of file +} From 61f56801a2d64ee2e71b5b4b2b7d0baee6e3d448 Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 17:39:45 +0200 Subject: [PATCH 06/21] required discord-modals and set modal(client) --- index.js | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/index.js b/index.js index 40380b9..dde07f0 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const path = require('path'); const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); const { Client, Collection, Intents } = require('discord.js'); +const modal = require("discord-modals"); const token = process.env.TOKEN; const clientId = process.env.CLIENT_ID; @@ -13,6 +14,8 @@ const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], }); +modal(client); + // Load events from event files. const eventsPath = path.join(__dirname, 'src', 'events'); const eventFiles = fs @@ -75,7 +78,7 @@ client.on('interactionCreate', async (interaction) => { } try { - await command.execute(interaction); + await command.execute(interaction, modal); } catch (error) { await interaction.reply({ content: 'There was an error while executing this command!', From 0db0b1283f1ff109eefa3343c918b26dedcd2f64 Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 17:40:51 +0200 Subject: [PATCH 07/21] moved require(discord-modals) to index.js --- src/commands/giveaway.js | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js index 176aa4f..e46d66d 100644 --- a/src/commands/giveaway.js +++ b/src/commands/giveaway.js @@ -1,12 +1,10 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); -const modal = require('discord-modals'); module.exports = { data: new SlashCommandBuilder() .setName('giveaway') .setDescription('An admin command for creating giveaways'), - async execute(interaction) { - modal(interaction.client); + async execute(interaction, modal) { let m = new modal.Modal() .setCustomId('giveaway') .setTitle('Create a giveaway') @@ -37,7 +35,7 @@ module.exports = { .setStyle('SHORT') .setRequired(false) ]); - return await modal.showModal(m, { + await modal.showModal(m, { interaction: interaction, client: interaction.client }) From e0c0a8d68c6db5c39fb5a98bf909b44bc0e17335 Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 17:41:12 +0200 Subject: [PATCH 08/21] added modalSubmit event --- src/events/modalSubmit.js | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/src/events/modalSubmit.js b/src/events/modalSubmit.js index 769c931..1f803c7 100644 --- a/src/events/modalSubmit.js +++ b/src/events/modalSubmit.js @@ -26,7 +26,8 @@ function createGiveawayEmbed(description, timelimit, emoji, image, winner) { value: "Winner: " + winner } ) - .setImage(image); + .setImage(image) + .setFooter({text: `Giveaway ended`}); return embed; } let embed = new MessageEmbed() From 727cf9e01e9f2b9ed9c6cee2ba42e7e8265c989e Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 17:42:36 +0200 Subject: [PATCH 09/21] added giveaway command --- src/commands/giveaway.js | 1 + 1 file changed, 1 insertion(+) diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js index f567a06..9e06040 100644 --- a/src/commands/giveaway.js +++ b/src/commands/giveaway.js @@ -35,6 +35,7 @@ module.exports = { .setStyle('SHORT') .setRequired(false) ]); + await modal.showModal(m, { interaction: interaction, client: interaction.client From 4b4a1a0953e55dd0ddb53a724e20eabb1004317d Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Fri, 20 May 2022 17:48:52 +0200 Subject: [PATCH 10/21] added modalSubmit event --- src/events/modalSubmit.js | 42 ++++++++++++++++++++------------------- 1 file changed, 22 insertions(+), 20 deletions(-) diff --git a/src/events/modalSubmit.js b/src/events/modalSubmit.js index 8caad4a..919e8ee 100644 --- a/src/events/modalSubmit.js +++ b/src/events/modalSubmit.js @@ -3,18 +3,18 @@ const { MessageEmbed, Message } = require('discord.js'); function createGiveawayEmbed(description, timelimit, emoji, image, winner) { let standardEmbedFields = [{ - name: "🎁 Item", + name: '🎁 Item', value: description }, { - name: "➡️ How to enter", - value: "React to this message with " + emoji + name: '➡️ How to enter', + value: 'React to this message with ' + emoji }, { - name: "🏆 Winners", - value: "Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!" + name: '🏆 Winners', + value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' }] - if (winner != "") { + if (winner != '') { let embed = new MessageEmbed() .setColor('GREEN') .addFields( @@ -22,8 +22,8 @@ function createGiveawayEmbed(description, timelimit, emoji, image, winner) { standardEmbedFields[1], standardEmbedFields[2], { - name: "🏁 Giveaway ended", - value: "Winner: " + winner + name: '🏁 Giveaway ended', + value: 'Winner: ' + winner } ) .setImage(image) @@ -38,24 +38,26 @@ function createGiveawayEmbed(description, timelimit, emoji, image, winner) { standardEmbedFields[2] ) .setImage(image) - .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?"hour":"hours"}`}); + .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); return embed; } module.exports = { - name: "modalSubmit", + name: 'modalSubmit', async execute (modal) { try { if (modal.customId == 'giveaway') { - - //const channel = '838711380076396574'; - const channel = '975791268741406800'; + const channel = '838711380076396574'; const gamekey = modal.getTextInputValue('gamekey'); const description = modal.getTextInputValue('description'); let timelimit = modal.getTextInputValue('timelimit'); - const emoji = `${modal.client.emojis.cache.find(emoji => emoji.name == modal.getTextInputValue("emoji"))}`; + if (isNaN(timelimit)) { + await modal.reply({content:'Timelimit has to be integer.', ephemeral: true}); + return; + } + const emoji = `${modal.client.emojis.cache.find(emoji => emoji.name == modal.getTextInputValue('emoji'))}`; if (emoji == 'undefined') { - await modal.reply({content:"Emoji not found. Only custom server-emojis work.", ephemeral:true}); + await modal.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); return; } let image = ''; @@ -63,20 +65,20 @@ module.exports = { image = modal.getTextInputValue('image'); } - let embed = createGiveawayEmbed(description,timelimit,emoji,image,""); + let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); let msg = await modal.client.channels.cache.get(channel).send({embeds: [embed]}); msg.react(emoji); let interval = setInterval(() => { timelimit -= 1; - embed = createGiveawayEmbed(description,timelimit,emoji,image,""); + embed = createGiveawayEmbed(description,timelimit,emoji,image,''); msg.edit({embeds: [embed]}); if (timelimit < 1) { clearInterval(interval); let winner; msg.reactions.cache.map(async (reaction) => { - let emojiName = emoji.split(":")[1]; + let emojiName = emoji.split(':')[1]; if (reaction.emoji.name !== emojiName) return; let usersThatReacted = []; let reactedUsers = await reaction.users.fetch(); @@ -86,7 +88,7 @@ module.exports = { } }); if (usersThatReacted.length == 0) { - winner = "No one reacted! :(" + winner = 'No one reacted! :(' } else { winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); @@ -98,7 +100,7 @@ module.exports = { } }, 60000); - await modal.reply({content: "Done!", ephemeral: true}); + await modal.reply({content: 'Done!', ephemeral: true}); }; } catch (e) { console.log(e); From 924b5d58864bc19c3ec7049846d727acf5e2e14d Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Sun, 22 May 2022 16:44:22 +0200 Subject: [PATCH 11/21] removed discord-modals package --- package-lock.json | 15 --------------- package.json | 1 - 2 files changed, 16 deletions(-) diff --git a/package-lock.json b/package-lock.json index 57fe7ba..d358676 100644 --- a/package-lock.json +++ b/package-lock.json @@ -12,7 +12,6 @@ "@discordjs/builders": "^0.13.0", "@discordjs/rest": "^0.4.1", "discord-api-types": "^0.32.1", - "discord-modals": "^1.3.6", "discord.js": "^13.7.0", "dotenv": "^16.0.1" }, @@ -393,14 +392,6 @@ "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, - "node_modules/discord-modals": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/discord-modals/-/discord-modals-1.3.6.tgz", - "integrity": "sha512-1RorghCu9LFU7qhmxppgz9U498Re9OGvGQ6NaRKwg4X9GxH9Ymj1kQNWu4274vlWL0AbYO2ua9o1kiK7BjD+lg==", - "peerDependencies": { - "discord.js": "*" - } - }, "node_modules/discord.js": { "version": "13.7.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.7.0.tgz", @@ -1534,12 +1525,6 @@ "resolved": "https://registry.npmjs.org/discord-api-types/-/discord-api-types-0.32.1.tgz", "integrity": "sha512-/ewl0CPYT5xjOC+SJ7wADJKjtpZfiiUaYXOP/Ns54lnBcv4Xqa4iKSqRF/w1fjiUvWTYN9W8UuOiyCHtmu5fJw==" }, - "discord-modals": { - "version": "1.3.6", - "resolved": "https://registry.npmjs.org/discord-modals/-/discord-modals-1.3.6.tgz", - "integrity": "sha512-1RorghCu9LFU7qhmxppgz9U498Re9OGvGQ6NaRKwg4X9GxH9Ymj1kQNWu4274vlWL0AbYO2ua9o1kiK7BjD+lg==", - "requires": {} - }, "discord.js": { "version": "13.7.0", "resolved": "https://registry.npmjs.org/discord.js/-/discord.js-13.7.0.tgz", diff --git a/package.json b/package.json index 62fa450..25f48cc 100644 --- a/package.json +++ b/package.json @@ -22,7 +22,6 @@ "@discordjs/builders": "^0.13.0", "@discordjs/rest": "^0.4.1", "discord-api-types": "^0.32.1", - "discord-modals": "^1.3.6", "discord.js": "^13.7.0", "dotenv": "^16.0.1" }, From c91ce49f9fde76d82e2ab6c7d14776c0722a1be2 Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Sun, 22 May 2022 17:07:04 +0200 Subject: [PATCH 12/21] moved interactionCreate to the EventHandler --- index.js | 27 +-------------------------- 1 file changed, 1 insertion(+), 26 deletions(-) diff --git a/index.js b/index.js index dde07f0..90625ae 100644 --- a/index.js +++ b/index.js @@ -3,8 +3,7 @@ const fs = require('fs'); const path = require('path'); const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); -const { Client, Collection, Intents } = require('discord.js'); -const modal = require("discord-modals"); +const { Client, Collection, Intents, MessageEmbed } = require('discord.js'); const token = process.env.TOKEN; const clientId = process.env.CLIENT_ID; @@ -14,8 +13,6 @@ const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES], }); -modal(client); - // Load events from event files. const eventsPath = path.join(__dirname, 'src', 'events'); const eventFiles = fs @@ -66,26 +63,4 @@ client.once('ready', () => { console.log('Ready!'); }); -client.on('interactionCreate', async (interaction) => { - if (!interaction.isCommand()) { - return; - } - - const command = client.commands.get(interaction.commandName); - - if (!command) { - return; - } - - try { - await command.execute(interaction, modal); - } catch (error) { - await interaction.reply({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - console.error(error); - } -}); - client.login(token); From dac932a7d2f1e5b2e3a91e81882dc78cf5b65aef Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Sun, 22 May 2022 17:07:57 +0200 Subject: [PATCH 13/21] giveaway command now uses discord.js's modals --- src/commands/giveaway.js | 86 ++++++++++++++++++++++------------------ 1 file changed, 47 insertions(+), 39 deletions(-) diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js index 9e06040..a88d382 100644 --- a/src/commands/giveaway.js +++ b/src/commands/giveaway.js @@ -1,44 +1,52 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); - +const { Modal, MessageActionRow, TextInputComponent } = require('discord.js'); + module.exports = { data: new SlashCommandBuilder() .setName('giveaway') .setDescription('An admin command for creating giveaways'), - async execute(interaction, modal) { - let m = new modal.Modal() - .setCustomId('giveaway') - .setTitle('Create a giveaway') - .addComponents([ - new modal.TextInputComponent() - .setCustomId('gamekey') - .setLabel('Game key') - .setStyle('SHORT') - .setRequired(true), - new modal.TextInputComponent() - .setCustomId('description') - .setLabel('Description') - .setStyle('SHORT') - .setRequired(true), - new modal.TextInputComponent() - .setCustomId('timelimit') - .setLabel('Time limit (minutes)') - .setStyle('SHORT') - .setRequired(true), - new modal.TextInputComponent() - .setCustomId('emoji') - .setLabel('Emoji to react with (without ::)') - .setStyle('SHORT') - .setRequired(true), - new modal.TextInputComponent() - .setCustomId('image') - .setLabel('Image') - .setStyle('SHORT') - .setRequired(false) - ]); - - await modal.showModal(m, { - interaction: interaction, - client: interaction.client - }) - } -}; + async execute(interaction) { + const modal = new Modal() + .setCustomId('giveaway') + .setTitle('Create a giveaway') + .addComponents([ + new MessageActionRow().addComponents( + new TextInputComponent() + .setCustomId('gameKey') + .setLabel('Game Key') + .setStyle('SHORT') + .setRequired(true), + ), + new MessageActionRow().addComponents( + new TextInputComponent() + .setCustomId('description') + .setLabel('Description') + .setStyle('SHORT') + .setRequired(true), + ), + new MessageActionRow().addComponents( + new TextInputComponent() + .setCustomId('timeLimit') + .setLabel('Time Limit (In Minutes)') + .setStyle('SHORT') + .setRequired(true), + ), + new MessageActionRow().addComponents( + new TextInputComponent() + .setCustomId('emoji') + .setLabel('Emoji (To React With)') + .setStyle('SHORT') + .setRequired(true), + ), + new MessageActionRow().addComponents( + new TextInputComponent() + .setCustomId('imageUrl') + .setLabel('Image URL') + .setStyle('SHORT') + .setRequired(false), + ), + ]); + + await interaction.showModal(modal); + }, +}; \ No newline at end of file From cffaf9497e67abf6af8bb19a943195a13f9f1e8c Mon Sep 17 00:00:00 2001 From: zeltim1 Date: Sun, 22 May 2022 17:08:28 +0200 Subject: [PATCH 14/21] interactionCreate event added --- src/events/interactionCreate.js | 73 ++++++++++++++++++ src/events/modalSubmit.js | 109 --------------------------- src/functions/modalSubmitGiveaway.js | 104 +++++++++++++++++++++++++ 3 files changed, 177 insertions(+), 109 deletions(-) create mode 100644 src/events/interactionCreate.js delete mode 100644 src/events/modalSubmit.js create mode 100644 src/functions/modalSubmitGiveaway.js diff --git a/src/events/interactionCreate.js b/src/events/interactionCreate.js new file mode 100644 index 0000000..5940cec --- /dev/null +++ b/src/events/interactionCreate.js @@ -0,0 +1,73 @@ +const { modalSubmitGiveaway } = require('../functions/modalSubmitGiveaway'); + +module.exports = { + name: 'interactionCreate', + async execute(interaction) { + if (interaction.isModalSubmit()) { + if (interaction.customId == 'giveaway') { + modalSubmitGiveaway(interaction); + } + } + if (!interaction.isCommand()) { + return; + } + + const command = interaction.client.commands.get(interaction.commandName); + + if (!command) { + return; + } + + try { + await command.execute(interaction); + } catch (error) { + await interaction.reply({ + content: 'There was an error while executing this command!', + ephemeral: true, + }); + console.error(error); + } + } +} + + +function createGiveawayEmbed(description, timelimit, emoji, image, winner) { + let standardEmbedFields = [{ + name: '🎁 Item', + value: description + }, + { + name: '➡️ How to enter', + value: 'React to this message with ' + emoji + }, + { + name: '🏆 Winners', + value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' + }] + if (winner != '') { + let embed = new MessageEmbed() + .setColor('GREEN') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + { + name: '🏁 Giveaway ended', + value: 'Winner: ' + winner + } + ) + .setImage(image) + .setFooter({text: `Giveaway ended`}); + return embed; + } + let embed = new MessageEmbed() + .setColor('YELLOW') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2] + ) + .setImage(image) + .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); + return embed; +} \ No newline at end of file diff --git a/src/events/modalSubmit.js b/src/events/modalSubmit.js deleted file mode 100644 index 919e8ee..0000000 --- a/src/events/modalSubmit.js +++ /dev/null @@ -1,109 +0,0 @@ -const { time } = require('@discordjs/builders'); -const { MessageEmbed, Message } = require('discord.js'); - -function createGiveawayEmbed(description, timelimit, emoji, image, winner) { - let standardEmbedFields = [{ - name: '🎁 Item', - value: description - }, - { - name: '➡️ How to enter', - value: 'React to this message with ' + emoji - }, - { - name: '🏆 Winners', - value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' - }] - if (winner != '') { - let embed = new MessageEmbed() - .setColor('GREEN') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2], - { - name: '🏁 Giveaway ended', - value: 'Winner: ' + winner - } - ) - .setImage(image) - .setFooter({text: `Giveaway ended`}); - return embed; - } - let embed = new MessageEmbed() - .setColor('YELLOW') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2] - ) - .setImage(image) - .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); - return embed; -} - -module.exports = { - name: 'modalSubmit', - async execute (modal) { - try { - if (modal.customId == 'giveaway') { - const channel = '838711380076396574'; - const gamekey = modal.getTextInputValue('gamekey'); - const description = modal.getTextInputValue('description'); - let timelimit = modal.getTextInputValue('timelimit'); - if (isNaN(timelimit)) { - await modal.reply({content:'Timelimit has to be integer.', ephemeral: true}); - return; - } - const emoji = `${modal.client.emojis.cache.find(emoji => emoji.name == modal.getTextInputValue('emoji'))}`; - if (emoji == 'undefined') { - await modal.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); - return; - } - let image = ''; - if (modal.getTextInputValue('image') != null) { - image = modal.getTextInputValue('image'); - } - - let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - - let msg = await modal.client.channels.cache.get(channel).send({embeds: [embed]}); - msg.react(emoji); - - let interval = setInterval(() => { - timelimit -= 1; - embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - msg.edit({embeds: [embed]}); - if (timelimit < 1) { - clearInterval(interval); - let winner; - msg.reactions.cache.map(async (reaction) => { - let emojiName = emoji.split(':')[1]; - if (reaction.emoji.name !== emojiName) return; - let usersThatReacted = []; - let reactedUsers = await reaction.users.fetch(); - reactedUsers.map((user) => { - if (user != msg.author) { - usersThatReacted.push(user); - } - }); - if (usersThatReacted.length == 0) { - winner = 'No one reacted! :(' - } else { - winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; - winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); - } - - embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); - msg.edit({embeds: [embed]}); - }); - - } - }, 60000); - await modal.reply({content: 'Done!', ephemeral: true}); - }; - } catch (e) { - console.log(e); - } - } -} diff --git a/src/functions/modalSubmitGiveaway.js b/src/functions/modalSubmitGiveaway.js new file mode 100644 index 0000000..afc89a3 --- /dev/null +++ b/src/functions/modalSubmitGiveaway.js @@ -0,0 +1,104 @@ +const { MessageEmbed } = require('discord.js'); + +function createGiveawayEmbed(description, timelimit, emoji, image, winner) { + let standardEmbedFields = [{ + name: '🎁 Item', + value: description + }, + { + name: '➡️ How to enter', + value: 'React to this message with ' + emoji + }, + { + name: '🏆 Winners', + value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' + }] + if (winner != '') { + let embed = new MessageEmbed() + .setColor('GREEN') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + { + name: '🏁 Giveaway ended', + value: 'Winner: ' + winner + } + ) + .setImage(image) + .setFooter({text: `Giveaway ended`}); + return embed; + } + let embed = new MessageEmbed() + .setColor('YELLOW') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2] + ) + .setImage(image) + .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); + return embed; +}; + +async function modalSubmitGiveaway (interaction) { + //const channel = '838711380076396574'; + const channel = '975791268741406800'; + const gamekey = interaction.fields.getTextInputValue('gameKey'); + const description = interaction.fields.getTextInputValue('description'); + let timelimit = interaction.fields.getTextInputValue('timeLimit'); + if (isNaN(timelimit)) { + await interaction.reply({content:'Timelimit has to be integer.', ephemeral: true}); + return; + } + const emoji = `${interaction.client.emojis.cache.find(emoji => emoji.name == interaction.fields.getTextInputValue('emoji'))}`; + if (emoji == 'undefined') { + await interaction.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); + return; + } + let image = ''; + if (interaction.fields.getTextInputValue('imageUrl') != null) { + image = interaction.fields.getTextInputValue('imageUrl'); + } + + let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); + + let msg = await interaction.client.channels.cache.get(channel).send({embeds: [embed]}); + msg.react(emoji); + + let interval = setInterval(() => { + timelimit -= 1; + embed = createGiveawayEmbed(description,timelimit,emoji,image,''); + msg.edit({embeds: [embed]}); + if (timelimit < 1) { + clearInterval(interval); + let winner; + msg.reactions.cache.map(async (reaction) => { + let emojiName = emoji.split(':')[1]; + if (reaction.emoji.name !== emojiName) return; + let usersThatReacted = []; + let reactedUsers = await reaction.users.fetch(); + reactedUsers.map((user) => { + if (user != msg.author) { + usersThatReacted.push(user); + } + }); + if (usersThatReacted.length == 0) { + winner = 'No one reacted! :(' + } else { + winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; + winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); + } + + embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); + msg.edit({embeds: [embed]}); + }); + + } + }, 60000); + await interaction.reply({content: 'Giveaway created.', ephemeral: true}); +}; + +module.exports = { + modalSubmitGiveaway +}; \ No newline at end of file From d9b89f75cb0107ab0bac06e45ac8f6b74fec3e9a Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sun, 22 May 2022 17:16:37 +0200 Subject: [PATCH 15/21] Delete src/functions directory --- src/functions/modalSubmitGiveaway.js | 104 --------------------------- 1 file changed, 104 deletions(-) delete mode 100644 src/functions/modalSubmitGiveaway.js diff --git a/src/functions/modalSubmitGiveaway.js b/src/functions/modalSubmitGiveaway.js deleted file mode 100644 index afc89a3..0000000 --- a/src/functions/modalSubmitGiveaway.js +++ /dev/null @@ -1,104 +0,0 @@ -const { MessageEmbed } = require('discord.js'); - -function createGiveawayEmbed(description, timelimit, emoji, image, winner) { - let standardEmbedFields = [{ - name: '🎁 Item', - value: description - }, - { - name: '➡️ How to enter', - value: 'React to this message with ' + emoji - }, - { - name: '🏆 Winners', - value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' - }] - if (winner != '') { - let embed = new MessageEmbed() - .setColor('GREEN') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2], - { - name: '🏁 Giveaway ended', - value: 'Winner: ' + winner - } - ) - .setImage(image) - .setFooter({text: `Giveaway ended`}); - return embed; - } - let embed = new MessageEmbed() - .setColor('YELLOW') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2] - ) - .setImage(image) - .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); - return embed; -}; - -async function modalSubmitGiveaway (interaction) { - //const channel = '838711380076396574'; - const channel = '975791268741406800'; - const gamekey = interaction.fields.getTextInputValue('gameKey'); - const description = interaction.fields.getTextInputValue('description'); - let timelimit = interaction.fields.getTextInputValue('timeLimit'); - if (isNaN(timelimit)) { - await interaction.reply({content:'Timelimit has to be integer.', ephemeral: true}); - return; - } - const emoji = `${interaction.client.emojis.cache.find(emoji => emoji.name == interaction.fields.getTextInputValue('emoji'))}`; - if (emoji == 'undefined') { - await interaction.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); - return; - } - let image = ''; - if (interaction.fields.getTextInputValue('imageUrl') != null) { - image = interaction.fields.getTextInputValue('imageUrl'); - } - - let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - - let msg = await interaction.client.channels.cache.get(channel).send({embeds: [embed]}); - msg.react(emoji); - - let interval = setInterval(() => { - timelimit -= 1; - embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - msg.edit({embeds: [embed]}); - if (timelimit < 1) { - clearInterval(interval); - let winner; - msg.reactions.cache.map(async (reaction) => { - let emojiName = emoji.split(':')[1]; - if (reaction.emoji.name !== emojiName) return; - let usersThatReacted = []; - let reactedUsers = await reaction.users.fetch(); - reactedUsers.map((user) => { - if (user != msg.author) { - usersThatReacted.push(user); - } - }); - if (usersThatReacted.length == 0) { - winner = 'No one reacted! :(' - } else { - winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; - winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); - } - - embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); - msg.edit({embeds: [embed]}); - }); - - } - }, 60000); - await interaction.reply({content: 'Giveaway created.', ephemeral: true}); -}; - -module.exports = { - modalSubmitGiveaway -}; \ No newline at end of file From fba9468c40ec6e06835a60ba35dc87b4d2aaec09 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sun, 22 May 2022 17:17:05 +0200 Subject: [PATCH 16/21] moved modalSubmit to functions folder --- src/functions/modalSubmitGiveaway.js | 104 +++++++++++++++++++++++++++ 1 file changed, 104 insertions(+) create mode 100644 src/functions/modalSubmitGiveaway.js diff --git a/src/functions/modalSubmitGiveaway.js b/src/functions/modalSubmitGiveaway.js new file mode 100644 index 0000000..0d48058 --- /dev/null +++ b/src/functions/modalSubmitGiveaway.js @@ -0,0 +1,104 @@ +const { MessageEmbed } = require('discord.js'); + +function createGiveawayEmbed(description, timelimit, emoji, image, winner) { + let standardEmbedFields = [{ + name: '🎁 Item', + value: description + }, + { + name: '➡️ How to enter', + value: 'React to this message with ' + emoji + }, + { + name: '🏆 Winners', + value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' + }] + if (winner != '') { + let embed = new MessageEmbed() + .setColor('GREEN') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + { + name: '🏁 Giveaway ended', + value: 'Winner: ' + winner + } + ) + .setImage(image) + .setFooter({text: `Giveaway ended`}); + return embed; + } + let embed = new MessageEmbed() + .setColor('YELLOW') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2] + ) + .setImage(image) + .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); + return embed; +}; + +async function modalSubmitGiveaway (interaction) { + //const channel = '838711380076396574'; + const channel = '975791268741406800'; + const gamekey = interaction.fields.getTextInputValue('gameKey'); + const description = interaction.fields.getTextInputValue('description'); + let timelimit = interaction.fields.getTextInputValue('timeLimit'); + if (isNaN(timelimit)) { + await interaction.reply({content:'Timelimit has to be integer.', ephemeral: true}); + return; + } + const emoji = `${interaction.client.emojis.cache.find(emoji => emoji.name == interaction.fields.getTextInputValue('emoji'))}`; + if (emoji == 'undefined') { + await interaction.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); + return; + } + let image = ''; + if (interaction.fields.getTextInputValue('imageUrl') != null) { + image = interaction.fields.getTextInputValue('imageUrl'); + } + + let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); + + let msg = await interaction.client.channels.cache.get(channel).send({embeds: [embed]}); + msg.react(emoji); + + let interval = setInterval(() => { + timelimit -= 1; + embed = createGiveawayEmbed(description,timelimit,emoji,image,''); + msg.edit({embeds: [embed]}); + if (timelimit < 1) { + clearInterval(interval); + let winner; + msg.reactions.cache.map(async (reaction) => { + let emojiName = emoji.split(':')[1]; + if (reaction.emoji.name !== emojiName) return; + let usersThatReacted = []; + let reactedUsers = await reaction.users.fetch(); + reactedUsers.map((user) => { + if (user != msg.author) { + usersThatReacted.push(user); + } + }); + if (usersThatReacted.length == 0) { + winner = 'No one reacted! :(' + } else { + winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; + winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); + } + + embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); + msg.edit({embeds: [embed]}); + }); + + } + }, 60000); + await interaction.reply({content: 'Giveaway created.', ephemeral: true}); +}; + +module.exports = { + modalSubmitGiveaway +}; From 057c744dd440d93d58a4d62412522b9f37fcd489 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sat, 28 May 2022 15:50:08 +0200 Subject: [PATCH 17/21] Delete src/functions directory --- src/functions/modalSubmitGiveaway.js | 104 --------------------------- 1 file changed, 104 deletions(-) delete mode 100644 src/functions/modalSubmitGiveaway.js diff --git a/src/functions/modalSubmitGiveaway.js b/src/functions/modalSubmitGiveaway.js deleted file mode 100644 index 0d48058..0000000 --- a/src/functions/modalSubmitGiveaway.js +++ /dev/null @@ -1,104 +0,0 @@ -const { MessageEmbed } = require('discord.js'); - -function createGiveawayEmbed(description, timelimit, emoji, image, winner) { - let standardEmbedFields = [{ - name: '🎁 Item', - value: description - }, - { - name: '➡️ How to enter', - value: 'React to this message with ' + emoji - }, - { - name: '🏆 Winners', - value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' - }] - if (winner != '') { - let embed = new MessageEmbed() - .setColor('GREEN') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2], - { - name: '🏁 Giveaway ended', - value: 'Winner: ' + winner - } - ) - .setImage(image) - .setFooter({text: `Giveaway ended`}); - return embed; - } - let embed = new MessageEmbed() - .setColor('YELLOW') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2] - ) - .setImage(image) - .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); - return embed; -}; - -async function modalSubmitGiveaway (interaction) { - //const channel = '838711380076396574'; - const channel = '975791268741406800'; - const gamekey = interaction.fields.getTextInputValue('gameKey'); - const description = interaction.fields.getTextInputValue('description'); - let timelimit = interaction.fields.getTextInputValue('timeLimit'); - if (isNaN(timelimit)) { - await interaction.reply({content:'Timelimit has to be integer.', ephemeral: true}); - return; - } - const emoji = `${interaction.client.emojis.cache.find(emoji => emoji.name == interaction.fields.getTextInputValue('emoji'))}`; - if (emoji == 'undefined') { - await interaction.reply({content:'Emoji not found. Only custom server-emojis work.', ephemeral:true}); - return; - } - let image = ''; - if (interaction.fields.getTextInputValue('imageUrl') != null) { - image = interaction.fields.getTextInputValue('imageUrl'); - } - - let embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - - let msg = await interaction.client.channels.cache.get(channel).send({embeds: [embed]}); - msg.react(emoji); - - let interval = setInterval(() => { - timelimit -= 1; - embed = createGiveawayEmbed(description,timelimit,emoji,image,''); - msg.edit({embeds: [embed]}); - if (timelimit < 1) { - clearInterval(interval); - let winner; - msg.reactions.cache.map(async (reaction) => { - let emojiName = emoji.split(':')[1]; - if (reaction.emoji.name !== emojiName) return; - let usersThatReacted = []; - let reactedUsers = await reaction.users.fetch(); - reactedUsers.map((user) => { - if (user != msg.author) { - usersThatReacted.push(user); - } - }); - if (usersThatReacted.length == 0) { - winner = 'No one reacted! :(' - } else { - winner = usersThatReacted[Math.floor(Math.random()*usersThatReacted.length)]; - winner.send(`Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`); - } - - embed = createGiveawayEmbed(description,timelimit,emoji,image,winner.toString()); - msg.edit({embeds: [embed]}); - }); - - } - }, 60000); - await interaction.reply({content: 'Giveaway created.', ephemeral: true}); -}; - -module.exports = { - modalSubmitGiveaway -}; From 35849fde1bfb499d153e4af70f3c56102962d7c2 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sat, 28 May 2022 15:52:02 +0200 Subject: [PATCH 18/21] changed emoji label --- src/commands/giveaway.js | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/src/commands/giveaway.js b/src/commands/giveaway.js index a88d382..bdbc368 100644 --- a/src/commands/giveaway.js +++ b/src/commands/giveaway.js @@ -1,6 +1,6 @@ const { SlashCommandBuilder } = require('@discordjs/builders'); const { Modal, MessageActionRow, TextInputComponent } = require('discord.js'); - + module.exports = { data: new SlashCommandBuilder() .setName('giveaway') @@ -34,7 +34,7 @@ module.exports = { new MessageActionRow().addComponents( new TextInputComponent() .setCustomId('emoji') - .setLabel('Emoji (To React With)') + .setLabel('Emoji (To React With, without ::)') .setStyle('SHORT') .setRequired(true), ), @@ -46,7 +46,7 @@ module.exports = { .setRequired(false), ), ]); - + await interaction.showModal(modal); }, -}; \ No newline at end of file +}; From ee11f33f1dcdd6ac50269286d745ba5731e7d116 Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sat, 28 May 2022 15:52:28 +0200 Subject: [PATCH 19/21] moved interactionCreate back to index.js --- index.js | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/index.js b/index.js index 90625ae..8f30275 100644 --- a/index.js +++ b/index.js @@ -4,6 +4,7 @@ const path = require('path'); const { REST } = require('@discordjs/rest'); const { Routes } = require('discord-api-types/v9'); const { Client, Collection, Intents, MessageEmbed } = require('discord.js'); +const { modalSubmitGiveaway } = require('./src/events/giveaway.js'); const token = process.env.TOKEN; const clientId = process.env.CLIENT_ID; @@ -59,6 +60,34 @@ const rest = new REST({ version: '9' }).setToken(token); } })(); +client.on('interactionCreate', async (interaction) => { + if (interaction.isModalSubmit()) { + if (interaction.customId == 'giveaway') { + modalSubmitGiveaway(interaction); + } + } + + if (!interaction.isCommand()) { + return; + } + + const command = client.commands.get(interaction.commandName); + + if (!command) { + return; + } + + try { + await command.execute(interaction); + } catch (error) { + await interaction.reply({ + content: 'There was an error while executing this command!', + ephemeral: true, + }); + console.error(error); + } +}); + client.once('ready', () => { console.log('Ready!'); }); From 6c914a718248325c0fbd8455b0e4d953b5f2894b Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sat, 28 May 2022 15:53:32 +0200 Subject: [PATCH 20/21] renamed interactionCreate --- src/events/giveaway.js | 130 ++++++++++++++++++++++++++++++++ src/events/interactionCreate.js | 73 ------------------ 2 files changed, 130 insertions(+), 73 deletions(-) create mode 100644 src/events/giveaway.js delete mode 100644 src/events/interactionCreate.js diff --git a/src/events/giveaway.js b/src/events/giveaway.js new file mode 100644 index 0000000..cf42e23 --- /dev/null +++ b/src/events/giveaway.js @@ -0,0 +1,130 @@ +const { MessageEmbed } = require('discord.js'); + +function createGiveawayEmbed(description, timelimit, emoji, image, winner) { + let standardEmbedFields = [ + { + name: '🎁 Item', + value: description, + }, + { + name: '➡️ How to enter', + value: 'React to this message with ' + emoji, + }, + { + name: '🏆 Winners', + value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!', + }, + ]; + if (winner != '') { + let embed = new MessageEmbed() + .setColor('GREEN') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + { + name: '🏁 Giveaway ended', + value: 'Winner: ' + winner, + }, + ) + .setImage(image) + .setFooter({ text: `Giveaway ended` }); + return embed; + } + let embed = new MessageEmbed() + .setColor('YELLOW') + .addFields( + standardEmbedFields[0], + standardEmbedFields[1], + standardEmbedFields[2], + ) + .setImage(image) + .setFooter({ + text: `Ending in ${Math.ceil(timelimit / 60)} ${ + Math.ceil(timelimit / 60) < 2 ? 'hour' : 'hours' + }`, + }); + return embed; +} + +async function modalSubmitGiveaway(interaction) { + //const channel = '838711380076396574'; + const channel = process.env.CHANNEL; + const gamekey = interaction.fields.getTextInputValue('gameKey'); + const description = interaction.fields.getTextInputValue('description'); + let timelimit = interaction.fields.getTextInputValue('timeLimit'); + if (isNaN(timelimit)) { + await interaction.reply({ + content: 'Timelimit has to be integer.', + ephemeral: true, + }); + return; + } + const emoji = `${interaction.client.emojis.cache.find( + (emoji) => emoji.name == interaction.fields.getTextInputValue('emoji'), + )}`; + if (emoji == 'undefined') { + await interaction.reply({ + content: 'Emoji not found. Only custom server-emojis work.', + ephemeral: true, + }); + return; + } + let image = ''; + if (interaction.fields.getTextInputValue('imageUrl') != null) { + image = interaction.fields.getTextInputValue('imageUrl'); + } + + let embed = createGiveawayEmbed(description, timelimit, emoji, image, ''); + + let msg = await interaction.client.channels.cache + .get(channel) + .send({ embeds: [embed] }); + msg.react(emoji); + + let interval = setInterval(() => { + timelimit -= 1; + embed = createGiveawayEmbed(description, timelimit, emoji, image, ''); + msg.edit({ embeds: [embed] }); + if (timelimit < 1) { + clearInterval(interval); + let winner; + msg.reactions.cache.map(async (reaction) => { + let emojiName = emoji.split(':')[1]; + if (reaction.emoji.name !== emojiName) return; + let usersThatReacted = []; + let reactedUsers = await reaction.users.fetch(); + reactedUsers.map((user) => { + if (user != msg.author) { + usersThatReacted.push(user); + } + }); + if (usersThatReacted.length == 0) { + winner = 'No one reacted! :('; + } else { + winner = + usersThatReacted[ + Math.floor(Math.random() * usersThatReacted.length) + ]; + winner.send( + `Congratulations! You won the giveaway for ${description}. \nYour Game key: ${gamekey}`, + ); + } + + embed = createGiveawayEmbed( + description, + timelimit, + emoji, + image, + winner.toString(), + ); + msg.edit({ embeds: [embed] }); + }); + } + }, 60000); + await interaction.reply({ content: 'Giveaway created.', ephemeral: true }); +} + +module.exports = { + modalSubmitGiveaway, +}; diff --git a/src/events/interactionCreate.js b/src/events/interactionCreate.js deleted file mode 100644 index 5940cec..0000000 --- a/src/events/interactionCreate.js +++ /dev/null @@ -1,73 +0,0 @@ -const { modalSubmitGiveaway } = require('../functions/modalSubmitGiveaway'); - -module.exports = { - name: 'interactionCreate', - async execute(interaction) { - if (interaction.isModalSubmit()) { - if (interaction.customId == 'giveaway') { - modalSubmitGiveaway(interaction); - } - } - if (!interaction.isCommand()) { - return; - } - - const command = interaction.client.commands.get(interaction.commandName); - - if (!command) { - return; - } - - try { - await command.execute(interaction); - } catch (error) { - await interaction.reply({ - content: 'There was an error while executing this command!', - ephemeral: true, - }); - console.error(error); - } - } -} - - -function createGiveawayEmbed(description, timelimit, emoji, image, winner) { - let standardEmbedFields = [{ - name: '🎁 Item', - value: description - }, - { - name: '➡️ How to enter', - value: 'React to this message with ' + emoji - }, - { - name: '🏆 Winners', - value: 'Winners are picked at random, and prizes are sent automatically through a direct message here on Discord when the giveaway concludes. Make sure you allow direct messages from other members!' - }] - if (winner != '') { - let embed = new MessageEmbed() - .setColor('GREEN') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2], - { - name: '🏁 Giveaway ended', - value: 'Winner: ' + winner - } - ) - .setImage(image) - .setFooter({text: `Giveaway ended`}); - return embed; - } - let embed = new MessageEmbed() - .setColor('YELLOW') - .addFields( - standardEmbedFields[0], - standardEmbedFields[1], - standardEmbedFields[2] - ) - .setImage(image) - .setFooter({text:`Ending in ${Math.ceil(timelimit/60)} ${Math.ceil(timelimit/60) < 2?'hour':'hours'}`}); - return embed; -} \ No newline at end of file From ae20f2e49e3695ef7e045a87764ab8a2cb447cca Mon Sep 17 00:00:00 2001 From: zeltim1 <105654247+zeltim1@users.noreply.github.com> Date: Sat, 28 May 2022 15:54:09 +0200 Subject: [PATCH 21/21] added CHANNEL to .env.example --- .env.example | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/.env.example b/.env.example index 063b7f2..9362bc7 100644 --- a/.env.example +++ b/.env.example @@ -2,4 +2,5 @@ TOKEN= CLIENT_ID= GUILD_ID= SOURCEJUMP_API_KEY= -SOURCEJUMP_API_URL= \ No newline at end of file +SOURCEJUMP_API_URL= +CHANNEL