添加多个前缀 discord.py
Adding multiple prefixes discord.py
我想为每个服务器实现自定义前缀,以及一个永久性前缀,即机器人提及,这意味着人们可以使用默认前缀 .
以及 @bot
,前缀是存储在 json 文件中。
这是我当前的代码:
with open("prefixes.json") as f:
prefixes = json.load(f)
default_prefix = "."
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(
command_prefix= (get_prefix),
intents=intents
)
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '.'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def prefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'`Prefix changed to:` {prefix}')
我试图找到一种方法并尝试了下面的方法,但它抛出了错误。任何帮助将不胜感激。
client = commands.Bot(
command_prefix= [(get_prefix), '<@!123>'],
intents=intents
)
json 文件格式:
{
"121212121212121221": "-",
"121212121212121222": "-",
"121212121212121223": "!",
"121212121212121224": "-",
"121212121212121225": "."
}
这是一个改进版本[=11=]
def get_prefix(client, message):
prefix = default_prefix
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefix = prefixes[str(message.guild.id)]
return commands.when_mentioned_or(prefix)(client, message)
client = commands.Bot(command_prefix=get_prefix, case_insensitive=True, intents=intents)
我想为每个服务器实现自定义前缀,以及一个永久性前缀,即机器人提及,这意味着人们可以使用默认前缀 .
以及 @bot
,前缀是存储在 json 文件中。
这是我当前的代码:
with open("prefixes.json") as f:
prefixes = json.load(f)
default_prefix = "."
def get_prefix(client, message):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
return prefixes[str(message.guild.id)]
client = commands.Bot(
command_prefix= (get_prefix),
intents=intents
)
@client.event
async def on_guild_join(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(guild.id)] = '.'
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.event
async def on_guild_remove(guild):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes.pop(str(guild.id))
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
@client.command(pass_context=True)
@commands.has_permissions(administrator=True)
async def prefix(ctx, prefix):
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefixes[str(ctx.guild.id)] = prefix
with open('prefixes.json', 'w') as f:
json.dump(prefixes, f, indent=4)
await ctx.send(f'`Prefix changed to:` {prefix}')
我试图找到一种方法并尝试了下面的方法,但它抛出了错误。任何帮助将不胜感激。
client = commands.Bot(
command_prefix= [(get_prefix), '<@!123>'],
intents=intents
)
json 文件格式:
{
"121212121212121221": "-",
"121212121212121222": "-",
"121212121212121223": "!",
"121212121212121224": "-",
"121212121212121225": "."
}
这是一个改进版本[=11=]
def get_prefix(client, message):
prefix = default_prefix
with open('prefixes.json', 'r') as f:
prefixes = json.load(f)
prefix = prefixes[str(message.guild.id)]
return commands.when_mentioned_or(prefix)(client, message)
client = commands.Bot(command_prefix=get_prefix, case_insensitive=True, intents=intents)