From 8da6e1136865f9000dcf9132e7741d0c45f70c5a Mon Sep 17 00:00:00 2001 From: Luke Robles Date: Thu, 21 Sep 2023 11:52:18 -0700 Subject: [PATCH] updates to the readme and flavor text for star citizen incidents --- README.md | 6 +- app/apex_legends.py | 126 +++++++++++++++++++++++++++++++++++ app/cogs/animal_functions.py | 4 +- app/cogs/game_apis.py | 28 ++++---- app/cogs/star_citizen.py | 11 ++- dalebot-overview.py | 14 ++-- helm/values.yaml | 11 +-- 7 files changed, 170 insertions(+), 30 deletions(-) create mode 100755 app/apex_legends.py diff --git a/README.md b/README.md index 55d52c8a..dcefbc97 100755 --- a/README.md +++ b/README.md @@ -1,4 +1,3 @@ -[![pipeline status](https://git.luker.fr/ldooks/dragon-bot/badges/master/pipeline.svg)](http://git.luker.fr/ldooks/dragon-bot/-/commits/master) # README # A discord bot written in python. @@ -11,9 +10,12 @@ Currently the CI/CD pipeline has workers running as Kubernetes pods and dispatch * This is a chat bot of sorts that connects to our [discord](https://discordapp.com/) channel and interacts with us via chat commands * This bot has also been an ongoing learning experience for several members of the discord channel. With this bot, I have been able to teach the repo contributors: * Docker + * Helm + * Kubernetes + * ArgoCD * Git * Python - * CI/CD + * CI/CD (First using gitlab CI, but now I've deployed my own DroneCI cluster) * Collaborative coding practices * Testing methodologies * Code Accountability diff --git a/app/apex_legends.py b/app/apex_legends.py new file mode 100755 index 00000000..27fdb11e --- /dev/null +++ b/app/apex_legends.py @@ -0,0 +1,126 @@ +import datetime +import discord +import os +import requests + + +def get_player(player): + # player = player.lower() + # url = "https://public-api.tracker.gg/v2/apex/standard/profile/origin/" + player + # headers = { + # "User-Agent": "Mozilla/5.0 (Windows NT 6.1; Win64; x64)", + # "accept": "application/json", + # "TRN-Api-Key": os.getenv("tracker_network_token"), + # } + + url = ( + "https://api.mozambiquehe.re/bridge?auth=%s&player=%s&platform=PC&merge=True" + % ( + os.getenv("apex_api_key"), + player, + ) + ) + # print(url) + response = requests.get(url).json() + + # Build the embed + embed = discord.Embed(description="-------", color=discord.Color.red(), type="rich") + embed.set_thumbnail(url=response["global"]["rank"]["rankImg"]) + embed.set_author(name="Apex stats for %s" % response["global"]["name"]) + embed.add_field( + name="**Rank**", + value="%s, %s" + % ( + response["global"]["rank"]["rankName"], + response["global"]["rank"]["rankScore"], + ), + inline=False, + ) + embed.add_field( + name="**Current level**", + value="%s, %s%% of the way to next level" + % ( + response["global"]["level"], + response["global"]["toNextLevelPercent"], + ), + inline=False, + ) + + gameplay_stats = { + "kills": "Total kills", + "kd": "K/D", + "executions": "Executions", + "revives": "Revies", + "games_played": "Games Played", + "headshots": "Headshots", + } + + embed.add_field(name="Totals", value="-------", inline=False) + for stat, readable in gameplay_stats.items(): + embed.add_field( + name="**%s**" % readable, + value=response["total"][stat]["value"], + inline=True, + ) + + embed.add_field( + name="Stats per legend", value="---------------------", inline=False + ) + legends = response["legends"]["all"] + for legend in legends: + if "data" in response["legends"]["all"][legend] and legend != "Global": + data = response["legends"]["all"][legend]["data"] + smush = {x["key"]: x for x in data} + + try: + legends_kills = smush["specialEvent_kills"]["value"] or 0 + legends_damage = smush["specialEvent_damage"]["value"] or 0 + legends_wins = smush["specialEvent_wins"]["value"] or 0 + + embed.add_field( + name=legend, + value="Total kills: %s\nTotal Damage: %s\nTotal Wins: %s" + % (legends_kills, legends_damage, legends_wins), + ) + except Exception as e: + print("%s threw an exception on %s" % (legend, e)) + pass + + # best_weapon = find_best(response["weapons"], "kills") + + # embed.add_field( + # name=":trophy:**Best Weapon**:trophy:", + # value="%s\nkills: %s\naccuracy: %s\nHeadshots: %s" + # % ( + # best_weapon["weaponName"], + # best_weapon["kills"], + # best_weapon["accuracy"], + # best_weapon["headshotKills"], + # ), + # inline=True, + # ) + + # favorite_class = find_best(response["classes"], "secondsPlayed") + + # embed.add_field( + # name=":trophy:**Favorite Class**:trophy:", + # value="%s\nKDR: %s\nTime Played: %s" + # % ( + # favorite_class["characterName"], + # favorite_class["killDeath"], + # str(datetime.timedelta(seconds=favorite_class["secondsPlayed"])), + # ), + # inline=True, + # ) + return embed + + +def find_best(blob, key): + """ + find_best(blob, key) + blob should be the list within the request you want to find the best of, + must be one of [weapopns, vehicles, classes, gamemodes, maps, gadgets], eg. response["weapons"], + and the key to use to make that distinction, for example, for weapons, you could use kills + """ + + return max(blob, key=lambda x: x[key]) diff --git a/app/cogs/animal_functions.py b/app/cogs/animal_functions.py index 214ee362..77ba8e01 100755 --- a/app/cogs/animal_functions.py +++ b/app/cogs/animal_functions.py @@ -74,7 +74,9 @@ class AnimalFunctions(commands.Cog): await ctx.respond(animals.cowboy()) @commands.slash_command( - guild_ids=None, name="dale", description="Posts a photo of the goodest boy" + guild_ids=None, + name="dale", + description="Posts a photo of the goodest boy. Rest in power king", ) async def dale(self, ctx: commands.Context): await ctx.defer() diff --git a/app/cogs/game_apis.py b/app/cogs/game_apis.py index b364aac6..67351522 100755 --- a/app/cogs/game_apis.py +++ b/app/cogs/game_apis.py @@ -6,22 +6,20 @@ class Games(commands.Cog): def __init__(self, bot): self.bot: commands.Bot = bot - # @commands.slash_command( - # guild_ids=None, - # name="bf5", - # description="Query the game's API for data about a player", - # ) - # async def bf5(self, ctx: commands.Context, player): - # import bf5 + @commands.slash_command( + guild_ids=None, + name="apex", + description="Query the game's API for data about a player", + ) + async def apex(self, ctx: commands.Context, player): + import apex_legends - # try: - # await ctx.defer() - # embed = bf5.get_player(player) - # await ctx.send_followup(embed=embed) - # except Exception as e: - # await ctx.send( - # "I encountered an error while searching for that player.\nPlease check that your player name is spelled correctly" - # ) + try: + await ctx.defer() + embed = apex_legends.get_player(player) + await ctx.send_followup(embed=embed) + except Exception as e: + await ctx.send(e) @commands.slash_command( guild_ids=None, diff --git a/app/cogs/star_citizen.py b/app/cogs/star_citizen.py index 5b5c7706..7bf07b2d 100644 --- a/app/cogs/star_citizen.py +++ b/app/cogs/star_citizen.py @@ -154,7 +154,7 @@ class StarCitizen(commands.Cog): url="https://media.robertsspaceindustries.com/t0q21kbb3zrpt/source.png" ) embed.set_author( - name="🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨\n🚨 OH NO THERES AN INCIDENT 🚨\n🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨" + name="🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨\n🚨 OH NO THERES AN INCIDENT 🚨\n🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨🚨" ) embed.set_image( url="https://media.giphy.com/media/WWIZJyXHXscn8JC1e0/giphy.gif" @@ -168,6 +168,15 @@ class StarCitizen(commands.Cog): ) await self.bot.get_channel(1097567909640929340).send(embed=embed) + # sent_embed = await self.bot.get_channel(932476007439552522).send( + # embed=embed + # ) + + # # If the devs update the content of the incident, update the embed with the new text + # if details.text != sent_embed.embeds[0].fields[0].value: + # sent_embed.embeds[0].set_field_at( + # index=0, name="Details", value=details.text, inline=True + # ) def setup(bot): diff --git a/dalebot-overview.py b/dalebot-overview.py index ef81557d..f4370aa6 100644 --- a/dalebot-overview.py +++ b/dalebot-overview.py @@ -1,7 +1,7 @@ from diagrams import Cluster, Diagram, Edge from diagrams.onprem.gitops import Argocd -from diagrams.onprem.vcs import Gitlab -from diagrams.onprem.ci import Gitlabci +from diagrams.onprem.vcs import Gitea +from diagrams.onprem.ci import DroneCI from diagrams.generic.os import LinuxGeneral from diagrams.k8s.compute import Pod from diagrams.onprem.container import Docker @@ -13,8 +13,8 @@ from diagrams.k8s.podconfig import Secret with Diagram("Dalebot overview", show=False): user = LinuxGeneral("User") with Cluster("On-Prem Kubernetes"): - gitlab = Gitlab("Gitlab") - gitlabci = Gitlabci("Gitlab CI runner") + gitea = Gitea("Gitea") + droneci = DroneCI("DroneCI runner") argocd = Argocd("ArgoCD") secrets = Secret("API Keys") bot = Pod("Dale-bot") @@ -22,10 +22,10 @@ with Diagram("Dalebot overview", show=False): dockerhub = Docker("Dockerhub") storage = Server("On-prem Storage") - user >> Edge(label="Push") >> gitlab >> gitlabci - gitlabci >> Edge(label="Application sync") >> dockerhub + user >> Edge(label="Push") >> gitea >> droneci + droneci >> Edge(label="Application sync") >> dockerhub dockerhub >> argocd - gitlabci >> Edge(label="Application sync") >> argocd + droneci >> Edge(label="Application sync") >> argocd storage >> bot secrets >> bot argocd >> bot diff --git a/helm/values.yaml b/helm/values.yaml index 64ac1460..128d9ec5 100755 --- a/helm/values.yaml +++ b/helm/values.yaml @@ -83,10 +83,13 @@ env: PYTHONUNBUFFERED: 1 secrets: + DRAGON_ENV: DRAGON_ENV + discord_token: discord_token ffxiv_token: ffxiv_token gitlab_token: gitlab_token - OPENAI_API_KEY: OPENAI_API_KEY - token: discord_token twitch_token: twitch_token - uexcorp_key: uexcorp_key - wolfram_token: wolfram_token \ No newline at end of file + wolfram_token: wolfram_token + OPENAI_API_KEY: OPENAI_API_KEY + apex_api_key: apex_api_key + star_citizen_token: star_citizen_token + uexcorp_key: uexcorp_key \ No newline at end of file