discord.py 前缀命令 [英] discord.py prefix command

查看:110
本文介绍了discord.py 前缀命令的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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.

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

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.

推荐答案

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

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.

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

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.

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!".

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),
    )

第 4 步 - 加入和离开服务器:所有这些都是操纵 prefixes.json.

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)

第 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")

这是针对您在使用它时可能遇到的任何问题.

This is for any issues you may come across while using this.

如何使用命令.当_提到这个?

为什么每当有人向我的机器人发送消息时我会收到错误消息?

如何当提到公会的设置前缀时,我是否让我的机器人做出响应?

这篇关于discord.py 前缀命令的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆