[英]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.