You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

  1. Invalid message reference: In the guildMemberAdd event, there's no message object available—you need to use member.guild.id instead of message.guild.id.
  2. Misuse of hasProp: Enmap's hasProp method only checks if a property exists, not its value. You should directly retrieve the welcomer boolean and check if it's true.
  3. 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:

  1. Use member.guild.id to target the correct guild settings.
  2. Check if client.configs.get(member.guild.id, "welcomer") is true before running the goodbye message code.
  3. 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 welcomer setting for a guild, use client.configs.set(guildId, false, "welcomer") to disable it, or true to enable it.

内容的提问来源于stack exchange,提问作者xItsDanger

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.14 06:25:58