Discord.py 发送参数"weird"



所以,我正在制作一个discord音乐机器人,当这个人搜索某个东西时,它会播放音乐,并使用有价值的arg,例如:?播放圣诞歌曲,但尽管它确实正确播放了音乐,但它还是会发送以下信息:现在播放:("圣诞节","歌曲"(这就像它剪切了两个单词,然后把它们都放在一个列表中一样?

这是我的代码:

@commands.command()
async def play(self, ctx, *arg):

if ctx.author.voice is None:
await ctx.send("Join a voice channel")
voice_channel = ctx.author.voice.channel
if ctx.voice_client is None:
await voice_channel.connect()
else:
await ctx.voice_client.move_to(voice_channel)
ctx.voice_client.stop()
try:
requests.get("" + str(arg))
except: arg = " " + str(arg)
else: arg = "" + str(arg)
YDL_OPTIONS = {'format':"bestaudio"}
vc = ctx.voice_client
FFMPEG_OPTIONS = {'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5', 'options': '-vn'}
with youtube_dl.YoutubeDL(YDL_OPTIONS) as ydl:
info = ydl.extract_info(f"ytsearch:{arg}", download=False)
if 'entries' in info:
video = info['entries'][0]
else:
video = info
url2 = video['formats'][0]['url']
print(video)
video_url = video['url']
print(video_url)
source = await discord.FFmpegOpusAudio.from_probe(url2, **FFMPEG_OPTIONS)
await ctx.send("Now playing: **" + str(arg) + "**")
vc.play(source)

它不是通过生成关键字参数来执行async def play(self, ctx, *arg):async def play(self, ctx, *, arg):,而是告诉discord.py使用传递给该单个参数的所有参数

因此,调用为{prefix}play hello from adelle的命令,在您的命令中,您的arg将是hello from adelle

这是因为你只是打印你收到的参数,你需要像这样解耦arg

await ctx.send("Now playing: **" + *arg + "**")

await ctx.send("Now playing: **" + ' '.join(arg) + "**")

最新更新