指定从不和谐机器人发送聊天的通道(例如通道命令)



这是GLaDOS机器人,Connor RK800机器人和TypicalBot中发现的功能。

通常,该命令如下所示:

!说 #general 哎呀

文本将通过机器人出现在该频道中。

如果可能的话,我想将其添加到我自己的机器人中!

我有一个用于 say-delete 命令的基本代码。我必须添加什么,我必须带走什么?

if (command === "say") {
const sayMessage = args.join(" ");
message.delete().catch(O_o => {
// Catch error
});
message.channel.send(sayMessage);
}

谢谢!我真的很感激。

首先,您需要将在这种情况下定义参数的代码更改为const channel = args.shift();,这将返回 args[] 数组中的第一项。

然后,您可以使用message.guild.channels[channel].send(sayMessage);(我认为(确定用户想要将消息发送到的渠道。

总之,您的代码将是:

if(command === "say") {
const channel = args.shift();
const sayMessage = args.join(" ");
message.delete().catch(O_o=>{});  
message.guild.channels[channel].send(sayMessage);
}

由于我现在无法检查这一点,我不确定这是否有效,但值得一试!如果你愿意,我可以在我有能力的时候为你测试一下。

编辑: 我测试并修复了代码,希望我写的评论有足够的解释性。

const channel = args.shift().slice(2,-1); // this is due to how channel mentions work in discord (they are sent to clients as <#462650628376625169>, this cuts off the first <# and the finishing >)
const sayMessage = args.join(` `);
message.delete(); // you may want to add a catch() here, i didn't because my bot requires permissions to be added to a server
client.channels.get(channel).send(sayMessage); // client here may need to be replaced with bot, or app, or whatever you're using - client.channels returns a collection, which we use get() to find an item in

为了清楚起见,此代码必须位于您的if (command === "say")块中。

// making the say command
const sayCommand = `${prefix}say`
// say command
if (message.content.toLowerCase().startsWith(`${sayCommand}`)) {
// declaring args variable
const args = message.content.slice(sayCommand.length).split(/ +/);
// delcaring sayMessage that clears the say command from the say message
let sayMessage = args.join(` `);
// deletes the command message
message.delete();
// bot sends the contents of the command without the say command
message.channel.send(sayMessage)
}

这对我很好

最新更新