自定义前缀 Discord.py

时间:2020-12-21 10:20:06

标签: python python-3.x bots discord.py

我正在尝试找到一种方法来创建一个命令,该命令可以为我的机器人的不同服务器设置自定义前缀。如果有任何帮助,我也在使用 Cogs...

主文件:

import discord
import os
from discord.ext import commands

client = commands.Bot(command_prefix = '/')

@client.command()
async def load(ctx, extension):
    client.load_extension(f'cogs.{extension}')

@client.command()
async def unload(ctx, extension):
    client.unload_extension(f'cogs.{extension}')

for filename in os.listdir('./cogs'):
    if filename.endswith('.py'):
      client.load_extension(f'cogs.{filename[:-3]}')


client.run('My Token')

1 个答案:

答案 0 :(得分:0)

async def get_prefix(bot, message):
    guild = message.guild
    # you can do something here with the guild obj, open a file and return something different per guild
    default_prefix, custom_prefix = '.', '>'
    return [default_prefix, custom_prefix]

client = commands.Bot(command_prefix=get_prefix)

每次处理命令时都会调用该函数。