Browse Source

task updater

huy
gsd 2 years ago
parent
commit
642f34e5cc
  1. 24
      other_ext/stats_loader.py
  2. 42
      other_ext/stats_presence.py

24
other_ext/stats_loader.py

@ -1,12 +1,22 @@
from discord.ext import tasks
import traceback
import asyncio
class Extension:
core = None
def __init__(self, core):
@tasks.loop(seconds=60.0)
async def stats_loader(core):
try:
print("Load stats from backend")
core.stats = await core.GetStats()
except:
traceback.print_exc()
self.core = core
self.core.loop.create_task(self.task())
async def task(self, timeout = 60):
await self.core.wait_until_ready()
while True:
await self.updater()
await asyncio.sleep(timeout)
async def updater(self):
try:
print("Load stats from backend")
self.core.stats = await self.core.GetStats()
except:
traceback.print_exc()

42
other_ext/stats_presence.py

@ -1,25 +1,35 @@
from discord.ext import tasks
import discord
import traceback
import asyncio
class Extension:
show_stats_prev = 0
core = None
def __init__(self, core):
@tasks.loop(seconds=3)
async def show_stats(core):
try:
if not core.stats['servers'].items():
print("Stats not be loaded")
return
try:
server = core.stats['servers'].items()[self.show_stats_prev]
self.show_stats_prev += 1
except:
self.show_stats_prev = 0
server = core.stats['servers'].items()[self.show_stats_prev]
self.core = core
self.core.loop.create_task(self.task())
async def task(self, timeout = 3):
await self.core.wait_until_ready()
while True:
await self.updater()
await asyncio.sleep(timeout)
addr = server['address'].split(":")
act = discord.Streaming(name = f"{server['name']} - {server['player_count']}", url=f"https://{addr[0]}/connect/{addr[1]}")
await core.change_presence(activity=act)
async def updater(self):
try:
if not self.core.stats['servers'].items():
print("Stats not be loaded")
return
try:
server = self.core.stats['servers'].items()[self.show_stats_prev]
self.show_stats_prev += 1
except:
traceback.print_exc()
self.show_stats_prev = 0
server = self.core.stats['servers'].items()[self.show_stats_prev]
addr = server['address'].split(":")
act = discord.Streaming(name = f"{server['name']} - {server['player_count']}", url=f"https://{addr[0]}/connect/{addr[1]}")
await self.core.change_presence(activity=act)
except:
traceback.print_exc()
Loading…
Cancel
Save