簡體   English   中英

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

[英]How to work with on_member_join and on_member_remove in discord.py

我剛開始使用on_member_join並發現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")

那么是我的代碼出錯了還是其他地方出了問題?

discord.py 1.5.0 現在支持 discord API 的特權網關意圖。 為了能夠利用服務器數據,您需要:

在您的 Discord 應用程序中啟用 Presence Intent 和 Server Members Intent: https : //i.stack.imgur.com/h6IE4.png

在代碼開頭使用 discord.Intents:

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM