-1

I am trying to have a server that will be able to give us the status on discord. I did achieve this by having an extra socketio client that run discord.py aswell. I would like discord to run on the server instead of on a client. I am unable to make this work at the same time, any insight or help would be appreciated.

I tried launching discord before socketio, also tried launching discord in a different thread.

--Update--
The orginal post had a emit onConenct. this is apparently a known issue and will cause a namespace error. . please find trhe edited code below

as a minimal reproductable example here is the code:

server:

import asyncio
from time import sleep
from flask import Flask
from aiohttp import web
import socketio
from discord.ext import commands

sio = socketio.AsyncServer(async_mode='aiohttp',logger=True)
app = web.Application()
sio.attach(app)
bot = commands.Bot(command_prefix='+')


@bot.command(help="testing")
async def test(ctx):
    await ctx.send("Hello world!")
    await sio.emit('marco')


@sio.on('polo')
async def message(sid):
    print("a wild client responded")

async def start_discord():
    print('starting discord')
    await bot.start('token')


if __name__ == '__main__':
    asyncio.run(asyncio.gather(web.run_app(app),start_discord()))

client :

import asyncio
from click import prompt
import socketio
from discord.ext import commands

sio = socketio.Client(logger=True)

@sio.on("marco")
def polo():
    sio.emit('polo')
    print("wow i found a server")

if __name__ == "__main__":
    sio.connect("http://localhost:8080")
    sio.wait()

Ther server and client are connecting but not discord.

Farid Fakhry
  • 354
  • 1
  • 10

1 Answers1

1

Your issue is that the socket server only starts when the discord bot exists. This will never happen.

If you want to run 2 functions concurrently use asyncio.gather in combination with client.start

Like this:

if __name__ == '__main__':
    asyncio.run(
        asyncio.gather(
             bot.start('SuperSecret Token')
             sio.start(app, host="localhost", port=8080)
        )
   )

This will ensure they start the the same time.

Note your code to create a custom event loop is probably unnecessary.

Having multiple calls to asyncio.run in your code is a mistake 99% of the time. You normally need only one async context.

mousetail
  • 7,009
  • 4
  • 25
  • 45
  • Thank you for the explanation! however the same issue persist. If i run discord first the server doesn't run, and if i put the server first the discord doesn't run. I'm gonna try to create a shared pipe between my standalone discord program and my sio server – Farid Fakhry Jul 15 '22 at 14:52
  • Oh rigth, neither of these actually return futures – mousetail Jul 15 '22 at 14:52
  • @FaridFakhry Use the `start` method instead of the `run` method – mousetail Jul 15 '22 at 14:53
  • soryr i should have seen this earlier, but I am running the server with discord not the client. I did change it from a regular server to an async server with aiohttp. I updated the code online. the server and client run but not discord – Farid Fakhry Jul 15 '22 at 15:35