需要帮助将 Bot 更新到 Discord.js v12
Posted
技术标签:
【中文标题】需要帮助将 Bot 更新到 Discord.js v12【英文标题】:Need Help Updating Bot to Discord.js v12 【发布时间】:2021-06-28 22:20:51 【问题描述】:我正在尝试更新此代码以使用 Discord.js v12。我遇到了一个错误,我对如何修复这个错误有点困惑。我已经更新了一些代码以使用 Discord.js v12。
TypeError: client.channels.fetch(...).send 不是函数
在客户端。 (/home/runner/Log/index.js:44:40) 在 Client.emit (events.js:314:20) 在 Client.EventEmitter.emit (domain.js:483:12) 在 MessageCreateAction.handle (/home/runner/Log/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14) 在 Object.module.exports [as MESSAGE_CREATE] (/home/runner/Log/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32) 在 WebSocketManager.handlePacket (/home/runner/Log/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31) 在 WebSocketShard.onPacket (/home/runner/Log/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22) 在 WebSocketShard.onMessage (/home/runner/Log/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10) 在 WebSocket.onMessage (/home/runner/Log/node_modules/ws/lib/event-target.js:132:16)
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix, token, logsChannel = require('./config.json');
client.once('ready', () =>
console.log('I am alive!');
);
// Messages log
client.on('message', message =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
if(message.author.bot) return;
let username = message.author.tag;
let channel = message.channel.name;
let server = message.channel.guild;
var serverAvatarURL = message.guild.iconURL;
var attachment = (message.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_send = new Discord.MessageEmbed()
.setAuthor(username, message.author.avatarURL)
.setColor('23c115')
.setTitle("Message sent!")
.setDescription(message.content + " " + img)
.setImage(img)
.setFooter("#" + channel)
.setTimestamp();
client.channels.fetch(logsChannel).send(embed_send);
);
// Message edit log
client.on("messageUpdate", async(oldMessage, newMessage) =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
if (oldMessage.content === newMessage.content)
return;
var attachment = (oldMessage.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_edit = new Discord.MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.avatarURL)
.setColor('2615c1')
.setTitle("Message edited!")
.addField("Old", oldMessage.content + " " + img, true)
.addField("New", newMessage.content + " " + img, true)
.setFooter("#" + oldMessage.channel.name)
.setTimestamp();
client.channels.fetch(logsChannel).send(embed_edit);
);
// Message delete log
client.on("messageDelete", async message =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
var attachment = (message.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_delete = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.avatarURL)
.setColor('c11515')
.setTitle("Message deleted!")
.setDescription(message.content + " " + img)
.setImage(img)
.setFooter("#" + message.channel.name)
.setTimestamp();
client.channels.fetch(logsChannel).send(embed_delete);
);
client.login(token);
原始代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix, token, logsChannel = require('./config.json');
client.once('ready', () =>
console.log('I am alive!');
);
// Messages log
client.on('message', message =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
if(message.author.bot) return;
let username = message.author.tag;
let channel = message.channel.name;
let server = message.channel.guild;
var serverAvatarURL = message.guild.iconURL;
var attachment = (message.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_send = new Discord.RichEmbed()
.setAuthor(username, message.author.avatarURL)
.setColor('23c115')
.setTitle("Message sent!")
.setDescription(message.content + " " + img)
.setImage(img)
.setFooter("#" + channel)
.setTimestamp();
client.channels.get(logsChannel).send(embed_send);
);
// Message edit log
client.on("messageUpdate", async(oldMessage, newMessage) =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
if (oldMessage.content === newMessage.content)
return;
var attachment = (oldMessage.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_edit = new Discord.RichEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.avatarURL)
.setColor('2615c1')
.setTitle("Message edited!")
.addField("Old", oldMessage.content + " " + img, true)
.addField("New", newMessage.content + " " + img, true)
.setFooter("#" + oldMessage.channel.name)
.setTimestamp();
client.channels.get(logsChannel).send(embed_edit);
);
// Message delete log
client.on("messageDelete", async message =>
// Function
function isEmpty(collection)
for(var arg in collection)
if(collection.hasOwnProperty(arg))
return false;
return true;
// Logs
var attachment = (message.attachments).array();
if(isEmpty(attachment))
var img = "";
else
var img = attachment[0].url;
let embed_delete = new Discord.RichEmbed()
.setAuthor(message.author.tag, message.author.avatarURL)
.setColor('c11515')
.setTitle("Message deleted!")
.setDescription(message.content + " " + img)
.setImage(img)
.setFooter("#" + message.channel.name)
.setTimestamp();
client.channels.get(logsChannel).send(embed_delete);
);
client.login(token);
【问题讨论】:
【参考方案1】:.fetch() 返回一个Promise。
因此,您需要等待.then()
的结果。
在您的代码中(假设 logsChannel
是一个 ID):
client.channels
.fetch(logsChannel)
.then(fetchedChannel => fetchedChannel.send(embed_delete));
目前,您需要更新所有.fetch
es
【讨论】:
谢谢你的回答。【参考方案2】:正如 NullDev 提到的,.fetch()
返回一个承诺,因此需要先解决它。您可以使用.then()
和.catch()
方法或async
/await
。
与.then()
.catch()
:
client.channels
.fetch(logsChannel)
.then((ch) => ch.send(embed_send))
.catch(console.error);
async
/await
也一样:
try
(await client.channels.fetch(logsChannel)).send(embed);
catch (error)
console.log(error);
如果您有一个在三个不同处理程序中使用的函数(如isEmpty
),您可以将其移出并只定义一次。虽然你甚至不需要这个。 message.attachments
返回邮件中的附件集合。集合有一个 .size
访问器属性,它返回集合中元素的数量。
检查是否有附件就像检查其大小一样简单:message.attachments.size > 0
。
集合还有一个.first()
方法,它可以获取此集合中的第一个值。这意味着您可以像这样获取第一个附件的 URL:message.attachments.first().url
。
另一个错误是message.author.avatarURL
不起作用,因为.avatarURL()
是一个方法,所以你应该调用它。您可能可以改用displayAvatarURL()
,因为如果用户没有任何头像,它会返回用户的默认头像。
您可以在下面找到所有这些更改的代码:
const Discord = require('discord.js');
const client = new Discord.Client();
const prefix, token, logsChannel = require('./config.json');
client.once('ready', () =>
console.log('I am alive!');
);
client.on('message', async (message) =>
if (message.author.bot) return;
let username = message.author.tag;
let channel = message.channel.name;
let img = message.attachments.size > 0 ? message.attachments.first().url : '';
let embed = new Discord.MessageEmbed()
.setAuthor(username, message.author.displayAvatarURL())
.setColor('23c115')
.setTitle('Message sent!')
.setDescription(message.content + ' ' + img)
.setImage(img)
.setFooter('#' + channel)
.setTimestamp();
try
let log = await client.channels.fetch(logsChannel);
log.send(embed);
catch (error)
console.log(error);
);
client.on('messageUpdate', async (oldMessage, newMessage) =>
if (oldMessage.content === newMessage.content) return;
let img =
oldMessage.attachments.size > 0 ? oldMessage.attachments.first().url : '';
let embed = new Discord.MessageEmbed()
.setAuthor(oldMessage.author.tag, oldMessage.author.displayAvatarURL())
.setColor('2615c1')
.setTitle('Message edited!')
.addField('Old', oldMessage.content + ' ' + img, true)
.addField('New', newMessage.content + ' ' + img, true)
.setFooter('#' + oldMessage.channel.name)
.setTimestamp();
try
let log = await client.channels.fetch(logsChannel);
log.send(embed);
catch (error)
console.log(error);
);
client.on('messageDelete', async (message) =>
let img = message.attachments.size > 0 ? message.attachments.first().url : '';
let embed = new Discord.MessageEmbed()
.setAuthor(message.author.tag, message.author.displayAvatarURL())
.setColor('c11515')
.setTitle('Message deleted!')
.setDescription(message.content + ' ' + img)
.setImage(img)
.setFooter('#' + message.channel.name)
.setTimestamp();
try
let log = await client.channels.fetch(logsChannel);
log.send(embed);
catch (error)
console.log(error);
);
client.login(token);
【讨论】:
啊,我明白了。这解决了我的问题。谢谢你,我真的很感激。以上是关于需要帮助将 Bot 更新到 Discord.js v12的主要内容,如果未能解决你的问题,请参考以下文章