wynncraft-bot/cogs/wynncraft/wynncraftcog.py

83 lines
3.6 KiB
Python
Raw Normal View History

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"):
async for guild in self.bot.fetch_guilds(limit=150):
guild_id = guild.id
try:
channel_id = await self.config.guild(guild).channel()
if channel_id:
for tracked in await self.config.guild(guild).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"]))
last_status[guild_id][tracked][cl["name"]] = cl
except:
traceback.print_exc()
await asyncio.sleep(30)