Bluesheep33
Bluesheep33

Reputation: 7

discord.py get a server prefix from .json file

I just got into coding discord bots using discord.py (Python 3.8; discord.py 1.5.1) and I ran into a problem. I want to give the user the server prefix when he/she uses my custom help command (which just returns a string with all commands and the prefix). I have stored the prefixes of servers in a .json file, but how can I return the server prefix from that file?

Tell me if you need some parts of my code to be able to find a solution.

Upvotes: 1

Views: 692

Answers (1)

creed
creed

Reputation: 1427

Assuming this is your prefixes.json file:

{
    "781524858026590218": "k-"
}

You can grab the ID of the guild and have it check the json file for that prefix:

import json

@client.command()
async def GetPrefix(ctx):
    with open("prefixes.json", "r") as f:
        data = json.loads(f.read())
        guildID = str(ctx.guild.id)
        prefix = data[guildID]
        await ctx.send("Prefix:", prefix)

Upvotes: 2

Related Questions