简体   繁体   中英

Discord.JS fetchMessage()

I have a problem with my Discord.JS Bot, I wanna to edit a embed in a spefic guild and channel, but when I try to run the commands, it errors out with this

/root/my-bot/my-bot.js:550
    guild.channels.get(channel).fetchMessage(user).edit(newMessage);
                               ^

TypeError: Cannot read property 'fetchMessage' of undefined
at Client.<anonymous> (/root/my-bot/my-bot.js:550:31)
at Client.emit (events.js:315:20)
at MessageCreateHandler.handle (/root/my-bot/node_modules/discord.js/src/client/websocket/packets/handlers/MessageCreate.js:9:34)
at WebSocketPacketManager.handle (/root/my-bot/node_modules/discord.js/src/client/websocket/packets/WebSocketPacketManager.js:105:65)
at WebSocketConnection.onPacket (/root/my-bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:333:35)
at WebSocketConnection.onMessage (/root/my-bot/node_modules/discord.js/src/client/websocket/WebSocketConnection.js:296:17)
at WebSocket.onMessage (/root/my-bot/node_modules/ws/lib/event-target.js:120:16)
at WebSocket.emit (events.js:315:20)
at Receiver.receiverOnMessage (/root/my-bot/node_modules/ws/lib/websocket.js:789:20)
at Receiver.emit (events.js:315:20)

Here's my command code:

 if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id')
     const channel = args[0];
     const user = args[1];
     let newMessage = args.slice(2).join(' ');
     guild.channels.get(channel).fetchMessage(user).edit(newMessage);
}

> If you are using Discord.js v11, the issue may come from the channel id you gave in the command.
> It appears that you try to fetch messages from an user but the method.fetchMessage() takes a message id as a parameter (See Discord.js documentation ).
> You can access the fetched Message with.then(callback function)

Fix

if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id');
     const channel = args[0];
     const messageId = args[1];
     let newMessage = args.slice(2).join(' ');
     let fetchedChannel = guild.channels.get(channel);
     if (!fetchedChannel) return console.log('Channel not found');
     fetchedChannel.fetchMessage(messageId).then(message => {
         message.edit(newMessage);
     }).catch(error => console.error(error));
}

Here is a possible fix:

 const prefix = 'YOUR_PREFIX';
if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id');
     const channel = args[0];
     const messageId = args[1];
     let newMessage = args.slice(2).join(' ');
     let fetchedChannel = guild.channels.get(channel);
     if (!fetchedChannel) {
         return console.log('Channel not found');
     }
     fetchedChannel.fetchMessage(messageId).then(message) => {
         message.edit(newMessage);
     }).catch(error => console.error(error));
}
 if(message.content.startsWith(prefix + "edit"))
 {
     message.delete();
     let guild = client.guilds.get('server-id')
     const channel = args[0];
     const user = args[1];
     let newMessage = args.slice(2).join(' ');
     guild.channels.get(channel).messages.fetch(user).then((msg) => {
       msg.edit(newmessage);
     })
}

you can try this

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM