Let's say I don't want to limit myself to a single prefix, and instead use a command to change the prefix to whatever I want for each individual server I'm on.
Basically, there would first be a default prefix, for example bl!, and if there was another bot with that prefix, I could do something like bl!prefix .... This would then have the bot read a given text file or json, edit the prefix depending on the guild, and work with that guild and only that guild.
Step 1 - Set up your json files:
First you'll want to make a json file. This file will store the information of message.guild.id as well as the prefix for that guild. In the picture below, you'll see the json file after it was added to multiple servers. If your bot is already in a large number of servers, you may need to add them manually before you can have an automatic system.

Step 2 - Define get_prefix: When you're setting up your discord.py bot, you will usually come across a line of code stating the command_prefix of the bot. To have a custom prefix, you will first need to define get_prefix, which will read from the json file you would have made before.
from discord.ext import commands
def get_prefix(client, message): ##first we define get_prefix
with open('prefixes.json', 'r') as f: ##we open and read the prefixes.json, assuming it's in the same file
prefixes = json.load(f) #load the json as prefixes
return prefixes[str(message.guild.id)] #recieve the prefix for the guild id given
Step 3 - Your bots command_prefix: This is to ensure your bot has a prefix. Instead of using command_prefix = "bl!", you'll be using your previously defined get_prefix.
client = commands.Bot(
command_prefix= (get_prefix),
)
Step 4 - Joining and leaving servers: All this does is manipulate prefixes.json.
@client.event
async def on_guild_join(guild): #when the bot joins the guild
with open('prefixes.json', 'r') as f: #read the prefix.json file
prefixes = json.load(f) #load the json file
prefixes[str(guild.id)] = 'bl!'#default prefix
with open('prefixes.json', 'w') as f: #write in the prefix.json "message.guild.id": "bl!"
json.dump(prefixes, f, indent=4) #the indent is to make everything look a bit neater
@client.event
async def on_guild_remove(guild): #when the bot is removed from the guild
with open('prefixes.json', 'r') as f: #read the file
prefixes = json.load(f)
prefixes.pop(str(guild.id)) #find the guild.id that bot was removed from
with open('prefixes.json', 'w') as f: #deletes the guild.id as well as its prefix
json.dump(prefixes, f, indent=4)
Step 5 - Change Prefix Command:
@client.command(pass_context=True)
@commands.has_permissions(administrator=True) #ensure that only administrators can use this command
async def changeprefix(ctx, prefix): #command: bl!changeprefix ...
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f: #writes the new prefix into the .json
json.dump(prefixes, f, indent=4)
await ctx.send(f'Prefix changed to: {prefix}') #confirms the prefix it's been changed to
#next step completely optional: changes bot nickname to also have prefix in the nickname
name=f'{prefix}BotBot'
client.run("TOKEN")
Edit: This is for any issues you may come across while using this.
How do I use commands.when_mentioned with this?
Why do I get an error whenever someone DMs my bot?
How do I get my bot to respond when mentioned with the guild's set prefix?
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With