Skip to content
Advertisement

Discord.js v13 Announce an Annoucement to a certain Channel

Expected

I want to make a command that sends a certain message, given from a user (an admin) to a specific channel.

What is going on

I tried to send a reply to the user who invoked the command with the args they entered.

Secondly, I made it so, that it joins every arg into an array and then replies it.

How can I put the spaces and the dots just like my message?

For example, if I put !announce hi hi hi, the output will be hihihi

If I set spaces between my args, !announce Hello World!, it will reply with Hello World ! and not Hello World!

Resulting code:

module.exports = {
    name: 'announce',
    description: 'Just a test command',
    aliases: [],
    usage: '[prefix] + announce + channel',
    guildOnly: false,
    args: false,
    permissions: {
        bot: [],
        user: [],
    },
    execute: (message, args, client) => {

        var out = [];
        for (let i = 0; i < args.length; i++) {
          out += args[i];
        }
        console.log(out);
        message.reply(out);
    },
};

P.S.: It detects neither the admin user nor the channel to send to.

Another way around it

Another way I found to get a text is through codeblocks, but how can I read its data as an arg?

Advertisement

Answer

If I can understand correctly, you want a command like !announce #announcements Some content to send in another channel that sends Some content to send in another channel in the channel mentioned (#announcements).

You can use message.mentions.channels to check if there is a channel mentioned. It returns a collection of channels, so you can grab the .first() one. If there is none, you can send an error message.

Another error is the way you tried to join the rest of the args. First, you’ll need to remove the first argument, that’s the channel name. You can use .slice(1) for this. Once the first item is removed, you can join() the rest of the items by a single space.

You mentioned that if you join the args by a space, you’ll receive Hello World ! and not Hello World!. You’ll need to make sure that you split the content by spaces only. Something like this:

const args = message.content.slice(prefix.length).split(/ +/);

Here is your code:

module.exports = {
  name: 'announce',
  description: 'Just a test command',
  aliases: [],
  usage: '[prefix] + announce + channel',
  guildOnly: false,
  args: false,
  permissions: {
    bot: [],
    user: [],
  },
  execute: (message, args, client) => {
    // check if there is a channel mentioned
    let channel = message.mentions.channels?.first();

    if (!channel)
      return message.reply('No channel mentioned');

    let content = args
      // remove the first item in args as that's the channel
      .slice(1)
      // join the items by a single space
      .join(' ');

    // send the content to the mentioned channel
    channel.send(content);

    message.reply(`Your message is sent in ${channel}`);
  },
};
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement