亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何在 discord.py cogs 中創建別名?

如何在 discord.py cogs 中創建別名?

慕的地6264312 2023-03-30 10:39:27
我已經設置了一個 discord.py cog,可以使用了。有一個問題,如何為命令設置別名?我會在下面給你我的代碼,看看我還需要做什么:# Importsfrom discord.ext import commandsimport bot  # My own custom module# Client commandsclass 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 functiondef setup(client):    client.add_cog(Member(client))ping這樣的話,我應該如何為下面的命令設置別名呢?@commands.command()
查看完整描述

1 回答

?
開心每一天1111

TA貢獻1836條經驗 獲得超13個贊

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屬性,該屬性采用調用命令時使用的別名作為值。


編輯:如果你只想讓你的 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


查看完整回答
反對 回復 2023-03-30
  • 1 回答
  • 0 關注
  • 108 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號