简体   繁体   English

我的 autorole 代码不起作用,我不明白为什么

[英]My code for autorole doesn't work and I can't figure out why

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

const client = new Client({
    intents: ["DIRECT_MESSAGES", "GUILDS", "GUILD_MEMBERS"],
    presence: {
        status: "online",
        activities: [{
            name: "markets",
            type: "WATCHING"
        }]
    },
});

client.on('ready', () => { 
    console.log(`Launched as a bot: ${client.user.tag}!`);
});


client.on('guildMemberAdd', member => {
    function roleAdd() {
        member.roles.add(member.guild.roles.cache.get(process.env.SERVER_ROLE_ID));
    }
    roleAdd();
});

client.login(process.env.DJS_TOKEN);

Guys I wrote this code but it is not working.伙计们,我写了这段代码,但它不起作用。 I am not getting any errors.我没有收到任何错误。 I want it to add a role whenever a user joins to my server.我希望它在用户加入我的服务器时添加一个角色。 the status of the bot is working.机器人的状态正在工作。 I think the issue is on the guildMemberAdd part.我认为问题出在guildMemberAdd部分。

Im not quite sure why you're using the approach of nested functions for this.我不太确定为什么要为此使用嵌套函数的方法。 If you want the event to call the function move the function outside of the event and then create the GuildMember in a parameter.如果您希望事件调用 function,请将 function 移到事件之外,然后在参数中创建GuildMember

Or discard the event entirely and leave the plain #<GuildMemberRoleManager>.add method by itself.或者完全丢弃事件并单独保留普通的#<GuildMemberRoleManager>.add方法。

I suggest the latter as I assume the nested function declaration wont work.我建议后者,因为我假设嵌套的 function 声明不会起作用。

If you wanted to use auto role add for your server, try the easiest method to do the role add, same way as giving role to a member如果你想为你的服务器使用自动角色添加,请尝试最简单的方法来添加角色,就像给成员赋予角色一样

When you giving a role to specific member should look like this:当您为特定成员赋予角色时,应该如下所示:

const role = message.guild.roles.cache.find(role => role.id === process.env.SERVER_ROLE_ID)
const target = message.mentions.members.first() || await message.guild.members.cache.get(args[0])

If a bot giving a role higher than the bot or equal to bot .如果 bot 给出的角色higher than the bot or equal to bot You will get an error such as Missing Permissions To prevent this you need to put a code to return it您将收到诸如“缺少权限”之类的错误。为防止出现这种情况,您需要输入代码以将其返回

if(!target.moderatable) return message.reply("I can't mute this person!")

This command is a same way for auto give role, just a few code lines added/edited/removed .此命令与自动赋予角色的方式相同,只是添加/编辑/删除了几行代码。

client.on('guildMemberAdd', member => {
function roleAdd() {
    member.roles.add(member.guild.roles.cache.get(process.env.SERVER_ROLE_ID));
}
roleAdd();
});
- function roleAdd() {}
+ let role = member.guild.roles.cache.find(role => role.id == process.env.SERVER_ROLE_ID)
+ member.roles.add(role).catch()

The .catch() line is to prevent on giving error and shutdown your bot. .catch()行是为了防止出错并关闭你的机器人。

client.on('guildMemberAdd', member => {
let role = member.guild.roles.cache.find(role => role.id == process.env.SERVER_ROLE_ID)
member.roles.add(role).catch()
});

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

相关问题 无法弄清楚为什么 React 代码不起作用 - Can't figure out why React code doesn't work clearInterval()不起作用,我无法弄清楚原因 - clearInterval() doesn't work and I can't figure out why 我不明白为什么我的代码不起作用 - I can't figure why my code does not work 简单的javascript代码不起作用,无法找出原因 - simple javascript code doesn't work, can't figure out why 无法弄清楚为什么我的 ROT13 转换器可以使用小写字母但不能使用大写字母 - Can't figure out why my ROT13 converter works with lowercase but it doesn't work with uppercase 我的 javascript 代码不会将元素推送到我的数组,我不知道为什么? - My javascript code won't push elements to my array and I can't figure out why? 我正在尝试为我的机器人在 discord.js 中创建一个自动角色 function 但它不起作用 - I'm trying to make an autorole function in discord.js for my bot but it doesn't work 我的javascript函数似乎无法正常工作。 我不知道为什么 - My javascript function doesn't seem to be working. I can't figure out why 我不明白为什么我的社交媒体分享链接不起作用 - I can`t figure out why my social media share links don`t work 我不明白为什么我的代码不起作用有人可以检查一下吗? 我很新 (JavaScript) - i can't understand why my code doesn't work can someone please check it out? I am very new (JavaScript)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM