Discord.js中如何通过Enmap检测属性值?欢迎功能配置故障排查
Fixing Your Discord Bot's Welcome/Goodbye Event Trigger Logic
Let's break down the issues in your code and fix them step by step to get the welcomer setting working as intended.
Key Issues in Your Current Code
- Invalid
messagereference: In theguildMemberAddevent, there's nomessageobject available—you need to usemember.guild.idinstead ofmessage.guild.id. - Misuse of
hasProp: Enmap'shasPropmethod only checks if a property exists, not its value. You should directly retrieve thewelcomerboolean and check if it'strue. - Unchecked channel lookup: If the configured log channel doesn't exist, your bot will crash—add a safety check here.
Corrected guildMemberAdd.js Code
const config = require("../../config.json"); const vars = require("../../variables.json"); const Discord = require("discord.js"); const defaultSettings = require("../../defaultSettings.js"); const Enmap = require("enmap"); module.exports = async (client, member) => { // Ensure the guild has settings (uses defaults if not present) client.configs.ensure(member.guild.id, defaultSettings); // Get the welcomer setting for this guild const isWelcomerEnabled = client.configs.get(member.guild.id, "welcomer"); // Exit early if welcomer is disabled if (!isWelcomerEnabled) return; // Fetch and format the welcome message let welcomeMessage = client.configs.get(member.guild.id, "welcomeMessage"); welcomeMessage = welcomeMessage .replace(/%user/g, member.user) .replace(/%server/g, member.guild.name) .replace(/%total/g, member.guild.memberCount); // Create the embed const welcomemsg = new Discord.RichEmbed() .setTitle(`Hello! :wave:`) .setDescription(welcomeMessage) .setThumbnail(member.user.avatarURL) .setColor(vars.good) .setTimestamp() .setFooter(`ID: ${member.user.id}`, member.user.avatarURL); // Find the log channel and send the message (with safety check) const logChannel = member.guild.channels.find(x => x.name === client.configs.get(member.guild.id, "logChannel")); if (logChannel) { logChannel.send(welcomemsg); } else { console.log(`Warning: Welcome log channel not found in guild ${member.guild.name}`); } };
Applying the Same Logic to guildMemberRemove
For your guildMemberRemove event, replicate the same pattern:
- Use
member.guild.idto target the correct guild settings. - Check if
client.configs.get(member.guild.id, "welcomer")istruebefore running the goodbye message code. - Add a safety check for the log channel.
Additional Notes
- Make sure your main bot file initializes the Enmap correctly:
const Enmap = require('enmap'); client.configs = new Enmap({ name: 'configs' }); // This creates/loads the configs database - When updating the
welcomersetting for a guild, useclient.configs.set(guildId, false, "welcomer")to disable it, ortrueto enable it.
内容的提问来源于stack exchange,提问作者xItsDanger
相关产品推荐
相关产品推荐

