How do I create aliases in discord.py cogs?(如何在 discord.py cogs 中创建别名?)
问题描述
我设置了一个 discord.py cog,可以使用了.有一个问题,如何为命令设置别名?我会在下面给你我的代码,看看我还需要做什么:
I have a discord.py cog set up, ready to use. 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))
这种情况下,@commands.command()
推荐答案
discord.ext.commands.Command
对象有一个 aliases
属性.使用方法如下:
discord.ext.commands.Command
objects have a aliases
attribute. Here's how to use it:
@commands.command(aliases=['testcommand', 'testing'])
async def test(self, ctx):
await ctx.send("This a test command")
然后您就可以通过编写 !test
、!testcommand
或 !testing
来调用您的命令(如果您的命令前缀是!
).
此外,如果您计划编写日志系统,Context
对象有一个 invoked_with
属性,它将调用命令时使用的别名作为值.
You'll then be able to invoke your command by writing !test
, !testcommand
or !testing
(if your command prefix is !
).
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.
参考: discord.py 文档
如果您只想让您的 cog 管理员,您可以覆盖现有的 cog_check
函数,该函数将在调用来自该 cog 的命令时触发:
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:
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 cogs 中创建别名?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:如何在 discord.py cogs 中创建别名?


基础教程推荐
- 使用PyInstaller后在Windows中打开可执行文件时出错 2022-01-01
- 筛选NumPy数组 2022-01-01
- 线程时出现 msgbox 错误,GUI 块 2022-01-01
- 如何让 python 脚本监听来自另一个脚本的输入 2022-01-01
- Python kivy 入口点 inflateRest2 无法定位 libpng16-16.dll 2022-01-01
- 用于分类数据的跳跃记号标签 2022-01-01
- 在 Python 中,如果我在一个“with"中返回.块,文件还会关闭吗? 2022-01-01
- 如何在海运重新绘制中自定义标题和y标签 2022-01-01
- Dask.array.套用_沿_轴:由于额外的元素([1]),使用dask.array的每一行作为另一个函数的输入失败 2022-01-01
- 何时使用 os.name、sys.platform 或 platform.system? 2022-01-01