在外部使用 python 为不和谐的人添加角色

时间:2021-03-23 15:37:44

标签: python discord discord.py

所以我正在制作一个脚本,让您可以使用 cmd 窗口从外部控制 discord bot,我已经完成了一些工作,但我似乎找不到任何关于如何向人们添加角色的示例。这是我为创建角色所做的

elif option == "4":
            guild = await bot.fetch_guild(guildid)
            amount = int(input("[!] Number of roles to make?\n[>] "))
            name = input("[!] Name of roles to make? Type RANDOM for random character names!\n[>] ")
            await bot.change_presence(activity=discord.Game('[!] Creating Roles [!]'))
            random = name.upper()
            colorcount = "red"
            for i in range (amount):   
                if random == "RANDOM":
                    name =  "".join(choice(characters) for x in range(randint(4, 15)))
                if colorcount == "red":
                    await guild.create_role(name=name,color=discord.Color.red())
                    colorcount = "red"
                elif colorcount == "red":
                    await guild.create_role(name=name,color=discord.Color.red())
                    colorcount = "red"
                currentDT = datetime.datetime.now()
                hour = str(currentDT.hour)
                minute = str(currentDT.minute)
                second = str(currentDT.second)
                print(f"{Fore.RED}[{Fore.WHITE}{hour}:{minute}:{second}{Fore.RED}]{Fore.WHITE} Role Created{Fore.RED} :{Fore.WHITE} {name}")
                os.system(f"title Spam creating roles - [{i+1}]")
            input(f"{Fore.RED}[  {Fore.WHITE}  +  {Fore.RED} ] {Fore.WHITE}Created All Roles {Fore.RED}:{Fore.WHITE} [{i+1}] \n[>] ")
            await bot.change_presence(activity=discord.Game('Idle...'))

现在我希望能够看到角色并将它们添加到用户,但我不知道如何做到这一点,谁能给我一些想法?或者甚至更好一些关于如何从外部控制/编码机器人的指南?

1 个答案:

答案 0 :(得分:0)

要创建角色,

对于重写分支:

guild = ctx.guild
await guild.create_role(name="role name")
to add color, just add colour=discord.Colour(0xffffff) as an option in create_role, and replace ffffff with the hex code for the color. To add permissions to the role, include permissions=discord.Permissions(permissions=<permission value>)

对于异步分支:

author = ctx.message.author
await client.create_role(author.server, name="role name")

要添加颜色(可能还有权限),只需像添加重写分支一样操作即可。

现在,如果您想为用户添加角色,

对于重写分支:

role = discord.utils.get(ctx.guild.roles, name="role to add name")
user = ctx.message.author
await user.add_roles(role)

对于异步分支:

user = ctx.message.author
role = discord.utils.get(user.server.roles, name="role to add name")
await client.add_roles(user, role)

要查看您拥有哪个分支,请执行 print(discord._version)。如果它说 1.0.0a,你有重写分支。如果它说 0.16.2 或更低的数字,你有异步分支。要计算权限值,您可以使用本网站

注意:Discord.py 目前为 1.6

可以这样,试试。而且在网上很容易找到