简体   繁体   中英

How to make one bot work on different servers and have different prefixes?

How to make one bot on different servers have a different prefix?

We can't use bot = commands.Bot(command_prefix = '!')

One person said to use JSON, but I don't understand how to configure the work of prefix from different servers in one .py file

You can do it with creating a method takes parameters bot , message .

First, create a json file (or database) to store prefixes with guild ids.

Json file can be like this.

{ "123456789123456789": "!" }

And now the code.

import json

def get_prefix(bot, message):
  with open(file_path, "r") as f:
    prefixes = json.load(f)
  return prefixes[str(message.guild.id)] #returning prefix that registered with guild's id

Last, registering the Bot object's command_prefix parameter.

bot = commands.Bot(command_prefix=get_prefix)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM