Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
4.3k views
in Technique[技术] by (71.8m points)

discord.py - I am trying to make a discord bot that can mute and unmute, but i am not having success. Here is my code

All relevant discord connections have been made, and the bot recognizes mute and unmute as a command, it just doesnt work. Also - how can i make it so there is a time for the mute?

@client.command()
async def mute(ctx, member: discord.Member):
role_members = discord.utils.get(ctx.guild.roles, name='Member')
role_muted = discord.utils.get(ctx.guild.roles, name='Muted')
await member.remove_roles(role_members)
await member.add_roles(role_muted)
await context.send("f'User {member} Was Muted")

@client.command()
async def unmute(ctx, member: discord.Member):
role_members = discord.utils.get(ctx.guild.roles, name='Member')
role_muted = discord.utils.get(ctx.guild.roles, name='Muted')
await member.remove_roles(role_muted)
await member.add_roles(role_members)
await context.send("f'User {member} Was Unmuted")

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

This should work. I took this from another stack overflow post. This code will find the user and add them to the "muted" server role.

@bot.command(pass_context = True)
async def mute(ctx, member: discord.Member):
     if ctx.message.author.server_permissions.administrator or ctx.message.author.id == '194151340090327041':
        role = discord.utils.get(member.server.roles, name='Muted')
        await bot.add_roles(member, role)
        embed=discord.Embed(title="User Muted!", description="**{0}** was muted by **{1}**!".format(member, ctx.message.author), color=0xff00f6)
        await bot.say(embed=embed)
     else:
        embed=discord.Embed(title="Permission Denied.", description="You don't have permission to use this command.", color=0xff00f6)
        await bot.say(embed=embed)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...