简体   繁体   English

如何使这段代码工作? ( discord.py )

[英]How to make this code work ?? ( discord.py )

Ok, so I want to remove a role from a user but it don't work ( the command is: $rmv_role @user )好的,所以我想从用户中删除一个角色,但它不起作用(命令是: $rmv_role @user )

CODE: ( it's not all of the code just the part with the command )代码:(不是所有的代码,只是带有命令的部分)

@bot.command(name='rmv_role', description="description for help command")
@commands.has_permissions(manage_roles=True)

async def rmv_role(ctx,arg):
    user_id = ''.join(c for c in arg if c.isdigit())
    guild = ctx.guild
    user = discord.utils.get(bot.get_all_members(), id=user_id)
    role_id = 999988854696194048
    role = discord.utils.get(guild.roles, id=role_id)
    await ctx.reply(f"th user :{arg} no longer has the role!")
    await bot.remove_roles(user,role)

So, finaly I changed my code and now it looks like this:所以,最后我改变了我的代码,现在它看起来像这样:

@bot.command(name='rmv_role', description="description for help command")
@commands.has_permissions(manage_roles=True)

async def rmv_role(ctx,user: discord.Member):
    guild = ctx.guild
    role_id = 999988854696194048
    role = discord.utils.get(guild.roles, id=role_id)
    await user.remove_roles (role)

I Made a Bot Couple Months Ago, I had This Problem But I solved it My Self, I had a Data Frame Like This几个月前我做了一个机器人,我遇到了这个问题,但我自己解决了,我有一个像这样的数据框

main.py
Commands (Folder)
  -> utility.py 

I just Linked The Main File to My Command Files so Instead Of @bot.command() , I had @commands.command()我只是将主文件链接到我的命令文件,所以我有@bot.command()而不是@commands.command()

You Don't Worry About The self (It Refers To your Prefix in My Code), If you have any other Doubts Regarding My Code Just Comment Down:你不用担心self (它指的是你在我的代码中的前缀),如果你对我的代码有任何疑问,请评论下来:

Code:代码:

   @commands.command(name='rmv_role', description="description for help command")
    @commands.has_permissions(manage_roles=True)
    async def rmv_role(self, ctx, member: discord.Member):
      print("open")
      guild = ctx.guild
      role_id =  999988854696194048
      role = discord.utils.get(guild.roles, id=role_id)
      await ctx.reply(f"th user :{member.name} no longer has the role!")
      await member.remove_roles(role)

Note That I Changed Your args to member: discord.Member Cause You dont Have to get the User with discord.Member Just Ping The Person!请注意,我将您的args更改为member: discord.Member因为您不必使用discord.Member get用户,只需 Ping 人!

I removed the user and user_id because of That As Well!因为那我删除了useruser_id

I tested and Checked The Code in My Bot Thats Why I took 20 Mins!我在我的机器人中测试并检查了代码这就是为什么我花了 20 分钟!

Hope This Helps, Comment If You have Any Doubt About this Or wanna learn about these self or @commands.command() more!希望这会有所帮助,如果您对此有任何疑问或想了解这些self@commands.command()更多信息,请发表评论! I am Happy To Help!我很乐意提供帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM