Why does my Python 3.x discord.py bot's purge command work in my test server but not in other servers?

huangapple go评论65阅读模式
英文:

Why does my Python 3.x discord.py bot's purge command work in my test server but not in other servers?

问题

以下是您的翻译:

我一直在使用 discord.py v2.2.3 和 Python v3.11.3 制作一个 Discord 机器人最近我添加了一个清理命令在我的测试服务器中加载后一切正常但现在当我尝试在其他服务器中使用它时可能只能工作一两次但现在它只是抛出错误消息我已经检查过了并观看了一些教程但似乎无法搞定

当我使用该命令时显然意图是清理消息如前所述在我的测试服务器中可以正常工作但在其他服务器中却无法正常工作这非常奇怪请注意我对 Python 是新手所以我预计会很早遇到一些错误并且很乐意努力修复它们以下是我的清理消息方法

```python
async def clean_message(self, interaction: Interaction, amount: int, check: Callable) -> Any:
    if isinstance((channel := interaction.channel), (CategoryChannel, ForumChannel, PartialMessageable)):
        return
    assert channel is not None
    try:
        msgs = [
            m async for m in channel.history(
                limit=300,
                before=Object(id=interaction.id),
                after=None
            ) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=365))) <= m.created_at  # 默认 14
        ][:amount]
        await channel.delete_messages(msgs)
    except Exception as e:
        msg = await self.bot.error(
            f"对不起,我无法清理 **{channel}** 中的消息!", interaction
        )
        if msg:
            await msg.delete(delay=5)
    else:
        if len(msgs) < 1:
            msg = await self.bot.error(
                f"在 **{channel}** 中未找到任何消息!", interaction
            )
            if msg:
                await msg.delete(delay=5)
        else:
            msg = await self.bot.success(
                f"成功清理了 **{len(msgs)}** 条来自 **{channel}** 的消息!", interaction
            )
            if msg:
                await msg.delete(delay=5)

该方法由此处的清理命令调用。

@app_commands.command(
    name='purge',
    description="清理频道中的消息"
)
@app_commands.default_permissions(manage_messages=True)
@app_commands.describe(
    amount='要清理的消息数量(默认:20)',
    user='仅清理特定用户的消息',
    content='仅清理特定内容的消息'
)
async def purge_command(self, interaction: Interaction, amount: Optional[int], user: Optional[User], content: Optional[str]):
    if not amount:
        amount = 20
    if amount < 1:
        return await self.bot.error("无法清理消息!数量太少!", interaction)
    if amount > 150:
        return await self.bot.error("无法清理消息!数量太多!", interaction)

    if user == None and content == None:
        def check(x): return x.pinned == False
    else:
        if user != None and content != None:
            def check(x): return x.author.id == user.id and x.content.lower(
            ) == content.lower() and x.pinned == False
        elif user != None and content == None:
            def check(x): return x.author.id == user.id and x.pinned == False
        else:
            assert content is not None
            def check(x): return x.conetent.lower(
            ) == content.lower() and x.pinned == False
    await interaction.response.defer()
    await self.clean_message(
        interaction=interaction,
        amount=amount,
        check=check
    )

希望这对您有所帮助!如果您有任何其他问题,请随时问。

英文:

I have been making a discord bot using discord.py v2.2.3 & Python v3.11.3 and I recently made a purge command. Once loaded in my test server everything worked fine, now when I try it in my other servers it worked maybe once or twice but now it just throws my error message. I have went over it and watched a few tutorials but I can't seem to get it right.

When i use the command the obvious intent is to purge / clear messages. As stated it works in my test server just not in my other servers which is very odd.. Note i am new to Python, so i did expect to run into bugs fairly early and am excited to work on fixing them. Here is my clean message method.

async def clean_message(self, interaction: Interaction, amount: int, check: Callable) -&gt; Any:
if isinstance((channel := interaction.channel), (CategoryChannel, ForumChannel, PartialMessageable)):
return
assert channel is not None
try:
msgs = [
m async for m in channel.history(
limit=300,
before=Object(id=interaction.id),
after=None
) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=365))) &lt;= m.created_at  # default 14
][:amount]
await channel.delete_messages(msgs)
except Exception as e:
msg = await self.bot.error(
f&quot;I&#39;m sorry, I am unable to purge messages in **{channel}**!&quot;, interaction
)
if msg:
await msg.delete(delay=5)
else:
if len(msgs) &lt; 1:
msg = await self.bot.error(
f&quot;No messages found in **{channel}**!&quot;, interaction
)
if msg:
await msg.delete(delay=5)
else:
msg = await self.bot.success(
f&quot;Succesfully purged **{len(msgs)}** messages from **{channel}**!&quot;, interaction
)
if msg:
await msg.delete(delay=5)

Which is called by the purge command here.

    @app_commands.command(
name=&#39;purge&#39;,
description=&quot;Purges messages in channel&quot;
)
@app_commands.default_permissions(manage_messages=True)
@app_commands.describe(
amount=&#39;Amount of messages to purge (Default: 20)&#39;,
user=&#39;Only purge messages by user&#39;,
content=&#39;Only purge messages by content&#39;
)
async def purge_command(self, interaction: Interaction, amount: Optional[int], user: Optional[User], content: Optional[str]):
if not amount:
amount = 20
if amount &lt; 1:
return await self.bot.error(&quot;Can&#39;t purge messages! Amount too small!&quot;, interaction)
if amount &gt; 150:
return await self.bot.error(&quot;Can&#39;t purge messages! Amount too Large!&quot;, interaction)
if user == None and content == None:
def check(x): return x.pinned == False
else:
if user != None and content != None:
def check(x): return x.author.id == user.id and x.content.lower(
) == content.lower() and x.pinned == False
elif user != None and content == None:
def check(x): return x.author.id == user.id and x.pinned == False
else:
assert content is not None
def check(x): return x.conetent.lower(
) == content.lower() and x.pinned == False
await interaction.response.defer()
await self.clean_message(
interaction=interaction,
amount=amount,
check=check
)

答案1

得分: 1

I had forgotten i was messing with the max purged messages and range limit.
我忘了我在调整最大删除消息和范围限制。

The max amount of purged messages was set to 300 over a 365 day range.
最大删除消息数量设置为在365天范围内为300条。

Simple mistake that took four hours to fix.
一个简单的错误,花了四个小时才修复。

All i had to do was up the message cap to anything more than it was and decrease the range.
我所要做的就是将消息上限增加到超过它的任何值,并缩小范围。

英文:

Okay... so i just realized how dumb i was. I had forgotten i was messing with the max purged messages and range limit. The max amount of purged messages was set to 300 over a 365 day range. Simple mistake that took four hours to fix. All i had to do was up the message cap to anything more than it was and decrease the range.

try:
msgs = [
m async for m in channel.history(
limit=30000,
before=Object(id=interaction.id),
after=None
) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=14))) &lt;= m.created_at  # default 14
][:amount]
await channel.delete_messages(msgs)

huangapple
  • 本文由 发表于 2023年5月26日 13:16:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/76337839.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定