【发布时间】:2021-08-05 05:36:21
【问题描述】:
我目前正在尝试在 python 中为我的不和谐机器人创建 2 个命令,一个静音和取消静音命令。该命令应该删除用户的所有角色,然后添加“静音”角色。在此之后,当我们使用 unmute 命令取消静音用户时,“静音”角色被删除,用户之前的角色被重新分配。我在 Repl.it 中对机器人进行编码,所以如果需要一个数据库,是否可以使用 repl.it 的内置数据库来执行此操作?非常感谢任何回答这个问题的人,我已经挣扎了几个小时。到目前为止,这是我所拥有的:
@commands.command()
@commands.has_permissions(manage_roles = True)
async def mute(self, ctx, member: discord.Member = None, *, reason = None):
if member is None:
embed = discord.Embed(title = "Please provide a user tag.", \
colour = discord.Colour.from_rgb(255, 0, 0))
await ctx.send(embed = embed)
return
everyone = ctx.guild.get_role(795989660920053770)
role = discord.utils.get(ctx.guild.roles, name='Muted')
roles = member.roles
roles.remove(everyone)
if "userRoles" not in db.keys():
db["userRoles"] = roles
else:
userRoles = db["userRoles"]
userRoles.append(roles)
db["userRoles"] = userRoles
await member.remove_roles(*roles)
await member.add_roles(role)
if reason is None:
embed = discord.Embed(title = f"***Muted {member}***", \
colour = discord.Colour.green())
await ctx.send(embed = embed)
else:
embed = discord.Embed(title = f"***Muted {member}***", \
description = f"Reason: {reason}", \
colour = discord.Colour.green())
await ctx.send(embed = embed)
return
@mute.error
async def mute_error(self, ctx, error):
if isinstance(error, MissingPermissions):
embed = discord.Embed(title = "***You don't have the right permissions for that.***", \
colour = discord.Colour.from_rgb(255, 0, 0))
await ctx.send(embed = embed)
return
@commands.command()
@commands.has_permissions(manage_roles = True)
async def unmute(self, ctx, member: discord.Member = None, *, reason = None):
if member is None:
embed = discord.Embed(title = "Please provide a user tag.", \
colour = discord.Colour.from_rgb(255, 0, 0))
await ctx.send(embed = embed)
return
userRoles = f"{member.id}_roles"
await member.remove_roles("Muted")
await member.add_roles(db[userRoles])
if reason is None:
embed = discord.Embed(title = f"***Unmuted {member}***", \
colour = discord.Colour.green())
await ctx.send(embed = embed)
else:
embed = discord.Embed(title = f"***Unmuted {member}***", \
description = f"Reason: {reason}", \
colour = discord.Colour.green())
await ctx.send(embed = embed)
return
@unmute.error
async def unmute_error(self, ctx, error):
if isinstance(error, MissingPermissions):
embed = discord.Embed(title = "***You don't have the right permissions for that.***", \
colour = discord.Colour.from_rgb(255, 0, 0))
await ctx.send(embed = embed)```
【问题讨论】:
-
因此,如果您已经战斗了几个小时,请向我们展示您的最佳尝试。我们不会为您编写所有代码。
-
我已经用我目前拥有的静音和取消静音代码编辑了我的问题。很抱歉没有事先包含它。
标签: python discord discord.py bots roles