简体   繁体   English

根据命令分配角色

[英]assign role on command

I'm pretty new to python and discord bots in general.一般来说,我对 python 和 discord 机器人还很陌生。 I am trying to make it so when a user runs.addrole (role) they will get that role.我正在努力做到这一点,以便当用户运行时。addrole(角色)他们将获得该角色。 I need it to work for multiple users and multiple roles.我需要它为多个用户和多个角色工作。 It would be nice if users could also give other users that role.如果用户也可以给其他用户该角色,那就太好了。

from discord.ext import commands
import os
import random
import discord
from discord.utils import get


my_secret = os.environ['TOKEN']
GUILD = os.getenv('DISCORD_GUILD')


bot = commands.Bot(command_prefix='!') #prefix

  

@bot.command(pass_context=True)
@commands.has_role("admin") 
async def addrole(ctx, *, rolewanted):
    member = ctx.message.author
    role = rolewanted
    await bot.add_roles(member, role)


bot.run(my_secret)

Make sure to read the documentation, you're code is pretty outdated.请务必阅读文档,您的代码已经过时了。 You can type hint member and role to discord objects and use the add_roles method to do what you need您可以将提示memberrole键入 discord 对象并使用add_roles方法来执行您需要的操作

    @bot.command()
    @commands.has_role('admin')
    async def addrole(ctx, member: discord.Member, role: discord.Role):
        await member.add_roles(role)

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

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