python - 有没有办法检查固定的消息,并且只使用 discord.py 清除某些成员的消息?
问题描述
我想制作一个类似于 Dyne 的清除命令,您可以在其中输入用户,并且它不会清除固定的,只有用户的消息(如果您输入用户)。我试过做一个单独的检查功能,但它不会清除任何东西。我没有错误,它只是不会清除。
@commands.command()
@commands.has_permissions(manage_messages=True)
async def purge(self, ctx, user: discord.Member = None, num: int = 10000):
if user:
def check_func(user: discord.Member, message: discord.Message):
return not msg.pinned
return user.id
await ctx.message.delete()
await ctx.channel.purge(limit=num, check=check_func)
verycool = await ctx.send(f'{num} messages deleted.')
await verycool.delete()
else:
await ctx.message.delete()
await ctx.channel.purge(limit=num, check=lambda msg: not msg.pinned)
verycool = await ctx.send(f'{num} messages deleted.')
await verycool.delete()
我有管理服务器上的消息权限。有谁知道如何使 check_func 正常工作?
解决方案
我所做的更改应该可以解决您的问题以及处理其他一些问题。
@commands.command()
@commands.has_permissions(manage_messages=True)
async def purge(self, ctx, num: int = None, user: discord.Member = None):
if user:
check_func = lambda msg: msg.author == user and not msg.pinned
else:
check_func = lambda msg: not msg.pinned
await ctx.message.delete()
await ctx.channel.purge(limit=num, check=check_func)
await ctx.send(f'{num} messages deleted.', delete_after=5)
仅根据参数更改函数看起来更好,效率更高,否则您会重复代码。此外,您最后发送的消息立即被有效删除。channel.send
有一个参数delete_after
会在给定的秒数后自动删除一条消息。还有一些我没有提到的其他语法问题,比如 check 函数只接受一个参数,但你解析了两个我也修复了。从技术上讲,PEP8 禁止将 lambda 存储在变量中,但我认为这可以原谅。
编辑:你可以做一些检查,如果是num == "*"
,则删除所有消息。
推荐阅读
- python - 为什么在 .png 格式的图像中检测到彩色像素,但在 .jpg 格式的图像中检测不到?
- python - I have a script that runs forever, how can I run it at startup in the background on macos?
- eslint - 如何解决更漂亮/更漂亮的问题
- import - 使用 @mapbox 而不是 'mapbox-gl' 来导入 mapbox
- excel - 相对路径与绝对路径
- php - 重新索引数组并添加总计
- reactjs - React Material Treeview,使用扩展属性时单击时树节点未扩展
- c# - 瀑布对话框中的主动消息 Microsoft Bot Framework
- mysql - MySQL:构建通知消息系统,使其部分可以点击
- google-apps-script - 无法将 Apps 脚本项目切换到新的云平台项目?