英文:
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) -> 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 # default 14
][:amount]
await channel.delete_messages(msgs)
except Exception as e:
msg = await self.bot.error(
f"I'm sorry, I am unable to purge messages in **{channel}**!", interaction
)
if msg:
await msg.delete(delay=5)
else:
if len(msgs) < 1:
msg = await self.bot.error(
f"No messages found in **{channel}**!", interaction
)
if msg:
await msg.delete(delay=5)
else:
msg = await self.bot.success(
f"Succesfully purged **{len(msgs)}** messages from **{channel}**!", interaction
)
if msg:
await msg.delete(delay=5)
Which is called by the purge command here.
@app_commands.command(
name='purge',
description="Purges messages in channel"
)
@app_commands.default_permissions(manage_messages=True)
@app_commands.describe(
amount='Amount of messages to purge (Default: 20)',
user='Only purge messages by user',
content='Only purge messages by 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("Can't purge messages! Amount too small!", interaction)
if amount > 150:
return await self.bot.error("Can't purge messages! Amount too Large!", 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))) <= m.created_at # default 14
][:amount]
await channel.delete_messages(msgs)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论