【问题标题】:Cannot read property 'bot' of undefined [duplicate]无法读取未定义的属性“机器人”[重复]
【发布时间】:2019-04-26 10:50:55
【问题描述】:

我正在尝试制作一个小而简单的机器人来删除用户在文本频道中发布的消息(如果它们以某些特定字符开头)。这是代码:

var Discord = require('discord.io');
var logger = require('winston');
var auth = require('./auth.json');

//configure logger session
logger.remove(logger.transports.Console);
logger.add(new logger.transports.Console, { colorize: true });
logger.level = 'debug';

//inizialize 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) {
    var msg = message.content;

    if(message.author.bot) return;

    if(message.channel.id === 'CHANNEL ID')
       if(!msg.startsWith('!') && !msg.startsWith('gs.')) {
           message.delete();
           message.reply('In questo canale sono ammessi solo comandi per i' +
                         ' bot,riprova sul canale "generale"')
                  .then(d_msg => { d_msg.delete(7000); })
       }
});

这里我用 CHANNEL ID 替换了真实的频道 ID,只是为了确保不会泄露可能的敏感数据。

【问题讨论】:

  • if(message.author.bot) return;... message.author 不存在
  • 您是否尝试这样做:如果消息内容以“!”开头或“gs.”...然后删除消息并回复“In questo canale...”或反之?当消息内容不以“!”开头时,您要删除并回复还是“gs”?
  • @MattLong 如果消息不以“!”开头或“gs”机器人必须删除它然后回复

标签: javascript node.js discord discord.io


【解决方案1】:

如果邮件以“!”开头,则删除并回复邮件或“gs”。

bot.on('message', function(user, userID, channelID, message, evt) {
  if (message.author.bot || message.channel.id !== 'CHANNEL ID') return;

  const msg = message.content;

  const msgUnwanted = msg.match(/^!/) || msg.match(/^gs\./);

  if (msgUnwanted) {
    message
      .delete(2000)
      .then(msg => console.log(`Deleted message from ${msg.author.username}`))
      .catch(console.error);

    message
      .reply(`In questo canale sono ammessi solo comandi per i bot,riprova sul canale "generale"`)
      .then(sent => {
        sent
          .delete(2000)
          .then(msg => console.log(`Deleted message from ${msg.author.username}`))
          .catch(console.error);
      });
  }
});

【讨论】:

  • message.author.botmessage.delete().reply()discord.js 代码。原始海报使用discord.io,它不像discord.js那样面向对象
猜你喜欢
  • 2021-09-25
  • 1970-01-01
  • 2020-07-20
  • 2017-06-05
  • 2019-03-10
  • 2017-12-02
  • 2016-09-14
  • 2021-03-18
  • 2018-11-18
相关资源
最近更新 更多