my function on_member_join(member) is never called

寵の児 提交于 2021-01-29 08:18:28

问题


i'm doing a discord bot (python 3.8.6).

My function on_ready(): is functional, same for my function "say_hello"

But i don't know why, discord never detects on_member_join(member)

bot = commands.Bot(command_prefix =".", description = "yolooooooooooooooooooooooooooooo")

#prints 'logged on' when bot is ready
@bot.event
async def on_ready():
    print('logged on salaud')


@bot.event
async def on_member_join(member):
    embed=discord.Embed(title=f"Welcome To Mafia Server, Be carefully or you will die", color=random.randint(0, 0xFFFFFF))
  
    print("please")
    await channel.send(embed=embed)

I don't understand this error, and if someone had this error, ty for help :)


回答1:


discord.py v1.5 introduces intents

An intent basically allows a bot to subscribe into specific buckets of events.

In order to be able to get server data you need to:

  • Enable Server Member intents in your application

  • And Implement discord.Intents at the beginning of your code.

import discord

intents = discord.Intents.all()


bot = discord.Client(intents=intents)

# or 

from discord.ext import commands
bot = commands.Bot(command_prefix = ".", intents=intents)


For more information, read about Intents | documentation




回答2:


You are trying to send into a undefined channel. You can use discord.utils.get to obtain the channel for the member.guild object.

Keep in mind you have to replace WELCOME_CHANNEL_NAME with the target channel.

@bot.event
async def on_member_join(member):
    embed = discord.Embed(
        title=f"Welcome To Mafia Server, Be carefully or you will die", color=random.randint(0, 0xFFFFFF))
    channel = discord.utils.get(member.guild.channels, name='WELCOME_CHANNEL_NAME')
    print("please")
    await channel.send(embed=embed)


来源:https://stackoverflow.com/questions/64425548/my-function-on-member-joinmember-is-never-called

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!