why after i run this code nothing happend, can you fix it for me? it bassed on discord.js v14
为什么我运行这段代码后什么都没有发生,你能帮我修复它吗?它基于discord.js v14
the code :
代码:
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers, // Required to track member joins
],
});
const token = 'token';
const roleIdToAdd = '1038099433595867246'; // Replace with the ID of the role you want to assign
const logChannelId = '1150425856322314330'; // Replace with the ID of the log channel
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('guildMemberAdd', async (member) => {
// Check if the member is the bot itself
if (member.user) return;
// Assign the specified role to the member
try {
const role = member.guild.roles.cache.get(roleIdToAdd);
if (role) {
await member.roles.add(role);
}
} catch (error) {
console.error('Error assigning the role:', error);
}
// Send a log message to the designated log channel
const logChannel = member.guild.channels.cache.get(logChannelId);
if (logChannel) {
logChannel.send(`User ${member.user.tag} added/invited the bot to the server.`);
}
});
client.login(token);
I want my bot to automatically assign a role to someone who invites/adds my bot to their server and automatically send a log when someone adds/invites my bot to their server
我希望我的机器人自动为邀请/添加我的机器人到他们的服务器的人分配一个角色,并在有人添加/邀请我的机器人到他们的服务器时自动发送一个日志
更多回答
Any error? Does the bot have Manage roles
permission?
有什么错误吗?机器人是否具有管理角色权限?
member.user
will always be truthy and it will always return from the function. Maybe you want member.user.bot
to check if the user bot and if it's bot return and don't give the role.
Member.user将始终为真,并且它将始终从函数返回。也许您想要Member.user.bot检查用户bot是否返回,是否为bot,并且不授予角色。
优秀答案推荐
- Since
member.user
returns a User type, this is truthy in any case
- If you want to check if a Bot has been invited, you need to use the
guildCreate
event
(Actually, the Discord API does not provide information about the user who invited the bot, so you have to give up posting information about the user who invited the bot)
so finnaly code is
所以最后的代码是
const { Client, GatewayIntentBits } = require('discord.js');
const client = new Client({
intents: [
GatewayIntentBits.Guilds,
GatewayIntentBits.GuildMembers, // Required to track member joins
],
});
const token = 'token';
const roleIdToAdd = '1038099433595867246'; // Replace with the ID of the role you want to assign
const logChannelId = '1150425856322314330'; // Replace with the ID of the log channel
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}`);
});
client.on('guildMemberAdd', async (member) => {
// Check if the member is the bot itself
if (member.user.id === client.user.id) return;
// Assign the specified role to the member
try {
const role = member.guild.roles.cache.get(roleIdToAdd);
if (role) {
await member.roles.add(role);
}
} catch (error) {
console.error('Error assigning the role:', error);
}
});
client.on("guildCreate", (guild) => {
// Send a log message to the designated log channel
const logChannel = client.channels.cache.get(logChannelId);
if (logChannel) {
logChannel.send(`This bot has been added to \`${guild.name
}\` .`);
}
});
client.login(token);
更多回答
我是一名优秀的程序员,十分优秀!