【问题标题】:How do I create aliases in discord.py cogs?如何在 discord.py cogs 中创建别名?
【发布时间】:2020-11-13 16:17:50
【问题描述】:

我设置了一个 discord.py cog,可以使用了。有一个问题,如何为命令设置别名?我会在下面给你我的代码,看看我还需要做什么:

# Imports
from discord.ext import commands
import bot  # My own custom module


# Client commands
class Member(commands.Cog):
    def __init__(self, client):
        self.client = client

    # Events
    @commands.Cog.listener()
    async def on_ready(self):
        print(bot.online)

    # Commands
    @commands.command()
    async def ping(self, ctx):
        pass


# Setup function
def setup(client):
    client.add_cog(Member(client))

这种情况下@commands.command()下的ping命令应该如何设置别名

【问题讨论】:

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


    【解决方案1】:

    discord.ext.commands.Command 对象具有aliases 属性。使用方法如下:

    @commands.command(aliases=['testcommand', 'testing'])
    async def test(self, ctx):
        await ctx.send("This a test command")
    

    然后您就可以通过编写!test!testcommand!testing 来调用您的命令(如果您的命令前缀是!)。
    此外,如果您计划编写日志系统,Context 对象有一个 invoked_with 属性,该属性将调用命令时使用的别名作为值。

    参考:discord.py documentation


    编辑:如果您只想让您的 cog 管理员,您可以覆盖现有的 cog_check 函数,该函数将在调用来自该 cog 的命令时触发:

    from discord.ext import commands
    from discord.utils import get
    
    class Admin(commands.Cog):
        def __init__(self, bot):
            self.bot = bot
    
        async def check_cog(self, ctx):
            admin = get(ctx.guild.roles, name="Admin")
            #False -> Won't trigger the command
            return admin in ctx.author.role
    

    【讨论】:

    • 感谢您的帮助!
    • 另外一件小事,我可以只做一个整个 cog 类的管理员吗?
    • 我已经编辑了我的答案,对于其他问题,请在提问前进行自己的研究:)
    猜你喜欢
    • 2019-04-30
    • 2020-12-12
    • 2021-04-29
    • 1970-01-01
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-19
    相关资源
    最近更新 更多