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

查看:63
本文介绍了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 ... <的操作./code>.然后,机器人将读取给定的文本文件或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步-您的漫游器command_prefix :这是为了确保您的漫游器具有前缀.代替使用 command_prefix ="bl!" ,您将使用以前定义的 get_prefix .

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.

如何使用命令.什么时候提到了?

为什么当有人DM我的机器人时,我为什么会出错?/a>

Why do I get an error whenever someone DMs my bot?

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

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