簡體   English   中英

Python Discord on_member_join

[英]Python Discord on_member_join

請原諒我的英語不好:-)

我有一個問題,我使用的 Discord 機器人在成員加入后不會向他們發送私人消息。 我嘗試了其他 Stack Overflow 帖子中建議的幾種解決方案,但它不起作用。

import discord

client = discord.Client()

@client.event
async def on_member_join(member):
    print(f'Someone joined')
    await member.send("You joined")

client.run("XXX")

但該函數永遠不會執行。 如果我在像?join這樣的命令中使用完全相同的代碼,如下例所示,它可以工作!

import discord
from discord.ext import commands

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

@client.command()
async def join(ctx):
    member = ctx.message.author
    print(f'Someone joined')
    await member.send("You joined")

client.run("XXX")

那么我認為on_member_join不再起作用是錯誤的嗎? 我究竟做錯了什么?

謝謝你的時間 :-)

從 discord.py 1.5+ 版本開始,bot 必須在使用前聲明桶類型事件的Intents 事件on_member_join需求意向discord.Intents.members設置為True

一個簡單的方法是:

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

discord.py 1.5 版聲明,如果未指定意圖,則它允許除Intents.membersIntents.presences之外的所有意圖。

可以在此處找到有關 Intent 的更多信息: discord.py 中的 Gateway Intents

暫無
暫無

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

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