I want to load some information about a server from a json file, each server is identified within this file by its guild.id. However if I want to try and load some data at the start with on_ready(), I cant use ctx, which I need to get the current servers guild.id, so I can identify it within the file.
(sorry if that's a bad explanation but just look at line 6 of my code and you'll understand what I'm trying to do)
Here is my current code:
@bot.event
async def on_ready():
with open("server_info.json", "r") as infoRaw:
infoJson = json.load(infoRaw)
for server in infoJson["Servers"]: #search each server data
if (server["id"] == ctx.message.guild.id): #compare id in file to current id (error line)
data = server[data]
break
I cant find any other ways online of getting the the servers id without a user sending a message first.
CodePudding user response:
You can use discord.utils, which would look like the following:
guild = discord.utils.get(bot.guilds, id=378473289473829)
You can use what every ID you want, just be sure to replace bot
with the name of your Client instance.
This works in on_ready, without any ctx
CodePudding user response:
You could use bot.guilds
which is a list of all guilds the bot is in and then check if the ID matches with an extra loop