Search code examples
javascriptdiscord.jscommando

Broadcast All Command not auto deleting broadcasts - Discord.js-Commando


So I'm trying to get my broadcast command to auto delete the broadcasts after a set amount of time. The person I built the EBS bot for wants it to auto delete after 30 minutes.

We got it to send to all text channels as he wanted but trying to make it auto delete triggers the following error:

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

Here is my broadcast.js file:

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

We would like to know how to set it to auto delete messages after 30 minutes.

I used an example from this post for the code to auto delete which apparently isn't working for me:

Making a bot delete its own message after a timeout

And the post that helped me get it to send to all channels is from:

Discord.js Commando Broadcast All command error

I'm assuming the sentMessage flag is what's erroring out, but I may be wrong.

Any help would be much appreciated.

The bot is built in discord.js-commando and uses node.js ^12.16.4 and discord.js ^12.0.1. It runs off of the discordjs/Commando master branch for discord.js-commando

---EDIT---

Thanks go to T. Dirk for the answer that fixed this.

If anyone wants to use the fixed code for anything, here it is:

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

Solution

  • The auto delete code you found and used is based on Discord JS v11. In this version, the Message.delete function only required a number as the parameter to set the delete timeout.

    Since you're using Discord JS v12, the Message.delete code is slightly changed. Instead of taking a number as a parameter, it takes an options object. This options object can have two properties; timeout and reason. So all you have to do to fix your issue is change the .delete parameter like so:

    // 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);