簡體   English   中英

如何向特定的不和諧頻道發送消息?

[英]How to send a message to a specific discord channel?

您好,我剛剛開始創建一個不和諧的機器人。 我有代碼,是從在線教程中復制的,當我在我的"@everyone We are playing! :)"服務器上的任何地方輸入!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..
    }
  }
});

您必須獲取行會對象,並從中獲取通道對象,然后向該對象發送消息。

如果是觸發該命令的命令,並且您位於bot.on("message")

,您可以執行message.channel.send("Message")如果在!command上希望將其發送到特定頻道,則可以執行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");

在 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) {} );
    });
});

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM