我正在处理discord.py,但是我的on_member_removedmember什么也不做

问题描述

我已经添加了所有必需的内容,例如意图和网关,但是我的机器人只是不响应,我放了一条调试打印命令,以便当有人离开时打印“机器人已检测到“ + member.mention +”已经离开服务器”,但无论我做什么,实际上都无济于事。

import discord

from discord.ext.commands import has_permissions
import asyncio
import logging

intents = discord.Intents.default()
intents.typing = True
intents.presences = True
intents.members = True

def replace_line(file_name,line_num,text):
    with open(file_name,'r') as file:
        data = file.readlines()
    print(data)
        
    data[line_num] = text
    
    with open(file_name,'w') as file:
        file.writelines( data )

    print(data)


def leavechannel(serverid,channel):
    flc = open("leavechannels.txt","r")
    checker = (flc.read())
    flc.close
    
    x = checker.find(serverid)
    print (x)
    if x >= 0:
        lookup = serverid

        with open("leavechannels.txt") as myFile:
            for num,line in enumerate(myFile,0):
                if lookup in line:
                    print (serverid +' found at line:',num)
                    linecache= num
        print(linecache)
        replace_line("leavechannels.txt",linecache,"\n"+serverid + " = " + channel)
    else:
        flc = open("leavechannels.txt",'a+')
        flc.write(+serverid + " = " + channel)
        
        
client = discord.Client()

prefix = "ez!"

@client.event
async def on_ready():
    print('We have logged in as {0.user}'.format(client))

@client.event
async def on_member_remove(member):
    print("recognised that "+member.mention+" has left")
    with open("leavechannels.txt") as myFile:
        for item in myFile.split("\n"):
            if member.server.id in item:
                leavechannelcache = item.strip()    
    embedVar= discord.Embed(title= member.mention + " has left :(",description="Come back plox!",color=12151512)
    await discord.Object(id=leavechannelcache).send(embed=embedVar)
    print("Sent message to #CHANNEL")



@client.event
async def on_message(message):
    if message.author == client.user:
        return

    if message.content.startswith(prefix+'leavechannel'):
        if message.author.guild_permissions.manage_channels or message.author.guild_permissions.administration:
            leavechannelhash = message.content.replace(prefix+'leavechannel ','')
            print (leavechannelhash)
            await message.channel.send("Server leave channel has been set to "+leavechannelhash)
            leavechannel(str(message.guild.id),str(leavechannelhash))
            
                  
        else:
            await message.channel.send("Sorry,"+message.author+"; You do not have sufficient Permissions to do this.")
            

    
client.run('token')

我省略了很多内容,因为这是不必要的(主要是与帮助和hello world fill命令相关的内容),但是主要的问题是我有intents.members,并且已经在discord应用程序中激活了它。面板,但是即使这样做,当有人离开服务器时,它甚至都没有给我提供DEBUG打印,这显然表明在检测到成员离开时出现了问题。您可以提出任何建议吗?

解决方法

您需要更改代码,如果您使用的是dpy 1.5.1,则有意向更新,请在代码中使用它。

from discord import Intents
from discord.ext.commands import Bot

intent = Intents().all()

bot = Bot(command_prefix='prefix',intents=intent)

... # Some code under this line

并尝试使用Intents().all吗? 在您的情况下,请像这样discord.Client(intents=intent)

相关问答

Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其...
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。...
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbc...