I’d like to create a dictionary with all roles of a guild for easier use in development:
roles_dictionary = {'role1': discord.utils.get(guild.roles, name='role1') }
etc.
I’d like to always be able to access any role using:
roles_dictionary['role1']
I already tried putting it into the on_ready() function, however it seems as if you shouldn’t do stuff like that in the on_ready() function, since whenever I tried closing the program it just restarted the bot in two instances. (Not sure why that happened, however my theory is, that I can’t do anything until the on_ready() function is done, meaning I made API calls before the bot was ready)
This is the code I have atm:
@client.event
async def on_ready():
for guild in client.guilds:
if guild.name == GUILD:
print(
f'{client.user} is connected to the following guild:\n'
f'{guild.name} (id: {guild.id})'
)
break
guild = client.get_guild(1099387953337335909)
roles_dict = {'owner_role': discord.utils.get(guild.roles, name='👑 Paradise Founder'),
'admin_role': discord.utils.get(guild.roles, name='🏝️ Island Manager'),
'moderator_role': discord.utils.get(guild.roles, name='🌴 Palm Guardian'),
'known_member_role': discord.utils.get(guild.roles, name='🍍 Tiki VIP'),
'booster_role': discord.utils.get(guild.roles, name='🌟 Starfish Supporter'),
'bot_role': discord.utils.get(guild.roles, name='🥥 Coconut Companion'),
'member_role': discord.utils.get(guild.roles, name='🌞 Beachcomber'),
'new_member_role': discord.utils.get(guild.roles, name='🌊 Seashell Seeker'),
'muted_role': discord.utils.get(guild.roles, name='🌅 Silent Sunset')}
I can’t use any other functions in the on_ready() function and I tried creating the dictionary in the on_ready() function, however I couldn’t access it and whenever I tried making the dictionary global, the weird startup issue started occuring.
>Solution :
There is no issue with doing this kind of thing in the on_ready
, what I am assuming is happening is that the dict you are creating is being garbage-collected as it is only available within that scope.
The easiest way to do this (imo) would be to subclass the standard bot class and make this new dict a member of it.
class MyBot(discord.Bot): # subclass discord.Bot
async def on_ready(self): # override the on_ready event
for guild in self.guilds:
if guild.name == GUILD:
self.roleDict = {'owner_role': discord.utils.get(guild.roles, name='👑 Paradise Founder'),
'admin_role': discord.utils.get(guild.roles, name='🏝️ Island Manager'),
'moderator_role': discord.utils.get(guild.roles, name='🌴 Palm Guardian'),
'known_member_role': discord.utils.get(guild.roles, name='🍍 Tiki VIP'),
'booster_role': discord.utils.get(guild.roles, name='🌟 Starfish Supporter'),
'bot_role': discord.utils.get(guild.roles, name='🥥 Coconut Companion'),
'member_role': discord.utils.get(guild.roles, name='🌞 Beachcomber'),
'new_member_role': discord.utils.get(guild.roles, name='🌊 Seashell Seeker'),
'muted_role': discord.utils.get(guild.roles, name='🌅 Silent Sunset')}
bot = MyBot() # create an instance of MyBot
bot.run("TOKEN") # run the bot
Then, to access this new dict, you would do as such:
bot.roleDict['moderator_role']
It’s important to note that because you are creating this in the on_ready
, if a role is created or removed, the roleDict will not reflect this data. I would rebuild it if this occurs.