简体   繁体   English

如何在 discord.py cogs 中创建别名?

[英]How do I create aliases in discord.py cogs?

I have a discord.py cog set up, ready to use.我有一个 discord.py 齿轮设置,可以使用了。 There is one issue, how do I set up aliases for commands?有一个问题,如何为命令设置别名? I'll give you my code below to see what else I need to do:我会在下面给你我的代码,看看我还需要做什么:

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

In this case, how should I set up an alias for the ping command under @commands.command()在这种情况下,我应该如何为@commands.command()下的ping命令设置一个别名

discord.ext.commands.Command objects have a aliases attribute. discord.ext.commands.Command对象具有aliases属性。 Here's how to use it:以下是如何使用它:

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

You'll then be able to invoke your command by writing !test , !testcommand or !testing (if your command prefix is ! ).然后,您可以通过编写!test!testcommand!testing来调用您的命令(如果您的命令前缀是! )。
Also, if you plan on coding a log system, Context objects have a invoked_with attribute which takes the aliase the command was invoked with as a value.此外,如果您计划编写日志系统, Context对象有一个invoked_with属性,该属性将调用命令时使用的别名作为值。

Reference: discord.py documentation参考: discord.py 文档


Edit: If you want to make your cog admin only, you can overwrite the existing cog_check function that will trigger when a command from this cog is invoked:编辑:如果您只想让您的 cog 管理员,您可以覆盖现有的cog_check function,当调用来自此 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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM