r/pythonhelp • • Jun 23 '21

How do I update an async function with a sync function?

I'm writing a discord music bot and have a problem with making a queue:

I think I slightly understand the error message I get:

RuntimeWarning: coroutine 'Command.__call__' was never awaited

I'm trying to call a sync function queueNext() from the async function play_combat() because queueNext() handles the queue lists of many functions in the same way.

  • Is it possible to mix sync and async functions?

  • How do you prevent sync functions to not be 'never awaited'?

  • how should I manipulate data from the async functions?

My code:

import discord
from discord.ext 
import commands 
import youtube_dl   
import os import random 
import numpy as np
client = commands.Bot(command_prefix='><')
defaultChannel='vc1' 
currentChannel='' 
musicFolder = r'C/path'
testlist=       ['no','testsong','war_horn','suspence']
queue = []

def queueNext(ctx, queue, listName): 
    queue.pop(0)    
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice.is_playing():            
        if listName == 'testlist':
            play_combat(queue)       #here is the error

@client.command(aliases = ['combat'])
async def play_combat(ctx, vcName=currentChannel, queue=[]):
queue plays
    list=testlist
    listName='testlist'
a bool to check if another command was given
    for item in np.arange(len(testlist)): # make the queue as long as the list
        song = random.choice(list)
        queue.append(song)  #add a randomly selected song to end of queue
        list.remove(song)   #shortens the list and prevents duplications
    voiceChannel = discord.utils.get(ctx.guild.channels, name=str(vcName)) 
    voice = discord.utils.get(client.voice_clients, guild=ctx.guild)
    if not voice.is_connected():      #check if I'm on the right vc
       await voiceChannel.connect()
    if not voice.is_playing(): # if no song is playing
        voice.play(discord.FFmpegPCMAudio(musicFolder+queue[0]+'.mp3'), after=lambda b: queueNext(ctx, queue, listName))


client.run('token')
2 Upvotes

1 comment sorted by

3

u/chrisking206 Jun 23 '21

Async is fun!

 play_combat(queue)

Should probably be

asyncio.run(play_combat(queue))

https://docs.python.org/3/library/asyncio-task.html