有没有办法检查固定的消息,并且只使用 discord.py 清除某些成员的消息?
问题描述
我想制作一个类似于 Dyne 的清除命令,您可以在其中输入一个用户,它不会清除固定的,只有用户的消息(如果您输入一个用户).我试过做一个单独的检查功能,但它不会清除任何东西.我没有收到任何错误,只是不会清除.
I want to make a purge command similar to Dyne's, where you can input a user, and it doesn't purge pinned, only the user's messages (if you input a user). I've tried making a seperate check function, but it doesn't purge anything. I get no error, it just won't purge.
@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 正常工作?
I have manage messages permissions on the server. Does anyone know how to make the check_func work properly?
解决方案
我所做的更改应该可以解决您的问题以及其他一些问题.
The changes I have made should solve your issue as well as deal with a few other issues.
@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 存储在变量中,但我认为这是可以原谅的.
Just changing the function based on the arguments looks better and is more efficient as otherwise you have duplicated code. Furthermore, the message you send at the end was being deleted effectively immediately. channel.send
has an argument delete_after
which will automatically deletes a message after a given amount of seconds. There are a few other syntax issue I haven't mentioned such as the check function only taking one argument but you parsing two which I have also fixed. Technically PEP8 prohibits storing lambda's in variables but I think this could be excused.
编辑:您可以执行类似检查 num == "*"
之类的操作,如果是则删除所有消息.
Edit: you could do something like check if num == "*"
and delete all messages if it does.
相关文章