Discord 狙击命令不狙击消息

Discord 狙击命令不狙击消息,discord,discord.py,Discord,Discord.py,我找到了一个发送最新删除消息的命令。在测试时,我发现当我删除一条消息时,它会向控制台发送一个回溯错误。这是回溯错误和代码 正在忽略on_消息\u delete中的异常 回溯(最近一次呼叫最后一次): 文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/client.py”,第343行,在运行事件中 等待coro(*args,**kwargs) 文件“/home/runner/Isla/cogs/awp.py”,第21行,

我找到了一个发送最新删除消息的命令。在测试时,我发现当我删除一条消息时,它会向控制台发送一个回溯错误。这是回溯错误和代码

正在忽略on_消息\u delete中的异常 回溯(最近一次呼叫最后一次): 文件“/opt/virtualenvs/python3/lib/python3.8/site packages/discord/client.py”,第343行,在运行事件中 等待coro(*args,**kwargs) 文件“/home/runner/Isla/cogs/awp.py”,第21行,在on_消息中\u delete 如果msg.author.client: AttributeError:“成员”对象没有属性“客户端”

import discord
from discord.ext import commands 
import datetime
import editdistance
import re



invitere = r"(?:https?:\/\/)?discord(?:\.gg|app\.com\/invite)?\/(?:#\/)([a-zA-Z0-9-]*)"

invitere2 = r"(http[s]?:\/\/)*discord((app\.com\/invite)|(\.gg))\/(invite\/)?(#\/)?([A-Za-z0-9\-]+)(\/)?"

class Awp(commands.Cog):

    def __init__(self, client):
        self.client = client
        self.snipes = {}
    
        @client.listen('on_message_delete')
        async def on_message_delete(msg):
            if msg.author.client:
                return
            self.snipes[msg.channel.id] = msg

        @client.listen('on_message_edit')
        async def on_message_edit(before, after):
            if before.author.client or after.author.client:
                return  # DEPARTMENT OF REDUNDANCY DEPARTMENT
            if (editdistance.eval(before.content, after.content) >= 10) and (
                    len(before.content) > len(after.content)):
                self.snipes[before.channel.id] = [before, after]
    @commands.Cog.listener()
    async def on_ready(self):
      print('Gifs bot is online.')

    def sanitise(self, string):
        if len(string) > 1024:
            string = string[0:1021] + "..."
        string = re.sub(invitere2, '[INVITE REDACTED]', string)
        return string

    @commands.command()
    async def awp(self, ctx):
        '"Snipes" someone\'s message that\'s been edited or deleted.'
        try:
            snipe = self.snipes[ctx.channel.id]
        except KeyError:
            return await ctx.send('No snipes in this channel!')
        if snipe is None:
            return await ctx.send('No snipes in this channel!')
        # there's gonna be a snipe after this point
        emb = discord.Embed()
        if type(snipe) == list:  # edit snipe
            emb.set_author(
                name=str(snipe[0].author),
                icon_url=snipe[0].author.avatar_url)
            emb.colour = snipe[0].author.colour
            emb.add_field(
                name='Before',
                value=self.sanitise(snipe[0].content),
                inline=False)
            emb.add_field(
                name='After',
                value=self.sanitise(snipe[1].content),
                inline=False)
            emb.timestamp = snipe[0].created_at
        else:  # delete snipe
            emb.set_author(
                name=str(snipe.author),
                icon_url=snipe.author.avatar_url)
            emb.description = self.sanitise(snipe.content)
            emb.colour = snipe.author.colour
            emb.timestamp = snipe.created_at
        emb.set_footer(
            text=f'Message sniped by {str(ctx.author)}',
            icon_url=ctx.author.avatar_url)
        await ctx.send(embed=emb)
        self.snipes[ctx.channel.id] = None


def setup(client):
    client.add_cog(Awp(client))



您只需将
message.author.client
更改为
message.author.bot
,无论您使用的是客户端还是bot


参考资料:


非常感谢您!!