gpt4 book ai didi

python - 如何修复 Discord.py 不为语音命令运行我的 asyncio 函数?

转载 作者:太空宇宙 更新时间:2023-11-04 04:15:50 28 4
gpt4 key购买 nike

在我的 discord.py 机器人中,我试图创建一个函数来运行机器人加入语音 channel 、播放音频文件然后离开所必需的代码。我正在这样做,所以我不必为我想发出的每个语音命令复制和粘贴相同的代码。但是,我无法运行该功能。

我曾尝试为我的函数使用 async def 和 await 函数,但它似乎不起作用。我一无所知,因为当我运行我的代码时,我没有收到任何错误。

async def voiceCommand(ctx, message, file, time, cmd):
channel = ctx.message.author.voice.voice_channel
if channel == None: # If the user who sent the voice command isn't in a voice channel.
await client.say('You are not in a voice channel, therefore I cannot run this command.')
return
else: # If the user is in a voice channel
print(executed(cmd, message.author))
voice = await client.join_voice_channel(channel)
server = ctx.message.server
voice_client = client.voice_client_in(server)
player = voice.create_ffmpeg_player(file) # Uses ffmpeg to create a player, however it makes a pop up when it runs.
player.start()
time.sleep(float(time))
await voice_client.disconnect() # Disconnects WingBot from the voice channel.

@client.command(pass_context = True)
async def bigbruh(ctx):
voiceCommand(ctx, message, 'bigbruh.mp3', '0.5', 'bigbruh')

这些是我试图用来运行函数的代码片段。

完整源代码在这里:https://pastebin.com/bv86jSvk

最佳答案

您可以使用这几种方法在 python 中运行异步函数

async def print_id():
print(bot.user.id)

@bot.event
async def on_ready():
bot.loop.create_task(print_id()) #this will run the print_id function
print(bot.user.name)

async def not_me(msg):
await bot.send_message(msg.message.channel,"You are not me")

async def greet():
print("Hi")

@bot.command(pass_context=True)
async def me(msg):
if msg.message.author.id == '123123':
await bot.say("Hello there") #await is used to run async function inside another async function

else:
await not_me(msg)


#To run the async function without it being inside another async function you have to use this method or something similar to it
import asyncio

# asyncio.get_event_loop().run_until_complete(the_function_name())
#in this case it's `greet`
asyncio.get_event_loop().run_until_complete(greet())

关于python - 如何修复 Discord.py 不为语音命令运行我的 asyncio 函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55445694/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com