如何在 discord.py 齿轮中创建别名?
How do I create aliases in discord.py cogs?
我已经设置好 discord.py 齿轮,可以使用了。有一个问题,如何为命令设置别名?我会在下面给你我的代码,看看我还需要做什么:
# 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
命令应该如何设置别名呢?
discord.ext.commands.Command
objects have a aliases
属性。使用方法如下:
@commands.command(aliases=['testcommand', 'testing'])
async def test(self, ctx):
await ctx.send("This a test command")
然后您可以通过编写 !test
、!testcommand
或 !testing
(如果您的命令前缀是 !
)来调用您的命令。
此外,如果您计划对日志系统进行编码,Context
objects have a invoked_with
属性以调用命令时使用的别名作为值。
编辑: 如果您只想让您的 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
我已经设置好 discord.py 齿轮,可以使用了。有一个问题,如何为命令设置别名?我会在下面给你我的代码,看看我还需要做什么:
# 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
命令应该如何设置别名呢?
discord.ext.commands.Command
objects have a aliases
属性。使用方法如下:
@commands.command(aliases=['testcommand', 'testing'])
async def test(self, ctx):
await ctx.send("This a test command")
然后您可以通过编写 !test
、!testcommand
或 !testing
(如果您的命令前缀是 !
)来调用您的命令。
此外,如果您计划对日志系统进行编码,Context
objects have a invoked_with
属性以调用命令时使用的别名作为值。
编辑: 如果您只想让您的 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