最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - discord.js - guildMemberRemove doesn't work, guildMemberAdd works perfectly fine - Stack Overflow

programmeradmin2浏览0评论

Sorry if this is poorly formatted, I've never written a question on here before.

First of all, this is my first time writing anything in JavaScript, so this might be some dumb mistake I'm making that's causing my problem.

What I want to do is send a message when a member joins the server, and send a different message when a member leaves. When someone joins the server, the join message is sent and everything works perfectly fine, but when a member leaves, absolutely nothing happens. I put in some console.logs in the joinMessage and leaveMessage functions, and I get an output for joinMessage but nothing for remove. I have enabled Presence Intent and Server Members Intent on the Discord developer portal.

The join and leave message functions are down at the bottom, but I went ahead and added the entire thing so people could help me better. The bot token and channel IDs have been removed, and they are correct in my version of the code.

console.log('Starting');

const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const mandHandler = require("./mands");
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
client.login('token');
var server = client.guilds.cache.get('server');

client.on('ready', ready);
function ready()
{
    console.log('Authenticated');
    server = client.guilds.cache.get('server');
}

client.on('messageCreate', receivedMessage);
function receivedMessage(msg)
{
    mandHandler(msg);
}


client.on('guildMemberAdd', joinMessage);
function joinMessage(member)
{
    let channelWele = server.channels.cache.get('wele');
    let channelIntro = server.channels.cache.get('intro');
    channelWele.send(`Wele to the server, ${member}! Head over to ${channelIntro} and introduce yourself!`); 
}


client.on('guildMemberRemove', leaveMessage);
function leaveMessage(member)
{
    let channelWele = server.channels.cache.get('wele');
    channelWele.send(`${member} has left the server.`); 
}

I've been trying to figure this out for about an hour, so I'd really appreciate it if someone could help me solve this. Thanks!

Sorry if this is poorly formatted, I've never written a question on here before.

First of all, this is my first time writing anything in JavaScript, so this might be some dumb mistake I'm making that's causing my problem.

What I want to do is send a message when a member joins the server, and send a different message when a member leaves. When someone joins the server, the join message is sent and everything works perfectly fine, but when a member leaves, absolutely nothing happens. I put in some console.logs in the joinMessage and leaveMessage functions, and I get an output for joinMessage but nothing for remove. I have enabled Presence Intent and Server Members Intent on the Discord developer portal.

The join and leave message functions are down at the bottom, but I went ahead and added the entire thing so people could help me better. The bot token and channel IDs have been removed, and they are correct in my version of the code.

console.log('Starting');

const Discord = require('discord.js');
const { Client, Intents } = require('discord.js');
const mandHandler = require("./mands");
const client = new Discord.Client({ intents: [Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });
client.login('token');
var server = client.guilds.cache.get('server');

client.on('ready', ready);
function ready()
{
    console.log('Authenticated');
    server = client.guilds.cache.get('server');
}

client.on('messageCreate', receivedMessage);
function receivedMessage(msg)
{
    mandHandler(msg);
}


client.on('guildMemberAdd', joinMessage);
function joinMessage(member)
{
    let channelWele = server.channels.cache.get('wele');
    let channelIntro = server.channels.cache.get('intro');
    channelWele.send(`Wele to the server, ${member}! Head over to ${channelIntro} and introduce yourself!`); 
}


client.on('guildMemberRemove', leaveMessage);
function leaveMessage(member)
{
    let channelWele = server.channels.cache.get('wele');
    channelWele.send(`${member} has left the server.`); 
}

I've been trying to figure this out for about an hour, so I'd really appreciate it if someone could help me solve this. Thanks!

Share Improve this question edited Oct 28, 2021 at 14:58 AndrewFBR asked Sep 1, 2021 at 23:37 AndrewFBRAndrewFBR 231 silver badge7 bronze badges 2
  • I see absolutely no flaws in the code whatsoever, just to check if the event is being emitted could you do something like client.on("guildMemberRemove", (member) => { console.log("emitted"); } ( you may do it with a basic function too but I just prefer arrow functions so yea... – Zero Commented Sep 2, 2021 at 4:39
  • I did that and I'm still not getting anything – AndrewFBR Commented Sep 2, 2021 at 16:55
Add a ment  | 

3 Answers 3

Reset to default 4

You need to include following intent: GUILD_PRESENCES

So your code needs to look like this:

const client = new Discord.Client({ intents: [Intents.FLAGS.GUILD_PRESENCES, Intents.FLAGS.GUILDS, Intents.FLAGS.GUILD_MESSAGES, Intents.FLAGS.GUILD_MEMBERS] });

Now your bot will detect if a member leaves the guild.

Try calling the function directly in the event parameters. There might be some sort of confliction when extracting the function

client.on("guildMemberAdd", (member) => {
    let channelWele = server.channels.cache.get('wele');
    let channelIntro = server.channels.cache.get('intro');
    channelWele.send(`Wele to the server, ${member}! Head over to ${channelIntro} and introduce yourself!`); 
}

client.on("guildMemberRemove", (member) => {
    let channelWele = server.channels.cache.get('wele');
    channelWele.send(`${member} has left the server.`);
}

Also make sure you have set the right intents (if you are running V13), and gave the bot the correct privileged intents in the developer portal

This question/answer is rather old, but I found that the selected answer here is not fully correct, at least in the current version.

You do not in fact need the Presences Intent to receive guildMemberRemove event. The event is actually not firing in cases when the member data is not cached and will therefore not emit the event.

In order to resolve this, you will need to enable Partials on the client, specifically Partials.GuildMember. This will enable the event to emit on un-cached data.

发布评论

评论列表(0)

  1. 暂无评论