我希望我的 discord.py Bot 在一定时间后删除静音角色,这将由运行“静音”命令的版主指定。
我试着在“ctx”之后加上一个参数,这将是指定静音的时间。
@bot.command(name='mute', pass_context=True)
@commands.has_role('mod')
async def mute(ctx, member=None, val: int=None):
ab = member
ab = ab.replace("<","")
ab = ab.replace(">","")
ab = ab.replace("@","")
ab = ab.replace("!","")
member = ctx.guild.get_member(int(ab))
role = discord.utils.get(ctx.guild.roles, name="muted")
await member.add_roles(role)
await ctx.send("muted")
它会引发错误“AttributeError: 'NoneType' object has no attribute 'add_roles''
member
有可能是None
在get_member
之后,如果在公会中找不到用户,那么你必须处理这种情况:
@bot.command(name='mute')
@commands.has_role('mod')
async def mute(ctx, user: discord.User, mute_duration_in_minutes: int=60):
if user is None:
# Do something here to handle user not being present in command.
member = ctx.guild.get_member(user.id)
if member is None:
# Do something here to handle member not being found in the guild.
role = discord.utils.get(ctx.guild.roles, name="muted")
await member.add_roles(role)
await ctx.send("muted")
用户、角色和 mute_duration_in_minutes(或任何名称)之类的情况也是如此。
关于在延迟后再次删除角色,例如,您可以存储有关用户的信息以及当您给他们静音角色时应该何时取消静音,并有一个线程每分钟不断检查需要的用户删除静音角色:
import threading
def check_and_remove_expired_muted_role():
# Retrieve stored users and remove role if needed.
# Start checking when starting up the bot.
threading.Timer(60.0, check_and_remove_expired_muted_role).start()