Как исправить, что Discord.py не запускает мою функцию asyncio для голосовой команды? - PullRequest
1 голос
/ 01 апреля 2019

В моем боте discord.py я пытаюсь создать функцию, которая запускает код, необходимый для того, чтобы бот присоединился к голосовому каналу, воспроизвел аудиофайл и затем ушел. Я делаю это, поэтому мне не нужно копировать и вставлять один и тот же код для каждой голосовой команды, которую я хочу сделать. Однако я не могу заставить функцию работать.

Я попытался поиграться с функциями 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.
*

и * 1006

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

Это фрагменты кода, которые я пытаюсь использовать для запуска функции.

Полный исходный код здесь: https://pastebin.com/bv86jSvk

1 Ответ

0 голосов
/ 01 апреля 2019

Вы можете использовать эти несколько методов для запуска асинхронной функции в 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())
...