简体   繁体   English

Discord.js一种说渠道用户选择的漫游器

[英]Discord.js A bot That Say Something in a Channel user Chooses

So basically I try to write some code that when the user type .say "message" in #general the bot will go to #general channel and say "message" 因此,基本上,我尝试编写一些代码,以便当用户在#general中键入.say“ message”时,机器人将转到#general频道并说“ message”

I tried working on that code so much, but I don't know how to assign a channel for the bot to say the message in 我尝试了很多代码,但是我不知道如何为机器人分配一个通道,以便在

`bot.on('message', message => {
    const args = message.content.split(" ").slice(1);
    if(message.content.startsWith('.say')) {
        message.delete()
        var saytext = args.join(" ");
        message.channel.send(saytext)
    };
  } )`

So the code is working when I type .say and whatever comes after it the bot say it in the channel the user said the code, but what I want is to make the bot send the message in another channel not the same channel. 因此,当我键入.say时,代码就可以正常工作了,然后机器人在用户说出该代码的通道中说出了什么,但是我想要让机器人在另一个通道而不是同一通道中发送消息。 I mean if the server have #general and #Music and the user was in #general and just typed .say I love music in #Music the bot will say "I love music in #Music" not "I love music" in "#music". 我的意思是,如果服务器上有#general和#Music,并且用户使用的是#general且只是键入了.say我喜欢#Music中的音乐,则机器人会说“我喜欢#Music中的音乐”而不是“#我喜欢音乐”音乐”。

Instead of using message.channel.send , which makes the bot answer in the same channel the message was sent, you need to retrieve the specified channel. 您无需检索message.channel.send (使机器人在与发送消息的通道相同的位置上回答),而是需要检索指定的通道。 To achieve this, you can use message.mentions.channels to detect which channel(s) have been mentionned in your command. 为此,您可以使用message.mentions.channels来检测命令中提到的通道。

Here is an example of how you can do this. 这是如何执行此操作的示例。 Please also note that in my case, I made it work with a command like : .say <#channel> <message> , this syntax is easier to parse than the one you asked for. 另请注意,在我的情况下,我使用如下命令来使它工作: .say <#channel> <message> ,此语法比您要求的语法更易于解析。

In your case, you'll have to type ".say #Music I love music." 对于您的情况,您必须输入“ .say #Music I love music”。 in any channel the bot has access to. 该机器人可以访问的任何渠道。

bot.on('message', message => {
  if (message.content.startsWith(".say")) {
      // Get the channel mention
      if (message.mentions.channels.size == 0) {
          message.reply("please mention a channel first.");
      }
      else {
          let targetChannel = message.mentions.channels.first();
          // Get the message to print

          const args = message.content.split(" ").slice(2);
          let saytext = args.join(" ");
          targetChannel.send(saytext);
          message.delete();
      }
});

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

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