【问题标题】:How to send a message to a specific discord channel?如何将消息发送到特定的不和谐频道?
【发布时间】:2019-04-11 12:03:29
【问题描述】:

您好,我刚刚开始创建一个不和谐的机器人。我有代码,是我从在线教程中复制的,当我在我的不和谐服务器上的任何地方输入!play 时,它会有一个机器人说"@everyone We are playing! :)"

我希望它以某种方式工作,以便当我在机器人频道中输入 !play 时,它会向 looking-for-game channel 发送消息。我将粘贴我的整个代码,但我认为重要的部分是:

bot.sendMessage({
  to: channelID,
  message: '@everyone The Owner, Admins and The OGs are playing Mass Effect Andromeda! Come play with them! :smiley:'
});

有没有办法让它发送到特定频道而不是channelID
如果解决方案需要,这是整个代码。

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');
// Configure logger settings
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, {
  colorize: true
});
logger.level = 'debug';
// Initialize Discord Bot
var bot = new Discord.Client({
  token: auth.token,
  autorun: true
});
bot.on('ready', function(evt) {
  logger.info('Connected');
  logger.info('Logged in as: ');
  logger.info(bot.username + ' - (' + bot.id + ')');
});
bot.on('message', function(user, userID, channelID, message, evt) {
  // Our bot needs to know if it will execute a command
  // It will listen for messages that will start with `!`
  if (message.substring(0, 1) == '!') {
    var args = message.substring(1).split(' ');
    var cmd = args[0];

    args = args.splice(1);
    switch (cmd) {
      // !play
      case 'play':
        bot.sendMessage({
          to: channelID,
          message: '@everyone The Owner, Admins and The OGs are playing Mass Effect Andromeda! Come play with them! :smiley:'

        });
        break;
        // Just add any case commands if you want to..
    }
  }
});

【问题讨论】:

标签: javascript discord discord.io


【解决方案1】:

你必须得到公会对象,然后从中得到频道对象,然后向它发送消息。

如果是触发此操作的命令,并且您在 bot.on("message")

,你可以做message.channel.send("Message")如果你希望它发送到特定频道,你可以做message.guild.channel.get("CHANNEL-ID").send("Message");

也可以message.guild.channel.find("name", "channel-name").send("Message");,虽然一般不这样做,但最好通过ID来查找

如果你不是,你必须这样做:

bot.guilds.get('GUILD-ID').channels.get('CHANNEL-ID').send("Message");

【讨论】:

    【解决方案2】:

    在 PHP 中,你可以这样做:

    $newChannel = $interaction->guild->channels->create([
         'category'  => $this->channelRPG,
         'parent_id' => $this->channelRPG->id,
         'owner_id'  => $author->id,
         'name'      => $displayedDate->format('Y m d')." ".$txt,
         'type'      => Channel::TYPE_TEXT,
         'topic'     => "",
         'nsfw'      => false
     ]);
    $interaction->guild->channels->save($newChannel)->done(function (Channel $channel) use ($interaction) {
        $author  = $interaction->member->user;
        $txt = $author->username." added an event.";
        $channel->sendMessage($txt)->done( function ($msg) {
            // To pin this new message in the new channel.
            $channel->pinMessage($msg)->done( function ($x) {} );
        });
    });
    

    【讨论】:

      猜你喜欢
      • 2020-07-14
      • 2021-06-17
      • 2020-05-07
      • 1970-01-01
      • 2021-08-14
      • 2020-11-06
      • 2020-11-16
      • 2021-04-09
      • 2021-05-08
      相关资源
      最近更新 更多