首页 > 解决方案 > discord.py on_member_join() 不起作用

问题描述

事先:我已经尝试了很多在堆栈溢出时可用的潜在修复。可悲的是,他们都没有工作。

这是代码:

import discord
import asyncio
import datetime
from discord.ext import commands

bot = commands.Bot(command_prefix='!', case_insensitive=True)

@bot.event
async def on_ready():
    print('bot is online')
    return await bot.change_presence(activity=discord.Activity(type=2, name='bla'))

@bot.event
async def on_member_join(member):
    embed = discord.Embed(colour=0x95efcc, description=f"Welcome! You are the {len(list(member.guild.members))} Member!"),

    embed.set_thumbnail(url=f"{member.avatar_url}")

    embed.set_author(name=f"{member.name}", url=f"{member.avatar_url}", icon_url=f"{member.avatar_url}")

    embed.set_footer(text=f"{member.guild}", icon_url=f"{member.guild.icon_url}")

    embed.timestemp = datetime.datetime.utcnow()

    channel = bot.get_channel(id=012391238123)

    await channel.send(embed=embed)

bot.run('Token')

机器人登录但不会执行 on_member_join。有人知道可能出了什么问题吗?on_message 工作正常。

intents = discord.Intents.all()
client = discord.Client(intents=intents)

没有帮助,并且在不和谐的开发人员中也对其进行了检查(服务器成员意图)。该机器人还具有管理员权限

问候爱德华


简而言之,解决方案:

imports
intents = discord.Intents(messages=True, guilds=True, members=True)
bot = commands.Bot(command_prefix='!', intents=intents, case_insensitive=True)

@bot.event
async def on_member_join(member):
    embed = discord.Embed(colour=0x95efcc, description=f"Welcome to my discord server! You are the {len(list(member.guild.members))} member!")
channel = bot.get_channel(id=12931203123)

await channel.send(embed=embed)

bot.run('token')

标签: pythonpython-3.xdiscordbotsdiscord.py

解决方案


啊,原来如此。由于您正在使用commands.Bot, thebot.get_channel不再是一个函数(因为它不再是discord.Client)。尝试member.guild.get_channel改用。

文档


推荐阅读