【问题标题】:discord.py prefix command [closed]discord.py前缀命令[关闭]
【发布时间】:2021-02-07 07:49:36
【问题描述】:

假设我不想将自己限制为单个前缀,而是使用命令将前缀更改为我所在的每个单独服务器所需的任何前缀。

基本上,首先会有一个默认前缀,例如bl!,如果有另一个具有该前缀的机器人,我可以执行bl!prefix ... 之类的操作。然后,机器人会读取给定的文本文件或 json,根据公会编辑前缀,并使用该公会并且仅与该公会合作。

【问题讨论】:

    标签: discord.py


    【解决方案1】:

    第 1 步 - 设置您的 json 文件: 首先,您需要制作一个 json 文件。该文件将存储message.guild.id 的信息以及该公会的前缀。在下图中,您将看到添加到多个服务器后的 json 文件。如果您的机器人已经在大量服务器中,您可能需要手动添加它们,然后才能拥有自动系统。

    第 2 步 - 定义 get_prefix:当您设置 discord.py 机器人时,您通常会遇到一行代码,说明机器人的 command_prefix。要拥有自定义前缀,您首先需要定义 get_prefix,它将从您之前创建的 json 文件中读取。

    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
    

    第 3 步 - 您的机器人命令前缀:这是为了确保您的机器人具有前缀。您将使用之前定义的get_prefix,而不是使用command_prefix = "bl!"。

    client = commands.Bot(
        command_prefix= (get_prefix),
        )
    

    第 4 步 - 加入和离开服务器:所有这些都是操纵 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)
    

    第 5 步 - 更改前缀命令:

    @client.command(pass_context=True)
    @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")
    

    编辑:这是针对您在使用它时可能遇到的任何问题。

    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?

    【讨论】:

    • 太好了,你自己回答了,但我认为创建一个像 mongodb 这样的数据库会更好,因为它会减少你的 IDE 的负载。只是一个提示,祝你好运
    猜你喜欢
    • 2019-11-09
    • 2021-04-04
    • 2021-03-06
    • 2021-10-02
    • 2021-09-17
    • 2021-02-03
    • 2021-05-04
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多