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

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

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

问题

以下是您的翻译:

  1. 我一直在使用 discord.py v2.2.3 Python v3.11.3 制作一个 Discord 机器人最近我添加了一个清理命令在我的测试服务器中加载后一切正常但现在当我尝试在其他服务器中使用它时可能只能工作一两次但现在它只是抛出错误消息我已经检查过了并观看了一些教程但似乎无法搞定
  2. 当我使用该命令时显然意图是清理消息如前所述在我的测试服务器中可以正常工作但在其他服务器中却无法正常工作这非常奇怪请注意我对 Python 是新手所以我预计会很早遇到一些错误并且很乐意努力修复它们以下是我的清理消息方法
  3. ```python
  4. async def clean_message(self, interaction: Interaction, amount: int, check: Callable) -> Any:
  5. if isinstance((channel := interaction.channel), (CategoryChannel, ForumChannel, PartialMessageable)):
  6. return
  7. assert channel is not None
  8. try:
  9. msgs = [
  10. m async for m in channel.history(
  11. limit=300,
  12. before=Object(id=interaction.id),
  13. after=None
  14. ) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=365))) <= m.created_at # 默认 14
  15. ][:amount]
  16. await channel.delete_messages(msgs)
  17. except Exception as e:
  18. msg = await self.bot.error(
  19. f"对不起,我无法清理 **{channel}** 中的消息!", interaction
  20. )
  21. if msg:
  22. await msg.delete(delay=5)
  23. else:
  24. if len(msgs) < 1:
  25. msg = await self.bot.error(
  26. f"在 **{channel}** 中未找到任何消息!", interaction
  27. )
  28. if msg:
  29. await msg.delete(delay=5)
  30. else:
  31. msg = await self.bot.success(
  32. f"成功清理了 **{len(msgs)}** 条来自 **{channel}** 的消息!", interaction
  33. )
  34. if msg:
  35. await msg.delete(delay=5)

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

  1. @app_commands.command(
  2. name='purge',
  3. description="清理频道中的消息"
  4. )
  5. @app_commands.default_permissions(manage_messages=True)
  6. @app_commands.describe(
  7. amount='要清理的消息数量(默认:20)',
  8. user='仅清理特定用户的消息',
  9. content='仅清理特定内容的消息'
  10. )
  11. async def purge_command(self, interaction: Interaction, amount: Optional[int], user: Optional[User], content: Optional[str]):
  12. if not amount:
  13. amount = 20
  14. if amount < 1:
  15. return await self.bot.error("无法清理消息!数量太少!", interaction)
  16. if amount > 150:
  17. return await self.bot.error("无法清理消息!数量太多!", interaction)
  18. if user == None and content == None:
  19. def check(x): return x.pinned == False
  20. else:
  21. if user != None and content != None:
  22. def check(x): return x.author.id == user.id and x.content.lower(
  23. ) == content.lower() and x.pinned == False
  24. elif user != None and content == None:
  25. def check(x): return x.author.id == user.id and x.pinned == False
  26. else:
  27. assert content is not None
  28. def check(x): return x.conetent.lower(
  29. ) == content.lower() and x.pinned == False
  30. await interaction.response.defer()
  31. await self.clean_message(
  32. interaction=interaction,
  33. amount=amount,
  34. check=check
  35. )

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

英文:

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.

  1. async def clean_message(self, interaction: Interaction, amount: int, check: Callable) -&gt; Any:
  2. if isinstance((channel := interaction.channel), (CategoryChannel, ForumChannel, PartialMessageable)):
  3. return
  4. assert channel is not None
  5. try:
  6. msgs = [
  7. m async for m in channel.history(
  8. limit=300,
  9. before=Object(id=interaction.id),
  10. after=None
  11. ) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=365))) &lt;= m.created_at # default 14
  12. ][:amount]
  13. await channel.delete_messages(msgs)
  14. except Exception as e:
  15. msg = await self.bot.error(
  16. f&quot;I&#39;m sorry, I am unable to purge messages in **{channel}**!&quot;, interaction
  17. )
  18. if msg:
  19. await msg.delete(delay=5)
  20. else:
  21. if len(msgs) &lt; 1:
  22. msg = await self.bot.error(
  23. f&quot;No messages found in **{channel}**!&quot;, interaction
  24. )
  25. if msg:
  26. await msg.delete(delay=5)
  27. else:
  28. msg = await self.bot.success(
  29. f&quot;Succesfully purged **{len(msgs)}** messages from **{channel}**!&quot;, interaction
  30. )
  31. if msg:
  32. await msg.delete(delay=5)

Which is called by the purge command here.

  1. @app_commands.command(
  2. name=&#39;purge&#39;,
  3. description=&quot;Purges messages in channel&quot;
  4. )
  5. @app_commands.default_permissions(manage_messages=True)
  6. @app_commands.describe(
  7. amount=&#39;Amount of messages to purge (Default: 20)&#39;,
  8. user=&#39;Only purge messages by user&#39;,
  9. content=&#39;Only purge messages by content&#39;
  10. )
  11. async def purge_command(self, interaction: Interaction, amount: Optional[int], user: Optional[User], content: Optional[str]):
  12. if not amount:
  13. amount = 20
  14. if amount &lt; 1:
  15. return await self.bot.error(&quot;Can&#39;t purge messages! Amount too small!&quot;, interaction)
  16. if amount &gt; 150:
  17. return await self.bot.error(&quot;Can&#39;t purge messages! Amount too Large!&quot;, interaction)
  18. if user == None and content == None:
  19. def check(x): return x.pinned == False
  20. else:
  21. if user != None and content != None:
  22. def check(x): return x.author.id == user.id and x.content.lower(
  23. ) == content.lower() and x.pinned == False
  24. elif user != None and content == None:
  25. def check(x): return x.author.id == user.id and x.pinned == False
  26. else:
  27. assert content is not None
  28. def check(x): return x.conetent.lower(
  29. ) == content.lower() and x.pinned == False
  30. await interaction.response.defer()
  31. await self.clean_message(
  32. interaction=interaction,
  33. amount=amount,
  34. check=check
  35. )

答案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.

  1. try:
  2. msgs = [
  3. m async for m in channel.history(
  4. limit=30000,
  5. before=Object(id=interaction.id),
  6. after=None
  7. ) if check(m) == True and UTC.localize((datetime.now() - timedelta(days=14))) &lt;= m.created_at # default 14
  8. ][:amount]
  9. 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:

确定