全局变量与其他服务器混合

时间:2020-11-07 02:29:52

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

我目前正在对Discord Bot进行狙击命令。但是,当我在服务器1(示例)上侦听该消息,并且在另一台服务器上执行该命令时,它仍然会说服务器1上的被剪切的消息。如何防止这种情况发生?我希望它不会在频道之间混在一起。

代码:

class utility(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.snipe_message_content = None
        self.snipe_message_author = None
        self.snipe_message_id = None

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

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

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

    @commands.command()
    async def snipe(self, message):
        if self.snipe_message_content==None:
            await message.channel.send("It seems like there are nothing to snipe with!")
        else:
            embed = discord.Embed(description=f"{self.snipe_message_content}", colour=discord.Color.dark_green())
            embed.set_footer(text=f"Requested by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
            embed.set_author(name= f"<@{self.snipe_message_author}>")
            await message.channel.send(embed=embed)
            return

谢谢!

2 个答案:

答案 0 :(得分:0)

您要使用字典

类似这样的东西:

class utility(commands.Cog):
    def __init__(self, client):
        self.client = client
        self.sniped_messages = {}

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

        server_id = message.guild.id
        self.sniped_messages[server_id] = {"content": message.content,
                                           "author": message.author.id}
        # * OR USE A TUPLE *
        # self.sniped_messages[server_id] = (message.content, message.author.id)

        await asyncio.sleep(60)

        if message.id == self.sniped_messages[server_id]["id"]:
            self.sniped_messages.pop(server_id)

    @commands.command()
    async def snipe(self, message):
        if message.guild.id not in self.sniped_messages:
            await message.channel.send("It seems like there are nothing to snipe with!")
        else:
            sniped_message = self.sniped_messages[message.guild.id]
            embed = discord.Embed(description=f"{sniped_message['content']}", colour=discord.Color.dark_green())
            embed.set_footer(text=f"Requested by {message.author.name}#{message.author.discriminator}", icon_url=message.author.avatar_url)
            embed.set_author(name= f"<@{sniped_message['author']}>")
            await message.channel.send(embed=embed)
            return

请告诉我是否有任何错误,希望对您有所帮助:)

答案 1 :(得分:0)

齿轮现在具有一个用于已删除消息的变量。因此,如果您在另一台服务器上运行该命令,则只需检查该变量即可。 相反,您应该保留一个词典,以便键将是服务器,值将是已删除的消息。

更改此设置时,每次检查已剪切的消息时,首先需要获取服务器的ID,然后以这种方式从字典中获取消息。而且,当您要存储它时,您将希望获取服务器的ID,以将其插入到该键下的字典中。