Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
469 views
in Technique[技术] by (71.8m points)

How do i delete a message in discord.js v12

I have made a bot, and when I updated to discord.js v12 I change the code to v12 but I get this error here is my code I have tried to uninstall and then install the discord.js

const Discord = require("discord.js");
const bot = new Discord.Client();

module.exports.run = async (bot, message, args) => {
    message.delete();
    let totalSeconds = (bot.uptime / 1000);
    totalSeconds %= 86400;
    let hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;
    let minutes = Math.floor(totalSeconds / 60);

    let uptimeEmbed = new Discord.MessageEmbed()
    .setDescription(`${bot.user.username} Bot Uptime`)
    .setColor("#e56b00")
    .addField("Hours", hours)
    .addField("Minutes", minutes)
    .setTimestamp()
    .setFooter(`Lavet`)
    
    message.channel.send(uptimeEmbed).then(message.delete({ timeout: 5000 })).catch(console.error)
}

module.exports.help = {
    name: "uptime" //NAVNET ER LIG MED KOMMANDOEN
}

This is the error I get when I try

C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrc
estRequestHandler.js:154
      throw new DiscordAPIError(request.path, data, request.method, res.status);
            ^

DiscordAPIError: Unknown Message
    at RequestHandler.execute (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrc
estRequestHandler.js:154:13)
    at processTicksAndRejections (node:internal/process/task_queues:93:5)
    at async RequestHandler.push (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrc
estRequestHandler.js:39:14)
    at async MessageManager.delete (C:UserslauriDesktopQuebecCity
ode_modulesdiscord.jssrcmanagersMessageManager.js:126:5) {
  method: 'delete',
  path: '/channels/791725159362330635/messages/798219060780466196',
  code: 10008,
  httpStatus: 404
}
question from:https://stackoverflow.com/questions/65670773/how-do-i-delete-a-message-in-discord-js-v12

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

In your code, you try to delete the same message twice.

First, in message.delete();, and next in message.channel.send(uptimeEmbed).then(message.delete({ timeout: 5000 })).catch(console.error)

For context, error code 10008 in the Discord API means that the message could not be found, which makes sense in the current situation.

In order to fix this, assuming that the second message.delete is trying to delete the uptimeEmbed message sent by the bot, you can do this:

module.exports.run = async (bot, message, args) => {
    let channel = message.channel;
    message.delete();

    let totalSeconds = (bot.uptime / 1000);
    totalSeconds %= 86400;
    let hours = Math.floor(totalSeconds / 3600);
    totalSeconds %= 3600;
    let minutes = Math.floor(totalSeconds / 60);

    let uptimeEmbed = new Discord.MessageEmbed()
    .setDescription(`${bot.user.username} Bot Uptime`)
    .setColor("#e56b00")
    .addField("Hours", hours)
    .addField("Minutes", minutes)
    .setTimestamp()
    .setFooter(`Lavet`)
    
    channel.send(uptimeEmbed).then(msg => msg.delete({ timeout: 5000 })).catch(console.error);
}

Instead of attempting to delete the same message twice, this stores the channel in a variable, send the message to the channel, and then deletes its own message.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...