This commit is contained in:
Amazed 2020-08-31 23:56:09 +02:00
parent 3082061cae
commit e99409c089

View File

@ -2,6 +2,7 @@ import asyncio
from redbot.core import commands from redbot.core import commands
from redbot.core import Config, checks from redbot.core import Config, checks
from discord import TextChannel from discord import TextChannel
import discord
import traceback import traceback
import requests import requests
@ -11,8 +12,30 @@ class WynncraftCog(commands.Cog):
self.bot = bot self.bot = bot
self.config = Config.get_conf(self, identifier=48775419874) self.config = Config.get_conf(self, identifier=48775419874)
default_guild = { default_guild = {
"tracked": [], "guild_name": None, # OK
"channel": None "ping_levels": False, # OK
"ping_channel": None, # OK
"role_levels": [
750098524976185525, # 0-9
750098542802239488, # 10-19
750098546883166292, # 20-29
750098551756947496, # 30-39
750098557444292740, # 40-49
750098560732889088, # 50-59
750098568194424872, # 60-69
750098573353418822, # 70-79
750098579535822918, # 80-89
750098584082448434, # 90-99
750098593976811550, # 100+
],
"role_class": {
"mage": 750097053840965752,
"assassin": 750097058362425417,
"shaman": 750097056093438012,
"archer": 750097050867204246,
"warrior": 750097044433272833
},
"update_msg": 123456
} }
default_global = { default_global = {
"log": True "log": True
@ -22,38 +45,29 @@ class WynncraftCog(commands.Cog):
@commands.command() @commands.command()
@checks.admin_or_permissions(manage_guild=True) @checks.admin_or_permissions(manage_guild=True)
async def track(self, ctx, username: str): async def ping_channel(self, ctx, channel: TextChannel):
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 i = channel.id
await self.config.guild(ctx.guild).channel.set(i) await self.config.guild(ctx.guild).ping_channel.set(i)
await ctx.send("Les messages seront désormais envoyés dans %s" % channel.mention) await self.config.guild(ctx.guild).ping_levels.set(True)
await ctx.send("Les messages de montée de niveau seront désormais envoyés dans %s" % channel.mention)
@commands.command()
@checks.admin_or_permissions(manage_guild=True)
async def ping_levels(self, ctx, state):
if state == "true" or state == "on":
await self.config.ping_levels.set(True)
await ctx.send(":white_check_mark: Activé!")
elif state == "false" or state == "off":
await self.config.ping_levels.set(False)
await ctx.send(":white_check_mark: Désactivé!")
else:
await ctx.send(":x:")
@commands.command()
@checks.admin_or_permissions(manage_guild=True)
async def set_guild(self, ctx, guild_name):
await self.config.guild(ctx.guild).guild_name.set(guild_name)
await ctx.send(":white_check_mark: Votre guilde est désormais **%s**" % guild_name)
@commands.command() @commands.command()
@checks.is_owner() @checks.is_owner()
@ -62,9 +76,11 @@ class WynncraftCog(commands.Cog):
if state == "true" or state == "on": if state == "true" or state == "on":
await self.config.log.set(True) await self.config.log.set(True)
await ctx.send(":white_check_mark:") await ctx.send(":white_check_mark:")
print("Logs on")
elif state == "false" or state == "off": elif state == "false" or state == "off":
await self.config.log.set(False) await self.config.log.set(False)
await ctx.send(":white_check_mark:") await ctx.send(":white_check_mark:")
print("Logs off")
else: else:
await ctx.send(":x:") await ctx.send(":x:")
@ -78,34 +94,66 @@ class WynncraftCog(commands.Cog):
while self is self.bot.get_cog("WynncraftCog"): while self is self.bot.get_cog("WynncraftCog"):
await self._log("Looping guilds") await self._log("Looping guilds")
async for guild in self.bot.fetch_guilds(limit=150): async for guild in self.bot.fetch_guilds(limit=150):
roles_combat_level = await self.config.guild(guild).role_levels()
guild_id = guild.id guild_id = guild.id
await self._log("Loop id %s" % guild_id) await self._log("Loop id %s" % guild_id)
try: try:
channel_id = await self.config.guild(guild).channel() guild_name = await self.config.guild(guild).guild_name()
if channel_id: r = requests.get("https://api.wynncraft.com/public_api.php?action=guildStats&command=%s" % guild_name).json()
await self._log("Channel set") for member in r["members"]:
for tracked in await self.config.guild(guild).tracked(): member_name = member["name"]
await self._log("calling API for player %s" % tracked) await self._log("calling API for player %s" % member_name)
r = requests.get("https://api.wynncraft.com/v2/player/%s/stats" % tracked).json() r = requests.get("https://api.wynncraft.com/v2/player/%s/stats" % member_name).json()
max_combat_lvl_class = None
for cl in r["data"][0]["classes"]: for cl in r["data"][0]["classes"]:
if guild_id not in last_status: if guild_id not in last_status:
last_status[guild_id] = {} last_status[guild_id] = {}
if tracked not in last_status[guild_id]: if member_name not in last_status[guild_id]:
last_status[guild_id][tracked] = {} last_status[guild_id][member_name] = {}
if cl["name"] not in last_status[guild_id][tracked]: if cl["name"] not in last_status[guild_id][member_name]:
last_status[guild_id][tracked][cl["name"]] = cl last_status[guild_id][member_name][cl["name"]] = cl
if cl["professions"]["combat"]["level"] > last_status[guild_id][tracked][cl["name"]]["professions"]["combat"]["level"]: # ping level up
channel = self.bot.get_channel(channel_id) ping_channel_id = await self.config.guild(guild).ping_channel()
await channel.send(":high_brightness: %s a level up au niveau %s! GG!" % (tracked, cl["professions"]["combat"]["level"])) if await self.config.guild(guild).ping_levels() and ping_channel_id:
await self._log("Checking for level up")
if cl["professions"]["combat"]["level"] > last_status[guild_id][member_name][cl["name"]]["professions"]["combat"]["level"]:
ping_channel = self.bot.get_channel(ping_channel_id)
await ping_channel.send(":high_brightness: %s a level up au niveau %s! GG!" % (member_name, cl["professions"]["combat"]["level"]))
else: else:
await 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"])) await self._log("Player %s had last level %s and current %s" % (member_name, last_status[guild_id][member_name][cl["name"]]["professions"]["combat"]["level"], cl["professions"]["combat"]["level"]))
last_status[guild_id][tracked][cl["name"]] = cl
else: else:
await self._log("Guild has no channel set") await self._log("Not checking for level up")
last_status[guild_id][member_name][cl["name"]] = cl
# get max_combat_lvl_class
if cl["professions"]["combat"]["level"] > max_combat_lvl_class["professions"]["combat"]["level"]:
max_combat_lvl_class = cl
await self._log("Setting max_combat_lvl_class role")
# set max_combat_lvl_class role
discord_member = discord.utils.find(lambda m: m.display_name == member_name, guild.members)
if max_combat_lvl_class and discord_member:
await self._log("max_combat_lvl_class and discord_member True")
max_combat_lvl = max_combat_lvl_class["professions"]["combat"]["level"]
combat_role_index = max_combat_lvl // 10
if combat_role_index > 10: combat_role_index = 10
combat_role_id = roles_combat_level[combat_role_index]
combat_role = self.bot.get_role(combat_role_id)
if combat_role not in discord_member.roles:
await self._log("Add role: %s" % combat_role)
# await discord_member.add_roles(combat_role)
roles_to_remove = filter(lambda role: role.id in roles_combat_level and role.id != combat_role_id, discord_member.roles)
if roles_to_remove:
# await discord_member.remove_roles(roles_to_remove)
await self._log("remove roles: %s" % roles_to_remove)
else:
await self._log("max_combat_lvl_class and discord_member False")
except: except:
if await self.config.log(): if await self.config.log():
traceback.print_exc() traceback.print_exc()