简体   繁体   中英

discord.js v12 AFK command

I was trying to make a AFK command. I wanted a reason to be shown when a user the user gets pinged so I edited a code by a bit here is my current code:

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');
 }
});

When user is mentioned:

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);
 });
});

When I run the code there are no errors until the user gets pinged.. here is the error I get:

/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)

When you try to access messageafk here:

message.channel.send('The user is AFK: ' + messageafk)

It is looking for a variable defined inside the scope of the arrow function called on client.on() .

You should instead get such message from db and assign it to a variable before sending it.

You seem to use quick.db . if you want to access a variable you'll have to use the get() function: This:

message.channel.send('The user is AFK: ' + messageafk)

Will become:

message.channel.send('The user is AFK: ' + db.get(<User>.messageafk))

Of course you'll replace <User> by the actual mentionned user id

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