简体   繁体   中英

How do I check if the user specified a channel?

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(" ");
}

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.

you can simply check if the args are a number with

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

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