Search code examples
pythondiscord

How do I create a role and add it to guild in Hikari?


So basically I'm trying to create a role and add it to a guild in Hikari. I've been googling and looking at the documentation but I can't seem to find anything to add a role and if this is not possible are there any workarounds?


Solution

  • The documentation you were looking for is located here.

    hikari.GatewayBot has a rest client used for making api calls to Discord - this is the rest attribute of the bot.

    In this code block, the bot will use the rest client to create a role called new-role-name every time is it invited to a new guild while it is online.

    import os
    import hikari
    
    
    bot = hikari.GatewayBot(os.environ["TOKEN"])
    
    @bot.listen()
    async def on_guild_join(event: hikari.GuildJoinEvent) -> None:
        await bot.rest.create_role(event.guild_id, name="new-role-name")
    
    
    if __name__ == "__main__":
        bot.run()
    

    Keep in mind, if the bot doesn't have the MANAGE_ROLES permission in the guild it just joined, this code will raise an ForbiddenError.