【问题标题】:How do I make my bots snipe get messages per server instead of getting all the servers?如何让我的机器人狙击每台服务器获取消息而不是获取所有服务器?
【发布时间】:2021-06-03 07:02:03
【问题描述】:

好的,所以当我删除一条消息,然后执行狙击命令时,它会从不同的服务器获取一条消息。所以,我将删除 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))

【问题讨论】:

  • 我投票结束这个问题,因为这是一个愿望清单,你希望我们为你编写整个逻辑,这根本不是一个编程问题。
  • 啊,好吧,我没有意识到,谢谢。 @ŁukaszKwieciński,我应该删除它吗?
  • 如果您正在寻找答案,则不应删除问题。在 StackOverflow 上,即使您提出的问题没有很好地表述,很多人仍然会尝试帮助您解决编程问题。
  • 您可以随时编辑您的问题。它使您有更多机会获得有意义的答案;)
  • 好的,谢谢!!!!!!!!!

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


【解决方案1】:

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

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

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 变量的数据。

【讨论】:

  • 哇!我同意变量很长......但他们工作如此¯_(ツ)_/¯工作!太棒了!我这样做的唯一想法是一遍又一遍地附加到一个json......但这会非常慢。谢谢!!!
猜你喜欢
  • 2021-07-09
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2021-03-19
  • 2021-02-13
  • 2022-01-10
  • 2014-05-12
  • 2023-04-08
相关资源
最近更新 更多