-
Couldn't load subscription status.
- Fork 9
V14 update #1
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
Merged
Merged
V14 update #1
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
64561db
Added where to find developer mode toggle, removed uneccessary steps …
NwE0kmCE 0372ca6
Updated examples to discord.js v14, added documentation
NwE0kmCE 80d0d57
finishing the example and expanding README
shiffman 88e2894
es6 update
shiffman 6dede93
fix ephemeral
shiffman 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
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,4 @@ | ||
| SERVERID=1234 | ||
| CLIENTID=1234 | ||
| TOKEN=abcd1234 | ||
| TENORKEY=abcd2345 |
This file was deleted.
Oops, something went wrong.
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,31 @@ | ||
| // Import the necessary discord.js classes using ES6 syntax | ||
| import { Client, Events, GatewayIntentBits } from 'discord.js'; | ||
| import dotenv from 'dotenv'; | ||
| import * as choochoo from './commands/choochoo.js'; | ||
| import * as gif from './commands/gif.js'; | ||
|
|
||
| dotenv.config(); | ||
|
|
||
| // Create a new client instance | ||
| const client = new Client({ | ||
| intents: [GatewayIntentBits.Guilds], | ||
| }); | ||
|
|
||
| // When the client is ready, run this code (only once) | ||
| client.once(Events.ClientReady, readyDiscord); | ||
|
|
||
| // Login to Discord with your client's token | ||
| client.login(process.env.TOKEN); | ||
|
|
||
| function readyDiscord() { | ||
| console.log('💖'); | ||
| } | ||
|
|
||
| client.on('interactionCreate', async (interaction) => { | ||
| if (!interaction.isCommand()) return; | ||
| if (interaction.commandName === 'choochoo') { | ||
| await choochoo.execute(interaction); | ||
| } else if (interaction.commandName === 'gif') { | ||
| await gif.execute(interaction); | ||
| } | ||
| }); |
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,14 @@ | ||
| // Importing modules using ES6 syntax | ||
| import { SlashCommandBuilder } from 'discord.js'; | ||
|
|
||
| // Replies array | ||
| const replies = ['🚂🌈💖', 'Choo choo!', 'Ding! 🛎', 'Never forget this dot!']; | ||
|
|
||
| // Command Builder export | ||
| export const data = new SlashCommandBuilder().setName('choochoo').setDescription('Replies with a random phrase!'); | ||
|
|
||
| // Execute function export | ||
| export async function execute(interaction) { | ||
| const index = Math.floor(Math.random() * replies.length); | ||
| await interaction.reply(replies[index]); | ||
| } |
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,39 @@ | ||
| // Importing modules using ES6 syntax | ||
| import fetch from 'node-fetch'; | ||
| import { SlashCommandBuilder, EmbedBuilder } from 'discord.js'; | ||
|
|
||
| export const data = new SlashCommandBuilder() | ||
| .setName('gif') | ||
| .setDescription('Searches Tenor for gifs!') | ||
| .addStringOption((option) => option.setName('keywords').setDescription('The keywords to search Tenor with')); | ||
|
|
||
| // Execute function to interact with Tenor API and reply with a GIF | ||
| export async function execute(interaction) { | ||
| // Default keyword set to 'kitten' if none provided | ||
| let defaultKeyword = 'kitten'; | ||
| const keywords = interaction.options.getString('keywords') ?? defaultKeyword; | ||
|
|
||
| // URL constructed with the provided or default keyword | ||
| let url = `https://tenor.googleapis.com/v2/search?q=${keywords}&key=${process.env.TENORKEY}&client_key=a2z_discord_bot&contentfilter=high`; | ||
|
|
||
| // Fetching data from Tenor API | ||
| let response = await fetch(url); | ||
| let json = await response.json(); | ||
| console.log(json); | ||
| console.log(json.results[0].media_formats); | ||
|
|
||
| // Randomly select a GIF from the response | ||
| const index = Math.floor(Math.random() * json.results.length); | ||
|
|
||
| // Creating an embed to display the GIF in the Discord message | ||
| const embed = new EmbedBuilder().setImage(json.results[index].media_formats.gif.url); | ||
|
|
||
| // Initially acknowledging the command interaction with a hidden (ephemeral) reply | ||
| await interaction.deferReply({ ephemeral: false }); | ||
|
|
||
| // Following up with the selected GIF embedded in the message | ||
| await interaction.followUp({ | ||
| embeds: [embed], | ||
| content: 'GIF from Tenor: ' + keywords, | ||
| }); | ||
| } | ||
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 |
|---|---|---|
| @@ -1,25 +1,42 @@ | ||
| require('dotenv').config(); | ||
|
|
||
| const { SlashCommandBuilder } = require('@discordjs/builders'); | ||
| const { REST } = require('@discordjs/rest'); | ||
| const { Routes } = require('discord-api-types/v9'); | ||
|
|
||
| // Note to self: define options for command arguments | ||
| const commands = [ | ||
| new SlashCommandBuilder().setName('choochoo').setDescription('Replies with a random message!'), | ||
| new SlashCommandBuilder() | ||
| .setName('gif') | ||
| .setDescription('Replies with a gif!') | ||
| .addStringOption((option) => | ||
| option.setName('keywords').setDescription('The gif to search for').setRequired(false) | ||
| ), | ||
| ].map((command) => command.toJSON()); | ||
|
|
||
| const rest = new REST({ version: '9' }).setToken(process.env.TOKEN); | ||
|
|
||
| rest | ||
| .put(Routes.applicationGuildCommands(process.env.CLIENTID, process.env.SERVERID), { | ||
| body: commands, | ||
| }) | ||
| .then(() => console.log('Successfully registered application commands.')) | ||
| .catch(console.error); | ||
| // Slash Commands Deployment Script | ||
| // https://discordjs.guide/creating-your-bot/command-deployment.html#guild-commands/ | ||
|
|
||
| // Importing modules using ES6 syntax | ||
| import { REST, Routes } from 'discord.js'; | ||
| import { config } from 'dotenv'; | ||
| import fs from 'node:fs'; | ||
|
|
||
| config(); // Using dotenv config function directly | ||
|
|
||
| const commands = []; | ||
| const commandFiles = fs.readdirSync('./commands').filter((file) => file.endsWith('.js')); | ||
|
|
||
| // Grab the SlashCommandBuilder#toJSON() output of each command's data for deployment | ||
| for (const file of commandFiles) { | ||
| const command = await import(`./commands/${file}`); // Using dynamic import | ||
| if ('data' in command && 'execute' in command) { | ||
| commands.push(command.data.toJSON()); | ||
| } else { | ||
| console.log(`[WARNING] The command ${file} is missing a required "data" or "execute" property.`); | ||
| } | ||
| } | ||
|
|
||
| // Construct and prepare an insance of the REST module | ||
| const rest = new REST().setToken(process.env.TOKEN); | ||
|
|
||
| // and deploy your commands! | ||
| (async () => { | ||
| try { | ||
| console.log(`Started refreshing ${commands.length} application (/) commands.`); | ||
|
|
||
| // The put method is used to fully refresh all commands in the guild with the current set | ||
| const data = await rest.put(Routes.applicationGuildCommands(process.env.CLIENTID, process.env.SERVERID), { | ||
| body: commands, | ||
| }); | ||
|
|
||
| console.log(`Successfully reloaded ${data.length} application (/) commands.`); | ||
| } catch (error) { | ||
| // And of course, make sure you catch and log any errors! | ||
| console.error(error); | ||
| } | ||
| })(); |
This file was deleted.
Oops, something went wrong.
This file was deleted.
Oops, something went wrong.
Oops, something went wrong.
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I totally forgot to mention but fetch is now natively supported in node. There isn't a need to actually install the package anymore. Since v18.18.0 it is implemented, see https://nodejs.org/dist/latest-v18.x/docs/api/globals.html for detailed information. This removes the need for this dependency.