diff --git a/bases/bot_detector/discord_bot/cogs/mod_commands.py b/bases/bot_detector/discord_bot/cogs/mod_commands.py index 5a64f4d..7cbe291 100644 --- a/bases/bot_detector/discord_bot/cogs/mod_commands.py +++ b/bases/bot_detector/discord_bot/cogs/mod_commands.py @@ -6,6 +6,8 @@ DISCORD_STAFF, OWNER_ROLE, VERIFICATION_STAFF, + build_kc_embed, + resolve_primary_rsn, ) from discord.ext import commands from discord.ext.commands import Cog, Context @@ -79,3 +81,55 @@ async def admin_linked(self, ctx: Context, discord_id: str): # check if there are any embeds left if embeds != []: await ctx.reply(embeds=embeds) + + @commands.hybrid_command() + @commands.has_any_role(DISCORD_STAFF, VERIFICATION_STAFF, OWNER_ROLE) + async def admin_kc(self, ctx: Context, discord_id: str): + """Shows the report (KC) stats of a user's linked accounts. + + :param ctx: The context of the command. + :param discord_id: The Discord ID of the user to look up. + """ + debug = { + "author": ctx.author.name, + "author_id": ctx.author.id, + "msg": f"is using admin_kc for {discord_id}", + } + logger.debug(debug) + await ctx.typing() + + assert self.deps.legacy_api is not None + linked_accounts = await self.deps.legacy_api.get_discord_links( + discord_id=str(discord_id) + ) + + if not linked_accounts: + await ctx.reply( + f"No linked OSRS accounts found for Discord ID {discord_id}." + ) + return + + linked_accounts = [ + {"name": acc.get("name"), "primary_rsn": acc.get("primary_rsn")} + for acc in linked_accounts + if acc.get("Verified_status") == 1 + ] + + if not linked_accounts: + await ctx.reply( + f"None of the accounts linked to {discord_id} are verified." + ) + return + + assert self.deps.public_api is not None + data = await self.deps.public_api.get_report_score( + names=[n["name"] for n in linked_accounts] + ) + + if not data: + await ctx.reply("No data found.") + return + + primary_rsn = resolve_primary_rsn(linked_accounts) + embed = build_kc_embed(primary_rsn=primary_rsn, data=data) + await ctx.reply(embed=embed) diff --git a/bases/bot_detector/discord_bot/cogs/player_stats_commands.py b/bases/bot_detector/discord_bot/cogs/player_stats_commands.py index b142d74..50027f6 100644 --- a/bases/bot_detector/discord_bot/cogs/player_stats_commands.py +++ b/bases/bot_detector/discord_bot/cogs/player_stats_commands.py @@ -5,7 +5,11 @@ import discord from bot_detector.discord_bot.dependencies import BotDependencies -from bot_detector.discord_bot.utils import VERIFIED_PLAYER_ROLE +from bot_detector.discord_bot.utils import ( + VERIFIED_PLAYER_ROLE, + build_kc_embed, + resolve_primary_rsn, +) from discord import Color, Embed from discord.ext import commands from discord.ext.commands import Cog, Context @@ -280,84 +284,8 @@ async def kc(self, ctx: Context): await ctx.reply("No data found.") return - reports_submitted = sum(d.count for d in data if not d.manual_detect) - possible_bans = sum( - d.count - for d in data - if not d.manual_detect and not d.confirmed_ban and d.possible_ban - ) - confirmed_bans = sum( - d.count - for d in data - if not d.manual_detect and d.confirmed_ban and d.possible_ban - ) - - manual_confirmed_ban = sum( - d.count for d in data if d.manual_detect and d.confirmed_ban - ) - manual_confirmed_player = sum( - d.count - for d in data - if d.manual_detect and not d.confirmed_ban and d.confirmed_player - ) - manual_flags = sum(d.count for d in data if d.manual_detect) - confirmed_manual_flags = manual_confirmed_ban + manual_confirmed_player - manual_flag_accuracy = ( - (manual_confirmed_ban / confirmed_manual_flags) * 100 - if confirmed_manual_flags - else 0 - ) - logger.info( - f"{manual_flags=}, {confirmed_manual_flags=}, {manual_confirmed_ban=}, {manual_confirmed_player=}" - ) - - primary_rsn = None - for account in linked_accounts: - if account.get("primary_rsn") == 1: - primary_rsn = account.get("name") - - if not primary_rsn and linked_accounts: - primary_rsn = linked_accounts[0].get("name") - - embed = discord.Embed(title=f"{primary_rsn}'s Stats", color=0x00FF00) - embed.add_field( - name="Reports Submitted (Auto):", - value=f"{reports_submitted:,}", - inline=False, - ) - embed.add_field( - name="Possible Bans (Auto):", - value=f"{possible_bans:,}", - inline=False, - ) - embed.add_field( - name="Confirmed Bans (Auto):", - value=f"{confirmed_bans:,}", - inline=False, - ) - - if manual_flags: - embed.add_field( - name="Manual Flags:", value=f"{manual_flags:,}", inline=False - ) - embed.add_field( - name="Manual Flag Accuracy:", - value=f"{manual_flag_accuracy:.2f}%", - inline=False, - ) - - embed.set_thumbnail( - url="https://user-images.githubusercontent.com/5789682/117364618-212a3200-ae8c-11eb-8b42-9ef5e225930d.gif" - ) - - if reports_submitted == 0: - embed.set_footer( - text=( - "If you have the plugin installed but are not seeing your KC increase, " - "you may have to disable Anonymous Mode in your plugin settings." - ), - icon_url="https://raw.githubusercontent.com/Bot-detector/bot-detector/master/src/main/resources/warning.png", - ) + primary_rsn = resolve_primary_rsn(linked_accounts) + embed = build_kc_embed(primary_rsn=primary_rsn, data=data) await ctx.reply(embed=embed) @commands.hybrid_command() diff --git a/bases/bot_detector/discord_bot/utils/__init__.py b/bases/bot_detector/discord_bot/utils/__init__.py index 307c3dd..b7451cb 100644 --- a/bases/bot_detector/discord_bot/utils/__init__.py +++ b/bases/bot_detector/discord_bot/utils/__init__.py @@ -20,6 +20,7 @@ is_allowed_channel, is_supporter, ) +from bot_detector.discord_bot.utils.kc_embed import build_kc_embed, resolve_primary_rsn from bot_detector.discord_bot.utils.string_processing import ( get_random_id, is_valid_rsn, @@ -52,4 +53,6 @@ "is_supporter", "BOOSTER_ROLE", "SUPPORTER_ROLE", + "build_kc_embed", + "resolve_primary_rsn", ] diff --git a/bases/bot_detector/discord_bot/utils/kc_embed.py b/bases/bot_detector/discord_bot/utils/kc_embed.py new file mode 100644 index 0000000..cf7cb0f --- /dev/null +++ b/bases/bot_detector/discord_bot/utils/kc_embed.py @@ -0,0 +1,82 @@ +import discord +from bot_detector.public_api import ReportScoreResponse + + +def resolve_primary_rsn(accounts: list[dict]) -> str | None: + for account in accounts: + if account.get("primary_rsn") == 1: + return account.get("name") + if accounts: + return accounts[0].get("name") + return None + + +def build_kc_embed( + primary_rsn: str | None, data: list[ReportScoreResponse] +) -> discord.Embed: + reports_submitted = sum(d.count for d in data if not d.manual_detect) + possible_bans = sum( + d.count + for d in data + if not d.manual_detect and not d.confirmed_ban and d.possible_ban + ) + confirmed_bans = sum( + d.count + for d in data + if not d.manual_detect and d.confirmed_ban and d.possible_ban + ) + + manual_confirmed_ban = sum( + d.count for d in data if d.manual_detect and d.confirmed_ban + ) + manual_confirmed_player = sum( + d.count + for d in data + if d.manual_detect and not d.confirmed_ban and d.confirmed_player + ) + manual_flags = sum(d.count for d in data if d.manual_detect) + confirmed_manual_flags = manual_confirmed_ban + manual_confirmed_player + manual_flag_accuracy = ( + (manual_confirmed_ban / confirmed_manual_flags) * 100 + if confirmed_manual_flags + else 0 + ) + + embed = discord.Embed(title=f"{primary_rsn}'s Stats", color=0x00FF00) + embed.add_field( + name="Reports Submitted (Auto):", + value=f"{reports_submitted:,}", + inline=False, + ) + embed.add_field( + name="Possible Bans (Auto):", + value=f"{possible_bans:,}", + inline=False, + ) + embed.add_field( + name="Confirmed Bans (Auto):", + value=f"{confirmed_bans:,}", + inline=False, + ) + + if manual_flags: + embed.add_field(name="Manual Flags:", value=f"{manual_flags:,}", inline=False) + embed.add_field( + name="Manual Flag Accuracy:", + value=f"{manual_flag_accuracy:.2f}%", + inline=False, + ) + + embed.set_thumbnail( + url="https://user-images.githubusercontent.com/5789682/117364618-212a3200-ae8c-11eb-8b42-9ef5e225930d.gif" + ) + + if reports_submitted == 0: + embed.set_footer( + text=( + "If you have the plugin installed but are not seeing your KC increase, " + "you may have to disable Anonymous Mode in your plugin settings." + ), + icon_url="https://raw.githubusercontent.com/Bot-detector/bot-detector/master/src/main/resources/warning.png", + ) + return embed