我已尝试过3次执行此代码,我的计划是从用户中删除所有角色,将角色写入ctx.send()
中,以发送消息,并在频道中包含旧角色,然后发送一条消息,指出用户被监禁,并说明原因并赋予囚犯角色。
“ BabaYaga”是adm的角色; “ D 001”是囚犯角色
代码01:
# Detentos 3
@client.command()
@commands.has_any_role('BABAYAGA')
async def det(ctx, member: discord.Member = None, role = discord.Guild.roles, *, reason = None):
if member == None:
await ctx.send('Say the user')
return
if reason == None:
await ctx.send('Say the reason')
return
Roles = member.roles
for _ in Roles:
print(Roles)
await client.remove_roles(member, *Roles)
await member.add_role(ctx, member, role)
await ctx.send(f'{member} was arrested for {reason}')
代码02:
# Detentos 2
@client.command()
@commands.has_any_role('BABAYAGA')
async def det(ctx, member: discord.Member, *,reason):
role = discord.utils.get(ctx.guild.roles, name = 'D 001')
await ctx.send(f'{member.roles}')
for _ in member.roles:
await member.remove_roles(member.top_role)
await ctx.message.add_reaction(emoji=self.tick)
await member.add_roles(role)
await ctx.send(f'{member} was arrested for {reason}')
代码03:
# Detentos
@client.command()
@commands.has_any_role('BABAYAGA')
async def det(self, ctx, member: discord.Member, *, reason = None):
if reason == None:
await ctx.send('Say the reason!')
return
roles = discord.utils.get(member.guild.roles) # member's roles
role = discord.utils.get(ctx.guild.roles, name = 'D 001') # Det's role
await ctx.message.add_reaction(emoji=self.tick)
await member.edit(member.guild.roles)
await ctx.send(f'{discord.Member} preso por {reason}')
await ctx.send(f'cargos do {discord.Member}: {member.roles}')
代码04:
# Detentos
@client.command()
@commands.has_any_role('BABAYAGA')
async def det(self, ctx, member: discord.Member = None, *, reason = None):
if reason == None:
await ctx.send('Say the reason! :angry: :angry:')
return
if member == None:
await ctx.send('Say the user')
return
rolesserver = ['D 001', 'D 002', 'D 003', 'D 004', 'testers']
await ctx.send(f'{member.roles}')
for roles in rolesserver:
await client.remove_roles(member, *roles)
await ctx.send(f'<@{member.id}> was arrested for {reason}')
我不知道我的错误是什么。谁能帮我吗?
答案 0 :(得分:0)
您的代码段中有很多错误。这是正确的做法:
@client.command()
@commands.has_any_role('BABAYAGA')
async def det(ctx, member: discord.Member, *, reason):
await ctx.send(f'Removed ", ".join([role.name for role in member.roles])')
for role in member.roles:
await member.remove_roles(role)
await ctx.message.add_reaction(emoji=self.tick)
role = discord.utils.get(ctx.guild.roles, name = 'D 001')
await member.add_roles(role)
await ctx.send(f'{member} was arrested for {reason}')
一些建议:
get
方法从一个可迭代对象返回单个项目,因此编写get(member.guild.roles)
无效,您只需键入roles = member.guild.roles
for
循环中设置一个变量,否则,您将无法循环浏览成员的角色,因此您的for _ in member.roles
必须成为for role in member.roles
。join
方法与list comprehension(例如', '.join([role.name for role in member.roles])
)结合使用remove_roles
是一种discord.Member
方法,因此您必须以这种方式使用它:member.remove_roles(role)