【问题标题】:Discord.JS ReferenceError: stopTyping is not DefinedDiscord.JS ReferenceError:未定义 stopTyping
【发布时间】:2021-05-06 02:46:11
【问题描述】:

我正在尝试让我们的宣布命令工作,但它不断抛出以下错误:

@Nimbi, An error occurred while running the command: ReferenceError: stopTyping is not defined
You shouldn't ever receive an error like this.
Please contact Nimbi#4961.

这是我的announce.js 代码:

const stripIndents = require('common-tags').stripIndents;
const { Command } = require('discord.js-commando');
require('dotenv').config();

ANNOUNCE_CHANNEL_NAME = process.env.ANNOUNCEMENT_CHANNEL;
NEWS_CHANNEL_NAME = process.env.NEWS_CHANNEL;
MODLOG_CHANNEL_NAME = process.env.MODLOG_CHANNEL;

module.exports = class NewsCommand extends Command {
    constructor (client) {
      super(client, {
        name: 'announce',
        memberName: 'announce',
        group: 'mod',
        aliases: ['news', 'ann', 'a'],
        description: 'Make an announcement in the news channel',
        format: 'Announcement',
        examples: ['announce John Appleseed reads the news'],
        guildOnly: true,
        throttling: {
          usages: 2,
          duration: 3
        },
        args: [
          {
            key: 'body',
            prompt: 'What do you want me to announce?',
            type: 'string'
          }
        ],
        userPermissions: ['MANAGE_MESSAGES'],
        clientPermissions: ['MANAGE_MESSAGES']
      });
    }
  
    run (msg, {body}) {
      try {
        startTyping(msg);
  
        let announce = body,
          newsChannel = null;
  
        const announceEmbed = new MessageEmbed(),
          modlogChannel = msg.guild.settings.get('modLogChannel',
            msg.guild.channels.find(c => c.name === MODLOG_CHANNEL) ? msg.guild.channels.find(c => c.name === MODLOG_CHANNEL).id : null);
  
        if (msg.guild.settings.get('announcechannel')) {
          newsChannel = msg.guild.channels.find(c => c.id === msg.guild.settings.get('announcechannel'));
        } else {
          msg.guild.channels.find(c => c.name === 'announcements')
            ? newsChannel = msg.guild.channels.find(c => c.name === ANNOUNCEMENT_CHANNEL)
            : newsChannel = msg.guild.channels.find(c => c.name === NEWS_CHANNEL);
        }
  
        if (!newsChannel) throw new Error('nochannel');
        if (!newsChannel.permissionsFor(msg.guild.me).has(['SEND_MESSAGES', 'VIEW_CHANNEL'])) throw new Error('noperms');
  
        newsChannel.startTyping(1);
  
        announce.slice(0, 4) !== 'http' ? announce = `${body.slice(0, 1).toUpperCase()}${body.slice(1)}` : null;
        msg.attachments.first() && msg.attachments.first().url ? announce += `\n${msg.attachments.first().url}` : null;
  
        announceEmbed
          .setColor('#AAEFE6')
          .setAuthor(msg.author.tag, msg.author.displayAvatarURL())
          .setDescription(stripIndents`**Action:** Made an announcement`)
          .setTimestamp();
  
        newsChannel.msg.say(announce);
        newsChannel.stopTyping(true);
  
        if (msg.guild.settings.get('mod-logs', true)) {
          if (!msg.guild.settings.get('hasSentModLogMessage', false)) {
            msg.reply(oneLine`???? I can keep a log of moderator actions if you create a channel named **${MODLOG_CHANNEL_NAME}**
                          (or some other name configured by the ${msg.guild.commandPrefix}setmodlogs command) and give me access to it.
                          This message will only show up this one time and never again after this so if you desire to set up mod logs make sure to do so now.`);
            msg.guild.settings.set('hasSentModLogMessage', true);
          }
          modlogChannel && msg.guild.settings.get('mod-logs', false) ? msg.guild.channels.get(modlogChannel).msg.say('', {embed: announceEmbed}) : null;
        }
  
        stopTyping(msg);
  
        return msg.embed(announceEmbed);
  
      } catch (err) {
        stopTyping(msg);
  
        if ((/(?:nochannel)/i).test(err.toString())) {
          return msg.reply(`there is no channel for me to make the announcement in. Create channel named either ${ANNOUNCE_CHANNEL_NAME} or ${NEWS_CHANNEL_NAME}`);
        } else if ((/(?:noperms)/i).test(err.toString())) {
          return msg.reply(`I do not have permission to send messages to the ${ANNOUNCE_CHANNEL_NAME} or ${NEWS_CHANNEL_NAME} channel. Better go fix that!`);
        }
  
  
        return msg.reply(oneLine`An error occurred but I notified ${this.client.owners[0].username}
        Want to know more about the error? Join the support server by getting an invite by using the \`${msg.guild.commandPrefix}invite\` command `);
      }
    }
};

我的discord.js版本是:^12.5.1

我的discord.js-commando版本是:^0.11.1

我的node.js版本是:^12.0.0

我已经尝试过定义它:

const {startTyping, stopTypeing } = require('discord.js');

const {startTyping, stopTypeing } = require('util');

但是,两者都只是抛出了startTyping is not a function 错误。

任何帮助将不胜感激。

【问题讨论】:

  • discord.js 不导出 startTyping 函数,uiutil 也不导出(错别字?util 也不导出)。你可能想要channel.startTyping()。见stackoverflow.com/questions/44751653/…
  • 这会引发Reference Error: channel is not defined 错误。所以显然这也行不通。我可能只需要削弱这个。我开始认为这个特定代码与discord.js-commando 不兼容,或者至少不适用于我的设置......这很糟糕,因为它声称是为discord.js-commando 使用而构建的

标签: node.js sqlite discord.js commando


【解决方案1】:

注意:这是在 discord.js@13 及以后

开始打字

// like 101arrowz said, startTyping (or now sendTyping) is a method of the TextChannel, which you can get by msg.channel
message.channel.sendTyping()

Discord.js 和 commando(由同一个人开发)效果很好,但如果您有任何问题,我建议您阅读 docs 或在他们的 discord 服务器上询问(他们确实有帮助)

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2020-09-19
    • 2021-06-03
    • 2020-11-15
    • 2020-09-08
    • 2021-12-27
    • 1970-01-01
    • 1970-01-01
    • 2019-07-31
    相关资源
    最近更新 更多