0
votes

I'm trying to make a say command in a Discord bot where you specify a channel to send the message, and if you don't specify a channel it will send everything after the command(args 1+), otherwise it will send everything after the channel specified(args 2+)

if(command === `${prefix}say`) {
    if(!message.member.roles.has(`337402636015894528`)) return;
    msg = args.slice(2).join(" ");
    let channel = message.mentions.channels.first() || message.guild.channels.find("name", args[1]) || message.channel;
    message.delete();
    channel.send(msg);
}

This is the code I have, but the problem is that want to check if the user specified a channel, and if not, set msg equal to args.slice(1).join(" "), but I have no idea how to check if the user specified a channel. Does anybody know how? Thanks in advance.

I figured out the solution, thank you all for your help.

let channel = message.mentions.channels.first() || message.guild.channels.find("name", args[1]);
if(!channel) { 
    channel = message.channel;
    msg = args.slice(1).join(" ");
}
2

2 Answers

0
votes

Tagging a channel in a message looks like this
<#channelid>
Example:
<#316648154927938738>
So you can match against that.

Alternatively, the api does return mentioned channels in the message object.
So that can be used as well.
I haven't used discord.js, but I'm sure they offer a way of accessing that quite easily.

0
votes

you can simply check if the args are a number with

if(isNaN(args[0])) {
// stuff
}