首页 > 解决方案 > 我正在处理 discord.py 但我的 on_member_removed(member) 什么也没做

问题描述

我已经添加了所有必需的东西,例如意图和网关,但我的机器人只是没有反应,我放置了一个调试打印命令,以便当有人离开它时打印“机器人检测到”+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')

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

标签: pythondiscordbotsmember

解决方案


您需要更改您的代码,如果您使用的是 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)


推荐阅读