我试图发出AFK命令。我希望在用户ping用户时显示原因,因此我稍微编辑了一个代码,这是我当前的代码:
client.on('message', (message) => {
if (db.has(message.author.id + '.afk')) {
message.channel.send(`Welcome back ${message.author} I removed your AFK.`);
db.delete(message.author.id + '.afk');
db.delete(message.author.id + '.messageafk');
}
if (message.content.startsWith('+afk')) {
message.channel.send(
'Aight, I have set your AFK. I will send a message to the users who mention you..'
);
// then here you use the database :
db.set(message.author.id + '.afk', 'true');
db.set(
message.author.id + '.messageafk',
message.content
.slice(5)
.trim()
.split(/ +/g)
);
}
if (message.content.includes('+afk off')) {
db.delete(message.author.id + '.afk');
db.delete(message.author.id + '.messageafk');
}
});
提到用户时:
client.on('message', (message) => {
// If one of the mentions is the user
message.mentions.users.forEach((user) => {
if (message.author.bot) return false;
if (
message.content.includes('@here') ||
message.content.includes('@everyone')
)
return false;
if (db.has(user.id + '.afk'))
message.channel.send('The user is AFK: ' + messageafk);
});
});
当我运行代码时,直到对用户执行ping操作,都没有错误。 这是我得到的错误:
/home/runner/MutedPinkBlogclient/index.js:224
if(db.has(user.id + '.afk')) message.channel.send('The user is AFK: ' + messageafk)
^
ReferenceError: messageafk is not defined
at /home/runner/MutedPinkBlogclient/index.js:224:73
at Map.forEach (<anonymous>)
at Client.<anonymous> (/home/runner/MutedPinkBlogclient/index.js:220:24)
at Client.emit (events.js:327:22)
at Client.EventEmitter.emit (domain.js:483:12)
at MessageCreateAction.handle (/home/runner/MutedPinkBlogclient/node_modules/discord.js/src/client/actions/MessageCreate.js:31:14)
at Object.module.exports [as MESSAGE_CREATE] (/home/runner/MutedPinkBlogclient/node_modules/discord.js/src/client/websocket/handlers/MESSAGE_CREATE.js:4:32)
at WebSocketManager.handlePacket (/home/runner/MutedPinkBlogclient/node_modules/discord.js/src/client/websocket/WebSocketManager.js:384:31)
at WebSocketShard.onPacket (/home/runner/MutedPinkBlogclient/node_modules/discord.js/src/client/websocket/WebSocketShard.js:444:22)
at WebSocketShard.onMessage (/home/runner/MutedPinkBlogclient/node_modules/discord.js/src/client/websocket/WebSocketShard.js:301:10)
答案 0 :(得分:0)
当您尝试在此处访问messageafk
时:
message.channel.send('The user is AFK: ' + messageafk)
它正在寻找在client.on()
上调用的箭头函数范围内定义的变量。
您应该从db
获取此类消息,并在发送之前将其分配给变量。
答案 1 :(得分:0)