Bot code似乎在它被调用之前就已经触发了。

huangapple go评论53阅读模式
英文:

Bot code seems to fire off before the it even has the chance to get called

问题

我已经编写了一些代码,用来检查并通知用户,如果他们不在语音聊天中,就不应该使用特定的频道。代码如下:

const { Client, GatewayIntentBits } = require('discord.js');
require('dotenv/config');

var guildID = "1128642534483****";

const client = new Client({
    intents: [
        GatewayIntentBits.Guilds,
        GatewayIntentBits.GuildMessages,
        GatewayIntentBits.MessageContent,
        GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildPresences
    ],
});

client.on('ready', (c) => {
    console.log('The bot is ready');
})

client.on('messageCreate', message => {
    console.log(1)
    if (message.content !== "") {
        console.log(2)
        if (!message.author.bot) {
            const Guild = client.guilds.cache.get(guildID); // 获取服务器
            const Member = Guild.members.cache.get(message.author.id); // 获取成员
            if (Member.voice.channel) {
                console.log(`${Member.user.tag} is connected to ${Member.voice.channel.name}!`);
            }
            if (!Member.voice.channel) {
                console.log(`${Member.user.tag} is not connected.`);
                message.reply({ content: '这是语音聊天频道。' })
                    .then(msg => {
                        setTimeout(() => msg.delete(), 7500)
                    })
            };
        }
    }
})

client.login(process.env.TOKEN)

问题是,假设我加入语音聊天 -> 保存代码(部署节点) -> 离开语音聊天 -> 然后输入一些内容,它似乎认为我仍然在语音聊天中,并给我发送“这是语音聊天频道”的消息(我认为它在部署时运行,然后再也不运行)。

无论我离开/加入多少次语音聊天,它都会返回相同的值。我需要重新启动节点,才能让机器人再次运行代码。

我必须补充说明,我对JS和Discord.js都很陌生,我已经阅读了一些有关异步函数的内容,以为那可能是问题所在,但我仍然无法理解。感谢所有的帮助。

英文:

I've written some code to check and notify the user that they're not supposed to be using a specific channel if they're not in voice chat. The code is as follows:

const { Client, GatewayIntentBits } = require('discord.js');
require('dotenv/config')

var guildID = "1128642534483****"

const client = new Client({
	intents: [
		GatewayIntentBits.Guilds,
		GatewayIntentBits.GuildMessages,
		GatewayIntentBits.MessageContent,
		GatewayIntentBits.GuildMembers,
        GatewayIntentBits.GuildPresences
	],
});

client.on('ready', (c) => {
    console.log('The bot is ready')
})

client.on('messageCreate', message => {
    console.log(1)
    if (message.content !== "") {
        console.log(2)
        if(!message.author.bot) {
            const Guild = client.guilds.cache.get(guildID); // Getting the guild.
            const Member = Guild.members.cache.get(message.author.id); // Getting the member.
                if (Member.voice.channel) {
                console.log(`${Member.user.tag} is connected to ${Member.voice.channel.name}!`);
                } 
                if (!Member.voice.channel) {
                console.log(`${Member.user.tag} is not connected.`);
                message.reply({ content: 'This is the voice chat channel.'})
                    .then(msg => {
                setTimeout(() => msg.delete(), 7500)
                })
            };            
        }             
    }
})

client.login(process.env.TOKEN)

The issue I'm having is that, let's say I join voice chat -> save the code (deploy the node) -> leave voice chat -> then type something, it seems to think I'm still in VC and throws me the 'This is the voice chat channel' message (I believe it's being run on deployment, and then never again).

And it just keeps on returning the same value, regardless of how many times I leave/join VC. I need to restart the node in order to get the bot to actually run the code again.

I must add I'm very new to both JS and Discord.js, I've read a bit on async functions thinking that might've been the issue but I still can't get my head around it. I appreciate any and all help.

答案1

得分: 0

这看起来像是一个缓存问题。请注意,您从缓存中同时获取了服务器和成员,例如 client.guilds.cache.get(guildID)。如果缓存自部署以来没有更新,您的用户仍然被认为在语音频道中。我找不到关于缓存何时更新的明确信息,但我建议您使用 fetch 命令获取对象:

const Guild = await client.guilds.fetch(guildID); // 获取服务器。
const Member = await client.users.fetch(message.author.id); // 获取成员。

请注意,这个命令是异步的,返回一个 Promise,所以您必须要么等待它完成,要么使用像您已经在使用的 .then() 结构一样。

编辑:正如在这个答案的评论中讨论的那样,问题实际上是缺少了 GUILD_VOICE_STATES intent,可以在 这里 进行了讨论。

英文:

This looks like a caching problem. Notice how you pull both the guild and the member from the cache, e.g. client.guilds.cache.get(guildID). If the cache has not updated since being deployed, your user is still in there as being in the voice channel. I could not find clear information about when exactly the cache is updated, but I suggest that you get the objects via the fetch command:

const Guild = await client.guilds.fetch(guildID); // Getting the guild.
const Member = await client.users.fetch(message.author.id); // Getting the member.

Notice how this command is asynchronous, returning a Promise, so you must either await its completion or use a .then() construction like those you are already using.

Edit: As discussed in the comments to this answer, the problem was actually the missing GUILD_VOICE_STATES intent, as discussed here.

huangapple
  • 本文由 发表于 2023年7月13日 00:51:34
  • 转载请务必保留本文链接:https://go.coder-hub.com/76672864.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定