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
}
2020-08-31 20:25:45 +02:00
default_global = {
" log " : True
}
2020-08-31 19:58:01 +02:00
self . config . register_guild ( * * default_guild )
2020-08-31 20:25:45 +02:00
self . config . register_global ( * * default_global )
2020-08-31 19:58:01 +02:00
@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 )
2020-08-31 20:25:45 +02:00
@commands.command ( )
@checks.is_owner ( )
async def log ( self , ctx , state ) :
state = state . lower ( )
if state == " true " or state == " on " :
await self . config . log . set ( True )
await ctx . send ( " :white_check_mark: " )
elif state == " false " or state == " off " :
await self . config . log . set ( False )
await ctx . send ( " :white_check_mark: " )
else :
await ctx . send ( " :x: " )
def _log ( self , s ) :
if await self . config . log ( ) :
print ( s )
2020-08-31 19:58:01 +02:00
async def loop ( self ) :
await self . bot . wait_until_ready ( )
last_status = { }
while self is self . bot . get_cog ( " WynncraftCog " ) :
2020-08-31 20:25:45 +02:00
self . _log ( " 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:25:45 +02:00
self . _log ( " 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:25:45 +02:00
self . _log ( " Channel set " )
2020-08-31 19:58:01 +02:00
for tracked in await self . config . guild ( guild ) . tracked ( ) :
2020-08-31 20:25:45 +02:00
self . _log ( " 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 :
2020-08-31 20:25:45 +02:00
self . _log ( " 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 :
2020-08-31 20:25:45 +02:00
self . _log ( " Guild has no channel set " )
2020-08-31 19:58:01 +02:00
except :
2020-08-31 20:25:45 +02:00
if await self . config . log ( ) :
traceback . print_exc ( )
2020-08-31 19:58:01 +02:00
await asyncio . sleep ( 30 )