したがって、私が今達成したいのは、コードを見つけて記述し、チャネルをパージするたびに、最初にメッセージが含まれているかどうかを確認することです。そうでない場合は、エラーを送信します。唯一の問題は、クライアントが最初にメッセージがあるかどうかを確認できるかどうかわからないことです。誰かアイデアや例があれば、教えていただければ幸いです。
答えはこれです:
@commands.command()
async def clear(self, ctx, *, limit=100):
await ctx.message.delete()
channel = ctx.channel
messages = await channel.history(limit=123).flatten()
if not messages:
await ctx.channel.send("I am really sorry but I can not purge an empty channel!")
return
else:
try:
await channel.purge(limit = limit)
return
except discord.Forbidden:
return await ctx.channel.send('I do not have perms')
を使用messages = await channel.history(limit=123).flatten()
して、チャネルのメッセージを含むリストを取得できます。制限は、読み戻すメッセージの最大数を指定するために使用されます。
そのリストが空であるかどうかを確認して、チャネルにメッセージがあるかどうかを確認できます。
APIリファレンス:https://discordpy.readthedocs.io/en/latest/api.html?highlight = history#discord.TextChannel.history
この記事はインターネットから収集されたものであり、転載の際にはソースを示してください。
侵害の場合は、連絡してください[email protected]
コメントを追加