简体   繁体   English

Discord.js guildCreate 和 guildDelete 事件不会被触发

[英]Discord.js guildCreate and guildDelete events are not fired

What is my problem?我的问题是什么? Sometimes I don't get guildCreate and guildDelete events.有时我没有得到guildCreateguildDelete事件。

Does the problem occur every time?问题是不是每次都出现? No. About 40% of the time it works without a problem.不会。大约 40% 的时间它可以正常工作。

Do I get an error message?我会收到错误消息吗? No. No error message is fired.不会。不会触发任何错误消息。

My code:我的代码:

const DiscordServer = require('./backend-lib/models/DiscordServer');

const { Client, Intents } = require('discord.js');

const client = new Client({ intents: [ Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD ] });

client.once('ready', () => {
  console.log('ready');
});

/* It's listening to the event `guildCreate` and then it's creating a new document in the database. */
client.once('guildCreate', async (guild) => {
  console.log('create');
});

/* It's listening to the event `guildDelete` and then it's deleting the document in the database. */
client.once('guildDelete', async (guild) => {
  console.log('delete', guild.id);
});

/* It's connecting to SQS and listening to it. */
client.once('ready', async () => {
   require('./services/helper');
});

client.login(conf.discord.token)

It seems you're using the correct intents.看来您使用的是正确的意图。 However, you're using client.once , that adds a one-time listener for your guildCreate and guildDelete events.但是,您使用的是client.once ,它为您的guildCreateguildDelete事件添加了一个一次性侦听器。 The first time the event is triggered, the listener is removed and the callback function is fired.第一次触发事件时,监听器被移除并触发回调函数。 As the listener gets removed, the callback won't run again unless you restart your bot.随着侦听器被删除,除非您重新启动机器人,否则回调将不会再次运行。

If you want to trigger these events more than once, you can use the .on() method:如果要多次触发这些事件,可以使用.on()方法:

client.on('guildCreate', async (guild) => {
  console.log('create');
});

client.on('guildDelete', async (guild) => {
  console.log('delete', guild.id);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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