简体   繁体   English

如何检查用户是否指定了频道?

[英]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+)我正在尝试在 Discord 机器人中创建一个 say 命令,您可以在其中指定一个通道来发送消息,如果您没有指定一个通道,它将在命令之后发送所有内容(参数 1+),否则它将发送所有内容在指定的频道之后(参数 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.这是我的代码,但问题是要检查用户是否指定了频道,如果没有,将 msg 设置为等于 args.slice(1).join(" "),但我不知道如何检查用户是否指定了频道。 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 .或者,api 确实返回消息对象中提到的通道。
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.我没有使用过 discord.js,但我确信它们提供了一种很容易访问的方法。

you can simply check if the args are a number with你可以简单地检查 args 是否是一个数字

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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