【问题标题】:How would I kick a user after a certain amount of time of joining the server in Discord.JS在 Discord.JS 中加入服务器一段时间后,我将如何踢用户
【发布时间】:2021-04-15 19:34:16
【问题描述】:

因此,如果成员没有角色,我正在尝试制作一个机器人,该机器人会在成员加入服务器一定时间后将其踢出。到目前为止,这是我的代码。请记住,我正在使用 sleep npm 插件https://www.npmjs.com/package/sleep

const fs = require('fs');
const Discord = require('discord.js');
const { prefix, token } = require('./config.json');
const db = require('quick.db')
const sleep = require('sleep');

const client = new Discord.Client();
client.commands = new Discord.Collection();

const commandFiles = fs.readdirSync('./commands').filter(file => file.endsWith('.js'));

for (const file of commandFiles) {
    const command = require(`./commands/${file}`);
    client.commands.set(command.name, command);
}

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

client.on('message', message => {
    if (!message.content.startsWith(prefix) || message.author.bot) return;

    const args = message.content.slice(prefix.length).trim().split(/ +/);
    const command = args.shift().toLowerCase();
    if (!client.commands.has(command)) return;
    try {
        client.commands.get(command).execute(message, args);
    } catch (error) {
        console.log(error);
    }

});

client.on("guildMemberAdd", (member) => {
    member.send("Welcome to the faith garden server! Please verify in the <#685597195789008993> to verify, you can see the questions at <#765603302774669372>")
    sleep.sleep("5")
    if(member.guild.roles.find(role => role.name === "Member")) {

    }
    else if(member.guild.roles.find(role => role.name === "Pre-Member")) {
        
    }
    else if(member.guild.roles.find(role => role.name === "Alpha-Member")) {
        
    }
    else if(member.guild.roles.find(role => role.name === "Observer")) {
        
    }
    else {
        member.kick
        member.send("Sorry we had to kick you because you didnt respond soon enough, you can always join back since this is just a temporary kick.")
    }
  });

client.login(token);

我怎么知道代码不起作用是因为节点。已经在控制台中,然后我加入了 alt,什么也没发生,没有 dm,5 秒后没有踢。没有。如果您知道如何修复机器人不做任何事情,请告诉我。 :)

【问题讨论】:

  • 不清楚你在这里要解决什么问题,或者你的问题的核心是什么(你的代码转储以及它“什么都不做”的声明真的不是特别描述性的)。根据How to Ask 指南为您的问题添加相关上下文。
  • 我现在更深入地解释了。 @esqew
  • 是否出现任何错误?

标签: javascript discord discord.js


【解决方案1】:

从您的代码中,很明显您使用的是 discord.js v11。 如果你想收听guildMemberAdd 事件,你不能使用 djs v11。 Discord 最近在其 API 中添加了一个 intents 功能,这会阻止你在没有特别订阅的情况下可靠地收听公会事件给他们。 Discord.js v11 不支持 Intent,因此您必须更新到 discord.js v12。

您需要订阅特定的意图才能可靠地接收关联事件。 guildMemberAdd 位于需要订阅意图的 list of events

无论您在哪里定义客户端,这里都有一种可能的解决方法:

const intents = ["GUILDS", "GUILD_MEMBERS"];
const client = new Discord.Client({intents: intents, ws:{intents: intents}});

您还需要在其 Discord 开发者页面上为您的机器人启用以下设置,因为公会成员加入事件是一个特权意图

由于更新到 v12.x.x,您的部分代码可能需要更改;请查看版本之间的what's changed

此外,您的代码非常有问题并且不必要地过于复杂。当您可以只使用setTimeout 时,为什么还要使用节点包sleep?为什么你有一大堆空的if 声明?此外,您不是在检查成员是否有角色,而是在检查 member.guild.roles.find() 是否在检查 guild 是否有角色。以下是您的事件处理程序中的一些更正:

client.on("guildMemberAdd", (member) => {
    member.send("Welcome to the faith garden server! Please verify in the <#685597195789008993> to verify, you can see the questions at <#765603302774669372>");

    var verifiedRoles = ["Member","Pre-Member","Alpha-Member","Observer"];

    setTimeout(() => {
        if(!member.roles.cache.find(role => verifiedRoles.includes(role.name))) {
            member.send("Sorry we had to kick you because you didnt respond soon enough, you can always join back since this is just a temporary kick.")
            .then(() => {
                member.kick();
            });
        }
    }, 5 * 1000); //The timeout is in ms, convert by multiplying seconds by 1000
});

相关资源:
List of intents and associated events
General info about intents

【讨论】:

  • 好的,但是,我有一个问题,在我看来,if 语句是在说你确实有这个角色,那么为什么当我没有这个角色时它会起作用。
  • 查看if 语句的组成部分。 member.roles.cache.find(condition) 根据条件查找角色,如果没有找到与条件匹配的角色,则返回 undefined! 在 javascript 中表示 NOT。通过if(!member.roles.cache.find(condition)),我们实质上是在说if NOT role found。如果没有感叹号,它会检查你是否有这个角色,但如果有感叹号,它会检查你是否有NOT 有这个角色。
猜你喜欢
  • 1970-01-01
  • 2020-06-30
  • 2021-05-29
  • 1970-01-01
  • 2021-03-05
  • 2023-03-18
  • 1970-01-01
  • 2021-02-07
  • 1970-01-01
相关资源
最近更新 更多