【问题标题】:Broadcast All Command not auto deleting broadcasts - Discord.js-Commando广播所有命令不自动删除广播 - Discord.js-Commando
【发布时间】:2021-01-17 17:18:39
【问题描述】:

所以我试图让我的广播命令在设定的时间后自动删除广播。我为其构建 EBS 机器人的人希望它在 30 分钟后自动删除。

我们让它按照他的意愿发送到所有文本频道,但尝试使其自动删除会触发以下错误:

(node:23) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object.

这是我的broadcast.js 文件:

broadcast.js

const Commando = require('discord.js-commando');
const prefix = (process.env.BOT_PREFIX);
require('dotenv').config();


module.exports = class BroadcastCommand extends Commando.Command {
  constructor(client) {
    super(client, {
      name: 'broadcast',
      aliases: [
        'ebcast',
        'bcast',
        'bc',
        'ebc'
      ],
      group: 'ebs',
      memberName: 'broadcast',
      userPermissions: [
        'MANAGE_MESSAGES',
        'MANAGE_CHANNELS'
      ],
      description: 'Send an Emergency Broadcast to all text channels in the guild',
      examples: [
        `Usage: ${prefix}bc <message.content>`,
        `Details: '<>' flags indicate a required field. '[]' flags indicates an optional field.`,
        `Note: Do not include the '<>' or '[]' flags in the command.`
      ],
      args: [
        {
          key: 'text',
          prompt: 'What would you like the bot to announce?',
          type: 'string',
        },
      ],
    })
  };

  run(msg, { text }) {
    msg.guild.channels.cache
      .filter(channel => channel.type === 'text')
      .forEach((textChannel) => {
        textChannel.send(text, { tts: true }).then(sentMessage => {
          sentMessage.delete(108000000).cache(console.error);
        });
      })
  }
};

我们想知道如何将其设置为在 30 分钟后自动删除消息。

我使用这篇文章中的一个示例来自动删除代码,这显然不适合我:

Making a bot delete its own message after a timeout

帮助我将它发送到所有频道的帖子来自:

Discord.js Commando Broadcast All command error

我假设 sentMessage 标志是错误的,但我可能错了。

任何帮助将不胜感激。

机器人内置于discord.js-commando 并使用node.js ^12.16.4discord.js ^12.0.1。它在discord.js-commandodiscordjs/Commando master 分支上运行

---编辑---

感谢T. Dirk 提供解决此问题的答案。

如果有人想将固定代码用于任何事情,这里是:

broadcast.js

const Commando = require('discord.js-commando');
const prefix = (process.env.BOT_PREFIX);
require('dotenv').config();


module.exports = class BroadcastCommand extends Commando.Command {
  constructor(client) {
    super(client, {
      name: 'broadcast',
      aliases: [
        'ebcast',
        'bcast',
        'bc',
        'ebc'
      ],
      group: 'ebs',
      memberName: 'broadcast',
      userPermissions: [
        'MANAGE_MESSAGES',
        'MANAGE_CHANNELS'
      ],
      description: 'Send an Emergency Broadcast to all text channels in the guild',
      examples: [
        `Usage: ${prefix}bc <message.content>`,
        `Details: '<>' flags indicate a required field. '[]' flags indicates an optional field.`,
        `Note: Do not include the '<>' or '[]' flags in the command.`
      ],
      args: [
        {
          key: 'text',
          prompt: 'What would you like the bot to announce?',
          type: 'string',
        },
      ],
    })
  };

  run(msg, { text }) {
    msg.guild.channels.cache
      .filter(channel => channel.type === 'text')
      .forEach((textChannel) => {
        textChannel.send(text, { tts: true }).then(sentMessage => {
          sentMessage.delete({ timeout: 108000000 }).catch(console.error);
        });
      })
  };
};

【问题讨论】:

    标签: javascript discord.js commando


    【解决方案1】:

    您找到并使用的自动删除代码基于 Discord JS v11。在这个版本中,Message.delete函数只需要一个数字作为参数来设置删除超时时间。

    由于您使用的是 Discord JS v12,Message.delete 代码略有更改。它不采用数字作为参数,而是采用选项对象。这个选项对象可以有两个属性; timeoutreason。因此,解决问题所需要做的就是更改 .delete 参数,如下所示:

    // Note that in your code you have .cache after the delete
    // but I'm guessing you meant .catch to catch errors
    sentMessage.delete({timeout: 108000000}).catch(console.error);
    

    【讨论】:

    • 好的,没有错误。我认为这行得通。将给它大约 10 分钟的时间删除消息。
    猜你喜欢
    • 2021-01-13
    • 2021-09-10
    • 1970-01-01
    • 1970-01-01
    • 2015-07-13
    • 1970-01-01
    • 2021-08-10
    • 2020-12-31
    • 2019-04-10
    相关资源
    最近更新 更多