我现在正在尝试制作Discord机器人,并且正在遵循有关踢/禁止成员的教程,但是我似乎找不到找到踢/禁止所有人的方法。当前代码会踢出特定用户:
async def kickall(ctx, member : discord.Member):
await member.kick()
我如何让它踢给所有人?谢谢。如果您有任何疑问或需要改善的方法,请随时问/告诉我。
答案 0 :(得分:3)
您可以获得公会的成员,并通过for
循环遍历他们:
@commands.command(aliases=['kickall'])
@commands.has_permissions(administrator=True)
async def kick_all(self, ctx, reason):
for member in ctx.guild.members:
try:
await member.kick(reason=reason)
print(f"Kicked {member.name}")
except:
print(f"Could not kick {member}")
由于您不在Class
中,因此您将按照以下方式进行操作:
@client.command()
async def kickall(ctx, *, reason=None):
if ctx.message.author.top_role.permissions.administrator:
for member in ctx.guild.members:
try:
await member.kick(reason=reason)
print(f"Kicked {member.name}")
except:
print(f"Could not kick {member}")
else:
await ctx.send("Missing permissions.")