简体   繁体   中英

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);
    },
};

PS: 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?

Using a for loop is not necessary here. You can get the content of the message by using slice() on your args .

Since your command usage looks like this: [prefix] + announce + channel , I decided not to use message.mentions.channels?.first(); since the member may mention a channel anywhere in his message.

I also decided to use message.content in this example, because I'm not too sure if your args are case insensitive.

What you can do is take the message.content and use .split(" ") to create an array, then use slice() to get the information you need.

const messageContent = message.content.split(" ") // array

// We remove the first element (your prefix) from the array, and the last 
// element (the channel) to get the announcement message
const announcement = message.content.split(" ").slice(1).slice(0, -1).join(" ")

// Get the channel by accessing the last element in the array
const channel = messageContent[messageContent.length - 1]

// check if the provided channel is a mention or the ID of the channel
if(!channel.match(/<#[0-9]+>/) && isNaN(channel)) return message.reply({content: 'provide a valid channel'})

// search for the channel inside the guild. We replace "<#" and ">" to get the ID 
// in case that the channel was mentioned
const foundChannel = await message.guild.channels.cache.get(channel.replace('<#','').replace('>',''));

// if the guild has no channel
if(!foundChannel) return message.reply({content: 'provide a valid channel'})

// send the announcement
foundChannel.send({content: announcement})

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)
      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}`);
  },
};

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM