2020-08-31 19:58:01 +02:00
import asyncio
from redbot . core import commands
from redbot . core import Config , checks
from discord import TextChannel
import traceback
import requests
class WynncraftCog ( commands . Cog ) :
2020-08-31 20:03:24 +02:00
def __init__ ( self , bot ) :
self . bot = bot
2020-08-31 19:58:01 +02:00
self . config = Config . get_conf ( self , identifier = 48775419874 )
default_guild = {
" tracked " : [ ] ,
" channel " : None
}
self . config . register_guild ( * * default_guild )
@commands.command ( )
@checks.admin_or_permissions ( manage_guild = True )
async def track ( self , ctx , username : str ) :
tracked = await self . config . guild ( ctx . guild ) . tracked ( )
if username in tracked :
await ctx . send ( " :x: ** %s ** est déjà tracké " % username )
else :
tracked . append ( username )
await self . config . guild ( ctx . guild ) . tracked . set ( tracked )
await ctx . send ( " :white_check_mark: ** %s ** est désormais tracké " % username )
@commands.command ( )
@checks.admin_or_permissions ( manage_guild = True )
async def untrack ( self , ctx , username : str ) :
tracked = await self . config . guild ( ctx . guild ) . tracked ( )
if username in tracked :
tracked . remove ( username )
await self . config . guild ( ctx . guild ) . tracked . set ( tracked )
await ctx . send ( " :white_check_mark: ** %s ** ne sera plus tracké " % username )
else :
await ctx . send ( " :x: ** %s ** n ' était pas tracké " % username )
@commands.command ( )
@checks.admin_or_permissions ( manage_guild = True )
async def tracked ( self , ctx ) :
tracked = await self . config . guild ( ctx . guild ) . tracked ( )
await ctx . send ( " Les joueurs trackés sont: %s " % " , " . join ( tracked ) )
@commands.command ( )
@checks.admin_or_permissions ( manage_guild = True )
async def set_channel ( self , ctx , channel : TextChannel ) :
i = channel . id
await self . config . guild ( ctx . guild ) . channel . set ( i )
await ctx . send ( " Les messages seront désormais envoyés dans %s " % channel . mention )
async def loop ( self ) :
await self . bot . wait_until_ready ( )
last_status = { }
while self is self . bot . get_cog ( " WynncraftCog " ) :
2020-08-31 20:06:55 +02:00
print ( " Looping guilds " )
2020-08-31 19:58:01 +02:00
async for guild in self . bot . fetch_guilds ( limit = 150 ) :
guild_id = guild . id
2020-08-31 20:06:55 +02:00
print ( " Loop id %s " % guild_id )
2020-08-31 19:58:01 +02:00
try :
channel_id = await self . config . guild ( guild ) . channel ( )
if channel_id :
2020-08-31 20:06:55 +02:00
print ( " Channel set " )
2020-08-31 19:58:01 +02:00
for tracked in await self . config . guild ( guild ) . tracked ( ) :
2020-08-31 20:06:55 +02:00
print ( " calling API for player %s " % tracked )
2020-08-31 19:58:01 +02:00
r = requests . get ( " https://api.wynncraft.com/v2/player/ %s /stats " % tracked ) . json ( )
for cl in r [ " data " ] [ 0 ] [ " classes " ] :
if guild_id not in last_status :
last_status [ guild_id ] = { }
if tracked not in last_status [ guild_id ] :
last_status [ guild_id ] [ tracked ] = { }
if cl [ " name " ] not in last_status [ guild_id ] [ tracked ] :
last_status [ guild_id ] [ tracked ] [ cl [ " name " ] ] = cl
if cl [ " professions " ] [ " combat " ] [ " level " ] > last_status [ guild_id ] [ tracked ] [ cl [ " name " ] ] [ " professions " ] [ " combat " ] [ " level " ] :
channel = self . bot . get_channel ( channel_id )
await channel . send ( " :high_brightness: %s a level up au niveau %s ! GG! " % ( tracked , cl [ " professions " ] [ " combat " ] [ " level " ] ) )
2020-08-31 20:06:55 +02:00
else :
print ( " Player %s had last level %s and current %s " % ( tracked , last_status [ guild_id ] [ tracked ] [ cl [ " name " ] ] [ " professions " ] [ " combat " ] [ " level " ] , cl [ " professions " ] [ " combat " ] [ " level " ] ) )
2020-08-31 19:58:01 +02:00
last_status [ guild_id ] [ tracked ] [ cl [ " name " ] ] = cl
2020-08-31 20:06:55 +02:00
else :
print ( " Guild has no channel set " )
2020-08-31 19:58:01 +02:00
except :
traceback . print_exc ( )
await asyncio . sleep ( 30 )