Reputation: 1
Hello I have just started out with creating a discord bot. I have code, that I copied from a tutorial online, which when I enter !play
on my discord server anywhere, it will have a bot say "@everyone We are playing! :)"
.
I would like for it to work in a way so that when I enter !play
in the bot channel it messages the looking-for-game channel
. I will paste my whole code however I think the important part is:
bot.sendMessage({
to: channelID,
message: '@everyone The Owner, Admins and The OGs are playing Mass Effect Andromeda! Come play with them! :smiley:'
});
Is there a way to get it to send to a specific channel instead of channelID
?
Here is the whole code if a solution requires it.
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..
}
}
});
Upvotes: 0
Views: 5376
Reputation: 261
In PHP, you can do it like this :
$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) {} );
});
});
Upvotes: 0
Reputation: 300
You have to get the guild object, and from that get the channel object, then send a message to that.
bot.on("message")
, you can do message.channel.send("Message")
If on !command you want it to send to a specific channel, you can do message.guild.channel.get("CHANNEL-ID").send("Message");
You can also do message.guild.channel.find("name", "channel-name").send("Message");
Though this is not usually done, it's better to find by ID
bot.guilds.get('GUILD-ID').channels.get('CHANNEL-ID').send("Message");
Upvotes: 3