首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >添加多个前缀discord.py

添加多个前缀discord.py
EN

Stack Overflow用户
提问于 2021-01-03 00:40:31
回答 1查看 447关注 0票数 1

我想实现每个服务器的自定义前缀,以及一个永久的前缀,这意味着人们可以使用喜欢的默认前缀.以及@bot,前缀存储在json文件中。

下面是我当前的代码:

代码语言:javascript
复制
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}') 

我试图找到一种方法,并尝试下面的方法,但它抛出了错误。任何帮助都将不胜感激。

代码语言:javascript
复制
client = commands.Bot(
    command_prefix= [(get_prefix), '<@!123>'],
    intents=intents
    )

json文件格式:

代码语言:javascript
复制
{
    "121212121212121221": "-",
    "121212121212121222": "-",
    "121212121212121223": "!",
    "121212121212121224": "-",
    "121212121212121225": "."
}
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2021-01-03 01:07:53

这是一个改进的版本

代码语言:javascript
复制
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)
票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/65541552

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档