Ad
Delete Message By ID
I've created a bot that shows a vote panel for polls on my server, ideally I would like it to delete the old message and send a new one to update the poll when people vote on things.
I've managed to get the old message ID using message.channel.fetchMessage and 'LastMessageID' is the right ID, but I can't seem to find a way to select and delete the message without making a load of errors in my console. For example I've tried:
message.channel.fetchMessage(LastMessageID)
.then(message.delete)
And it just gives the following errors:
(node:82184) UnhandledPromiseRejectionWarning: TypeError: Cannot read property 'client' of undefined
at resolve (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:480:14)
at new Promise (<anonymous>)
at delete (C:\Users\Username\Desktop\TestBot\node_modules\discord.js\src\structures\Message.js:479:14)
at <anonymous>
at process._tickCallback (internal/process/next_tick.js:189:7) (node:82184) UnhandledPromiseRejectionWarning: Unhandled promise rejection. This error originated either by throwing inside of an async function without a catch block, or by rejecting a promise which was not handled with .catch(). (rejection id: 2) (node:82184) [DEP0018] DeprecationWarning: Unhandled promise rejections are deprecated. In the future, promise rejections that are not handled will terminate the Node.js process with a non-zero exit code.
I feel really silly that I can't figure out how to do something so simple as delete a message by its ID. Any help would be much appreciated.
Ad
Answer
This is the thing you're looking for: finds the message and then deletes it.
// ASSUMPTIONS:
// channel: the channel you want the message to be sent in
// lastmsg: the id of the last poll message
channel.fetchMessage(lastmsg).then(msg => msg.delete());
This is fine, but let me suggest you a better way to do it:
// Option A: delete the old message and send the new one in the same function
channel.fetchMessage(lastmsg).then(async msg => {
await channel.send("Your new message.");
if (msg) msg.delete();
});
// Option B: if you have a poll dedicated channel that is kept cleaned and organized,
// you can edit the old message (you avoid notifications for every update)
channel.fetchMessage(lastmsg).then(msg => {
if (msg) msg.edit("Your new message.");
});
Ad
source: stackoverflow.com
Related Questions
- → How to update data attribute on Ajax complete
- → October CMS - Radio Button Ajax Click Twice in a Row Causes Content to disappear
- → Octobercms Component Unique id (Twig & Javascript)
- → Passing a JS var from AJAX response to Twig
- → Laravel {!! Form::open() !!} doesn't work within AngularJS
- → DropzoneJS & Laravel - Output form validation errors
- → Import statement and Babel
- → Uncaught TypeError: Cannot read property '__SECRET_DOM_DO_NOT_USE_OR_YOU_WILL_BE_FIRED' of undefined
- → React-router: Passing props to children
- → ListView.DataSource looping data for React Native
- → Can't test submit handler in React component
- → React + Flux - How to avoid global variable
- → Webpack, React & Babel, not rendering DOM
Ad