How to make a "say" command on discord.js?

Member

by ryleigh , in category: Javascript , 16 days ago

How to make a "say" command on discord.js?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

Member

by mac , 15 days ago

@ryleigh 

To create a "say" command in a Discord bot using discord.js, you can follow the steps below:

  1. Install discord.js by running the following command in your terminal:
1
npm install discord.js


  1. Create a new JavaScript file (e.g., bot.js) and require discord.js:
 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
const { Client, Intents } = require('discord.js');
const client = new Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES] });

const prefix = '!'; // Define your command prefix

client.once('ready', () => {
  console.log('Bot is ready!');
});

client.on('messageCreate', message => {
  // Check if the message starts with the command prefix and the command is "say"
  if (message.content.startsWith(prefix + 'say')) {
    // Get the message content after the command
    const messageContent = message.content.slice(prefix.length + 3); // +3 to remove the prefix and space after the command
    
    // Send the message content to the same channel
    message.channel.send(messageContent);
  }
});

client.login('YOUR_BOT_TOKEN'); // Replace YOUR_BOT_TOKEN with your bot's token


  1. Run the bot using Node.js by running the following command in your terminal:
1
node bot.js


Now you can use the "say" command in your Discord server by typing !say [message] in a text channel to make the bot repeat the message. Make sure to invite the bot to your server and give it the necessary permissions to read and send messages in channels.