Trevor Mathisen
Trevor Mathisen

Reputation: 13

Welcoming new members not working with discord.py

I'm trying to setup a simple discord.py enabled bot and having trouble welcoming new members. I have the following code and when a new member joins, the bot is able to process the default welcome message Discord sends but doesn't process anything in the on_member_join() function. I've enabled intents within Discord (both gateway and member) and still can't figure out why it won't process a new member joining. I've also tested with a brand-new created member and still won't trigger.

import discord

intents = discord.Intents.default()
intents.members = True

client = discord.Client()

@client.event
async def on_ready(): # When ready
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_member_join(member):
    print('Someone new!')
    await member.send("Welcome")

@client.event
async def on_message(message): # On every message
    if message.author == client.user: # Cancel own message
        return

    if message.content.startswith('?'):
        await message.channel.send('Command')

client.run(DISCORD_TOKEN)

Upvotes: 0

Views: 593

Answers (2)

Trevor Mathisen
Trevor Mathisen

Reputation: 13

EDIT: See answer. Changing the below correctly passed intents (with members = True) to the bot.

Unsure why but I needed to use bot commands in order to resolve the issue.

Added:

from discord.ext import commands

and changed

client = discord.Client()

to

client = commands.Bot(command_prefix = "!", intents = intents)

I think this is what was required to 'fully enable' intents, but I'm not fully sure why this corrected the issue.

Upvotes: 1

stijndcl
stijndcl

Reputation: 5651

This is not related to using Client instead of Bot.

You created the intents variable, but you're never using it. You're supposed to pass it into your discord.Client(), which you aren't doing, so the members intent will always be disabled.

# Original question
intents = discord.Intents.default()
intents.members = True

client = discord.Client()  # <-- You're not passing intents here! The variable is never used so intents are disabled

That's also why your answer fixes it: because you're actually using your intents (...intents=intents...).

# Your "fix"
client = commands.Bot(command_prefix = "!", intents = intents)  # <-- Notice the intents

Using discord.Client or commands.Bot has no influence on this: commands.Bot without passing intents wouldn't do anything either.

# This would cause the exact same issue, because intents aren't used
client = commands.Bot(command_prefix = "!")

Passing your intents into the Client would also work, just like that fixes the problem for your Bot.

client = discord.Client(intents=intents)

Upvotes: 1

Related Questions