如何向特定的不和频道发送消息



你好,我刚刚开始创建一个discord机器人。我有一段代码,是从网上教程中复制的,当我在discord服务器上的任何位置输入!play时,它都会有一个机器人,比如"@everyone We are playing! :)"

我希望它能以这样的方式工作,即当我在bot通道中输入!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")如果启用!命令你想让它发送到一个特定的通道,你可以做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) {} );
});
});

最新更新