以下是完整的脚本:
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
获取上下文我猜您的命令不起作用,因为您没有在
on_message
事件末尾添加process_commands
从现在起,每个命令都应该起作用
参考资料:
相关问题 更多 >
编程相关推荐