【问题标题】:I am working on a discord.py but my on_member_removed(member) does nothing我正在处理 discord.py 但我的 on_member_removed(member) 什么也没做
【发布时间】:2021-02-02 23:35:03
【问题描述】:

我已经添加了所有必需的东西,例如意图和网关,但我的机器人只是没有反应,我放了一个调试打印命令,这样当有人离开时它会打印“机器人检测到“+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 填充命令相关的内容),但手头的主要问题是我有 intents.members 并且我已经在不和谐的应用程序中激活了它面板,但即使这样做了,当有人离开服务器时,它甚至没有给我一个 DEBUG 打印,这显然表明它对成员离开的检测有问题。您可以提出任何建议吗?

【问题讨论】:

    标签: python discord bots member


    【解决方案1】:

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

    【讨论】:

    • 尝试通过不和谐联系我? Cedric#4630
    猜你喜欢
    • 1970-01-01
    • 2021-07-07
    • 1970-01-01
    • 1970-01-01
    • 2019-03-30
    • 2015-12-09
    • 2020-10-09
    • 2012-07-14
    • 1970-01-01
    相关资源
    最近更新 更多