简体   繁体   中英

Discord greetings with cogs ,on_member_join , doen't work

My on_member_join listener doesn't execute properly. The idea is that when a new person enters my discord, my bot greets them with a custom message.

There are no syntax errors and the class loads correctly. The on_ready listener responds correctly with Welcome: ON . If I try to do a debug print it is not executed.

Where am I doing wrong? I don't understand, it seems correct to me.

import discord
from discord.ext import commands

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

class Welcome(commands.Cog):
    def __init__(self,client):
        self.client=client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Welcome: ON")
    
    @commands.Cog.listener()
    async def on_member_join(self, member):
        guild= client.get_guild(828676048039706694)
        channel=guild.get_channel(828676048039706697)
        if channel is not None:
            await channel.send(f'Welcome {member.mention}.')

    @commands.command()
    async def Modulo_benvenuto(self,ctx):
        await ctx.send('')

def setup(client):
    client.add_cog(Welcome(client))

This is my main file:

import discord
import os
from discord.ext import commands

client = commands.Bot(command_prefix = '.')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')
        

client.run('TOKEN')

With a new bot it works, this is the code:

import discord
from  discord.ext import commands

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


@client.event
async def on_ready():
    print("Bot is on")

@client.event
async def on_member_join(member):
    
    print(member)
    await member.send("hello")

    guild = client.get_guild(831588406089744435) 
    channel = discord.utils.get(member.guild.channels, id=831588406089744438)

    if guild:
           print("guild ok")
    else:
        print("guild not found")

    if channel is not None:
        await channel.send(f'Welcome to the {guild.name} Discord Server, {member.mention} !  :partying_face:')
    else:
        print("id channel wrong")


client.run('TOKEN')

For the on_member_join() event reference, you need the Server Members Intent privileged gateway intent to be on. This has to be done both in your bot's page as well as in your script (which you already have done):

  1. Go to the Discord Developer Portal .
  2. Select the application and, under settings, navigate to Bot .
  3. If you scroll down just a bit, you'll reach a section title Privileged Gateway Intents and, under that, SERVER MEMBERS INTENT .
  4. Toggle the SERVERS MEMBERS INTENT to ON .

In the image below, it would be the second of the privileged gateway intents.

特权网关意图


The issue with your code is likely because you have a discord.Bot instance defined in both your cog file as well as your main file. Since setup(client) is defined in your cog file and client.load_extension() is called in your main file, you should delete the following lines from your cog file:

cog.py

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

However, to conserve the Intents, you'll want to add the following lines before your discord.Bot() call:

main.py

from discord.ext import commands

intents = discord.Intents.default()
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)

For your code, I would recommend using the Client.fetch_channel method , since it eliminates the possibility that an incorrect Guild ID results in channel being None .

async def on_member_join(self, member):
    channel = await client.fetch_channel(1234567890)
    if channel is not None:
        await channel.send(f"Welcome {member.mention}.")

Or, you could just use discord.utils.get() and member.guild.channels :

async def on_member_join(self, member):
    channel = discord.utils.get(member.guild.channels, id=1234567890)
    if channel is not None:
        await channel.send(f"Welcome {member.mention}.")

Just a suggestion for reducing potential bugs.

HOW TO FIX:

MAIN FILE

import discord
import os
from discord.ext import commands

intents = discord.Intents.default()
client = commands.Bot(command_prefix = '.', intents=intents)
intents.members = True

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

@client.command()
async def reload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')
    client.load_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
        client.load_extension(f'cogs.{filename[:-3]}')
        

client.run('TOKEN')

COG FILE

import discord
from discord.ext import commands

class Welcome(commands.Cog):
    def __init__(self, client):
        self.client=client

    @commands.Cog.listener()
    async def on_ready(self):
        print("Welcome: ON")
    
    @commands.Cog.listener()
    async def on_member_join(self, member):
        print(member)
        await member.send("hello")
        guild = self.client.get_guild(831588406089744435)
        channel = discord.utils.get(member.guild.channels, id=831588406089744438)
        if guild:
            print("guild ok")
        else:
            print("guild not found")
        
        if channel is not None:
                await channel.send(f'Welcome to the {guild.name} Discord Server, {member.mention} !  :partying_face:')
        else:
            print("id channel wrong")

    @commands.command()
    async def Modulo_benvenuto(self, ctx):
        await ctx.send('test')

def setup(client):
    client.add_cog(Welcome(client))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM