2 回答

TA貢獻(xiàn)1836條經(jīng)驗 獲得超13個贊
get_context
,這需要一個消息對象。然后invoke
。請記住,使用此方法有 3 個缺點。
轉(zhuǎn)換器(類型提示)不會被觸發(fā)。您需要將正確的類型傳遞給參數(shù)。
檢查將被繞過。您可以對非所有者調(diào)用僅限所有者的命令,它仍然有效。
如果
ctx.invoke
在命令外部調(diào)用(例如 eval),則錯誤處理程序?qū)⒉粫|發(fā)。
@client.command()
async def menu(ctx):
? ? await ctx.send("Hello")
@client.event
async def on_message(message):
? ? if message.content.startswith("nothing"):
? ? ? ? ctx = await client.get_context(message)
? ? ? ? await ctx.invoke(menu)
? ? await client.process_commands(message)

TA貢獻(xiàn)1860條經(jīng)驗 獲得超8個贊
如果您的客戶端是一個Bot
實例,您可以使用Bot.get_context()創(chuàng)建您自己的上下文并從那里調(diào)用命令:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
@bot.command()
async def menu(ctx):
? ? await ctx.send('bar')
@bot.event
async def on_message(message):
? ? if message.content.startswith('foo'):
? ? ? ? ctx = await bot.get_context(message, cls=commands.Context)
? ? ? ? ctx.command = bot.get_command('menu')
? ? ? ? await bot.invoke(ctx)
? ? await bot.process_commands(message)
添加回答
舉報