自定义前缀 Discord.py

Posted

技术标签:

【中文标题】自定义前缀 Discord.py【英文标题】:Custom Prefix Discord.py 【发布时间】:2021-03-31 02:40:16 【问题描述】:

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

【问题讨论】:

这有帮助吗? ***.com/questions/51915962/per-server-prefixs 【参考方案1】:
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)

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

【讨论】:

以上是关于自定义前缀 Discord.py的主要内容,如果未能解决你的问题,请参考以下文章