如何在discord.py中使用on_member_join和on_member_remove

时间:2020-10-18 13:20:52

标签: python discord discord.py

我刚从discord.py开始,发现on_member_joinon_member_remove对我不起作用。请记住,on_ready函数可以很好地工作。

import discord
from discord.ext import commands

bot = commands.Bot(command_prefix = "!")

@bot.event
async def on_ready():
    print("""Bot ready""")

@bot.event
async def on_member_join(member):
    channel = bot.get_channel(767370104814960640)
    await channel.send(f"{member} has joined the server")

@bot.event
async def on_member_remove(member):
    channel = bot.get_channel(766620766237753345)
    await channel.send(f"{member} has left the server")

bot.run("my token")

那么我是在我的代码上犯了错误还是其他地方出错了?

1 个答案:

答案 0 :(得分:0)

discord.py 1.5.0现在支持discord API的Privileged Gateway Intent。为了能够利用服务器数据,您需要:

在不一致的应用程序中启用状态意图和服务器成员意图: https://i.stack.imgur.com/h6IE4.png

在代码开头使用discord.Intent:

intents = discord.Intents.all()

#If you use commands.Bot()
bot = commands.Bot(command_prefix="!", intents=intents)

#If you use discord.Client()
client = discord.Client(intents=intents)