113 lines
4.8 KiB
Python
113 lines
4.8 KiB
Python
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):
|
|
def __init__(self, bot):
|
|
self.bot = bot
|
|
self.config = Config.get_conf(self, identifier=48775419874)
|
|
default_guild = {
|
|
"tracked": [],
|
|
"channel": None
|
|
}
|
|
default_global = {
|
|
"log": True
|
|
}
|
|
self.config.register_guild(**default_guild)
|
|
self.config.register_global(**default_global)
|
|
|
|
@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)
|
|
|
|
@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)
|
|
|
|
async def loop(self):
|
|
await self.bot.wait_until_ready()
|
|
last_status = {}
|
|
while self is self.bot.get_cog("WynncraftCog"):
|
|
self._log("Looping guilds")
|
|
async for guild in self.bot.fetch_guilds(limit=150):
|
|
guild_id = guild.id
|
|
self._log("Loop id %s" % guild_id)
|
|
try:
|
|
channel_id = await self.config.guild(guild).channel()
|
|
if channel_id:
|
|
self._log("Channel set")
|
|
for tracked in await self.config.guild(guild).tracked():
|
|
self._log("calling API for player %s" % tracked)
|
|
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"]))
|
|
else:
|
|
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"]))
|
|
|
|
last_status[guild_id][tracked][cl["name"]] = cl
|
|
else:
|
|
self._log("Guild has no channel set")
|
|
except:
|
|
if await self.config.log():
|
|
traceback.print_exc()
|
|
await asyncio.sleep(30)
|