Mac*_*Fan 3 python python-3.x discord.py-rewrite
我已经设置了一个 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))
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我应该如何为下的ping命令设置别名@commands.command()
discord.ext.commands.Command对象有一个aliases属性。以下是如何使用它:
@commands.command(aliases=['testcommand', 'testing'])
async def test(self, ctx):
await ctx.send("This a test command")
Run Code Online (Sandbox Code Playgroud)
然后,您将能够通过编写!test,!testcommand或!testing(如果您的命令前缀是!)来调用您的命令。
此外,如果您计划对日志系统进行编码,则Context对象具有一个invoked_with属性,该属性将调用命令的别名作为值。
参考: discord.py 文档
编辑:如果你只想让你的 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
Run Code Online (Sandbox Code Playgroud)