Discord Python - run bot from a different file using async/await

Viewed 812

I'm trying to integrate my code to a discord bot and I want it on different files, only problem is if I call the discord file without async/await, the functions on one of the files (either main file or discordNew file) dont run depending on the order I call them.

I'm new to async/await and I've written a basic prototype of a code - which I can't get working as well. I'd appreciate some help here with a reason to the solution as well please.

File 1 - main.py

import discordNew
import asyncio


class Temp(discordNew.MyClient):
    async def hello(self):
        print("Hello")

    async def run_main(self):
        print("A")
        await self.hello()
        await self.runDiscordBot()
        print("B")


if __name__ == '__main__':
    asyncio.run(Temp().run_main()) ##Error on reading from the event loop self pipe
    Temp().run_main() ##RuntimeWarning: coroutine 'Temp.run_main' was never awaited

File 2 - discordNew.py

import discord

TOKEN = "token here"


class MyClient(discord.Client):

    async def on_ready(self):
        print('Logged on as', self.user)

    async def on_message(self, message):
        if message.author == self.user:
            return

        if message.content == 'ping':
            await message.channel.send('pong')

    async def runDiscordBot(self):
        client = MyClient()
        client.run(TOKEN)

Forgive me if this question has already been asked in the past.

0 Answers
Related