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将始终是truthy,并且它将始终从函数返回。也许你想让member.user.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
所以finnaly代码是
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);
更多回答
我是一名优秀的程序员,十分优秀!