Python 如何忽略discord.py中的ctx参数?
以下是完整的脚本:Python 如何忽略discord.py中的ctx参数?,python,discord.py,Python,Discord.py,以下是完整的脚本: from discord.ext.commands import Bot import tracemalloc tracemalloc.start() client = Bot(command_prefix='$') TOKEN = '' @client.event async def on_ready(): print(f'Bot connected as {client.user}') @client.command(pass_context=Tru
from discord.ext.commands import Bot
import tracemalloc
tracemalloc.start()
client = Bot(command_prefix='$')
TOKEN = ''
@client.event
async def on_ready():
print(f'Bot connected as {client.user}')
@client.command(pass_context=True)
async def yt(ctx, url):
author = ctx.message.author
voice_channel = author.voice_channel
vc = await client.join_voice_channel(voice_channel)
player = await vc.create_ytdl_player(url)
player.start()
@client.event
async def on_message(message):
if message.content == 'play':
await yt("youtubeurl")
client.run(TOKEN)
当我运行这个脚本时,它工作得很好。当我在聊天中键入play时,出现以下错误:
Traceback (most recent call last):
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\client.py", line 343, in _run_event
await coro(*args, **kwargs)
File "C:\Users\user\bot.py", line 26, in on_message
await yt("youtubeurl")
File "C:\Users\user\AppData\Local\Programs\Python\Python39\lib\site-packages\discord\ext\commands\core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
TypeError: yt() missing 1 required positional argument: 'url'
我该如何解决这个问题?到目前为止,我所尝试的只是在ctx和url之间添加了一个参数*,该参数无法修复它无法“忽略”上下文参数,为什么要在on_message
事件中调用该命令
您可以使用Bot.get_context
@client.event
异步def on_消息(消息):
ctx=等待客户端。获取上下文(消息)
如果message.content==“播放”:
yt(ctx,“url”)
我猜您的命令不起作用,因为您没有在on\u消息
事件的末尾添加process\u命令
@client.event
异步def on_消息(消息):
# ... PS:不要为命令添加if语句,这是无用的
等待客户端处理命令(消息)
从现在起,每个命令都应该起作用
参考: