[英]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.