简体   繁体   中英

How to send a message to a specific discord channel?

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..
    }
  }
});

You have to get the guild object, and from that get the channel object, then send a message to that.

If it's a command that triggers this, and you're in your 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

If you're not, you have to do this:

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM