Home > Net >  RuntimeWarning: coroutine 'Messageable.send' was never awaited python.py
RuntimeWarning: coroutine 'Messageable.send' was never awaited python.py

Time:12-19

i have a discord.py Python Script but it dont send messages and it comes this error:

RuntimeWarning: coroutine 'Messageable.send' was never awaited

My Code:

@client.command()
async def shift(ctx, time=None, shifts=None):
    if time is None:
        f=open('times.json')
        lines=f.readlines()
        print(lines[1])
        print(lines[2])
        embed=discord.Embed(title="Shift Infos für diesen Tag")
        embed.add_field(name="Zeit", value=f"{time}", inline=False)
        embed.add_field(name="Shift", value=f"{shifts}", inline=False)
        embed.add_field(name="Game", value="https://web.roblox.com/games/8063846199/VWS-Verkehrsbetriebe-Beta", inline=False)
        await ctx.send(embed=embed)
    else:
        if shift is None:
            ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
        else:
            ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")
            with open('times.json', 'a') as the_file:
                the_file.write(f'{time}\n')
                the_file.write(f'{shifts}')

CodePudding user response:

Your error message already tells you the solution.
send is asynchronous so you have to await it, like you do when you send your embed.

else:
    if shift is None:
        await ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
    else:
        await ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")

https://discordpy.readthedocs.io/en/master/api.html?highlight=send#discord.abc.Messageable.send

Also on an unrelated note: You open your file but never close it in your first if-block. Consider using a context manager, like you already do in your last else-block.

CodePudding user response:

@client.command()
async def shift(ctx, time=None, shifts=None):
    if time is None:
        f=open('times.json')
        lines=f.readlines()
        print(lines[1])
        print(lines[2])
        embed=discord.Embed(title="Shift Infos für diesen Tag")
        embed.add_field(name="Zeit", value=f"{time}", inline=False)
        embed.add_field(name="Shift", value=f"{shifts}", inline=False)
        embed.add_field(name="Game", value="https://web.roblox.com/games/8063846199/VWS-Verkehrsbetriebe-Beta", inline=False)
        await ctx.send(embed=embed)
    else:
        if shift is None:
            await ctx.send("Bitte gebe ein ob heute eine Shift ist. (Ja oder Nein)")
        else:
            await ctx.send(f"Neue Shift Einstellung: Zeit: {time} {shifts}")

            with open('times.json', 'a') as the_file:
                the_file.write(f'{time}\n')
                the_file.write(f'{shifts}')
  • Related