-
Notifications
You must be signed in to change notification settings - Fork 7
Add giveaway command/event #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
zeltim1
wants to merge
22
commits into
sourcejump:main
Choose a base branch
from
zeltim1:main
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 17 commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
728d654
installed discord-modals
zeltim1 dee010b
added giveaway command
zeltim1 dee5e4b
modalSubmit event
zeltim1 87fadd7
added giveaway command
zeltim1 5887b03
added modalSubmit event
zeltim1 61f5680
required discord-modals and set modal(client)
zeltim1 0db0b12
moved require(discord-modals) to index.js
zeltim1 e0c0a8d
added modalSubmit event
zeltim1 1527898
Merge branch 'main' of https://github.com/zeltim1/discord-bot
zeltim1 727cf9e
added giveaway command
zeltim1 4b4a1a0
added modalSubmit event
zeltim1 924b5d5
removed discord-modals package
zeltim1 c91ce49
moved interactionCreate to the EventHandler
zeltim1 dac932a
giveaway command now uses discord.js's modals
zeltim1 cffaf94
interactionCreate event added
zeltim1 d9b89f7
Delete src/functions directory
zeltim1 fba9468
moved modalSubmit to functions folder
zeltim1 057c744
Delete src/functions directory
zeltim1 35849fd
changed emoji label
zeltim1 ee11f33
moved interactionCreate back to index.js
zeltim1 6c914a7
renamed interactionCreate
zeltim1 ae20f2e
added CHANNEL to .env.example
zeltim1 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +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) { | ||
| 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); | ||
| }, | ||
| }; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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; | ||
| } | ||
ecsr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -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'; | ||
ecsr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| 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 | ||
| }; | ||
ecsr marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.