【问题标题】:Adding roles to people in discord using python externally在外部使用 python 为不和谐的人添加角色
【发布时间】:2021-03-24 03:22:30
【问题描述】:

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

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...'))

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

【问题讨论】:

    标签: python discord discord.py


    【解决方案1】:

    要创建角色,

    对于重写分支:

    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

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

    【讨论】:

      猜你喜欢
      • 2018-08-11
      • 2022-10-24
      • 1970-01-01
      • 1970-01-01
      • 2020-04-04
      • 1970-01-01
      • 2021-05-31
      • 1970-01-01
      • 2022-12-03
      相关资源
      最近更新 更多