如何让我的机器人狙击每台服务器获取消息而不是获取所有服务器?

时间:2021-03-04 15:39:47

标签: python python-3.x discord discord.py python-3.7

好的,所以当我删除一条消息,然后执行 snipe 命令时,它会从不同的服务器获取一条消息。因此,我将删除 Hello,然后执行命令,它将从完全不同的服务器向我发送最近删除的消息。我怎样才能解决这个问题?有什么想法吗?

import discord
from discord.ext import commands
import asyncio

class Moderation(commands.Cog):
    def __init__(self, client):
        self.client = client


    snipe_message_content = None
    snipe_message_author = None
    snipe_message_id = None

    @commands.Cog.listener()
    async def on_message_delete(self, message):

        global snipe_message_content
        global snipe_message_author
        global snipe_message_id

        snipe_message_content = message.content
        snipe_message_author = message.author
        snipe_message_id = message.id
        await asyncio.sleep(60)

        if message.id == snipe_message_id:
            snipe_message_author = None
            snipe_message_content = None
            snipe_message_id = None


    @commands.command()
    async def snipe(self, message):
        if snipe_message_content==None:
            await message.channel.send("Theres nothing to snipe.")
        else:
            embed = discord.Embed(description=f"{snipe_message_content}")
            embed.set_footer(text=f"Asked by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
            embed.set_author(name= f"{snipe_message_author}")
            await message.channel.send(embed=embed,delete_after=5)
            return


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

1 个答案:

答案 0 :(得分:0)

您可以使用字典来存储在每个服务器中删除的消息。因此,我建议重新定义您当前的代码,使其与字典兼容。

这里是我如何定义一个简单的“snipe”命令:

class Moderation(commands.Cog):
    def __init__(self, bot):
        self.bot = bot
        self._last_member = None
        self.snipe_dict = {}
    
    # snipe
    @commands.Cog.listener()
    async def on_message_delete(self, message):
      if message.author.bot:
        return
      self.last_deleted = message.content
      self.deleted_author = message.author

    @commands.Cog.listener()
    async def on_message_delete(self, message):
        id = message.channel

        msg = str(message.content) + '˙' + str(message.author) + ''

        if message.attachments:
            r = str(message.attachments)
            p, thing = r.split('url=')
            thing, fdsdf = thing.split('>')

            msg = 'Sent an image. Not snipable.˙' + str(message.author) + ''

        self.snipe_dict[id] = msg

    @commands.command(name='snipe')
    async def sniped(self, ctx):
        id = ctx.message.channel
        wow = self.snipe_dict[id]
        if self.snipe_dict[id] == 'Pyth0nC0de':
            await ctx.send('There\'s nothing to snipe!')
        else:
            wow, message = wow.split('˙')
            embed = discord.Embed(title='Deleted Message', color=random.randint(100, 9999))
            embed.add_field(name=message, value=wow, inline=False)
            await ctx.send(embed=embed)
            self.snipe_dict[id] = 'Pyth0nC0de'

            print(self.snipe_dict)

您也许应该将变量重命名为更有意义的名称,但命令本身运行良好。

另请注意,如果您重新启动机器人,字典将重置为空字典。如果您想在重新启动机器人后保存截取的消息,您可以使用数据库来存储 self.sniped_dict 变量中的数据。