首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Broadcast All命令不自动删除广播- Discord.js-Commando

Broadcast All命令不自动删除广播- Discord.js-Commando
EN

Stack Overflow用户
提问于 2020-10-02 07:57:24
回答 1查看 83关注 0票数 3

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

我们让它像他想要的那样发送到所有的文本频道,但试图让它自动删除会触发以下错误:

代码语言:javascript
复制
(node:23) UnhandledPromiseRejectionWarning: TypeError [INVALID_TYPE]: Supplied options is not an object.

这是我的broadcast.js文件:

broadcast.js

代码语言:javascript
复制
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主分支上

-编辑

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

如果任何人想要使用固定的代码做任何事情,它是:

broadcast.js

代码语言:javascript
复制
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);
        });
      })
  };
};
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2020-10-02 17:02:33

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

由于您使用的是不一致的JS v12,因此Message.delete代码稍有更改。它不接受数字作为参数,而是接受一个options对象。此options对象可以有两个属性:timeoutreason。因此,要解决这个问题,只需更改.delete参数,如下所示:

代码语言:javascript
复制
// 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);
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/64164610

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档