Skip to content

Commit

Permalink
Merge pull request #119 from Freelancers-Union/arma_commands
Browse files Browse the repository at this point in the history
Added `arma_get_players` command.
  • Loading branch information
NecronicalPug authored Mar 23, 2024
2 parents b006ae5 + ab22228 commit f716d8b
Show file tree
Hide file tree
Showing 7 changed files with 92 additions and 2 deletions.
2 changes: 2 additions & 0 deletions .github/workflows/build-and-deploy-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ jobs:
MONGO_ADDRESS: mongodb-dev
MONGO_USERNAME: ${{ secrets.MONGO_USERNAME }}
MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }}
ARMA_DB_HOST: ${{ secrets.ARMA_DB_HOST }}
ARMA_DB_TOKEN: ${{ secrets.ARMA_DB_TOKEN }}
ARMA_HOST: ${{ secrets.ARMA_HOST }}
ARMA_QUERY_PORT: ${{ secrets.ARMA_QUERY_PORT }}
ARMA_SFTP_HOST: ${{ secrets.ARMA_SFTP_HOST }}
Expand Down
2 changes: 2 additions & 0 deletions .github/workflows/build-and-deploy.yml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ jobs:
MONGO_DATA_DIR: /var/lib/FUBot/mongodb
MONGO_USERNAME: ${{ secrets.MONGO_USERNAME }}
MONGO_PASSWORD: ${{ secrets.MONGO_PASSWORD }}
ARMA_DB_HOST: ${{ secrets.ARMA_DB_HOST }}
ARMA_DB_TOKEN: ${{ secrets.ARMA_DB_TOKEN }}
ARMA_HOST: ${{ secrets.ARMA_HOST }}
ARMA_QUERY_PORT: ${{ secrets.ARMA_QUERY_PORT }}
ARMA_SFTP_HOST: ${{ secrets.ARMA_SFTP_HOST }}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose-dev.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
- MONGO_ADDRESS=${MONGO_ADDRESS}
- MONGO_USERNAME=${MONGO_USERNAME}
- MONGO_PASSWORD=${MONGO_PASSWORD}
- ARMA_DB_HOST=${ARMA_DB_HOST}
- ARMA_DB_TOKEN=${ARMA_DB_TOKEN}
- ARMA_HOST=${ARMA_HOST}
- ARMA_QUERY_PORT=${ARMA_QUERY_PORT}
- ARMA_SFTP_HOST=${ARMA_SFTP_HOST}
Expand Down
2 changes: 2 additions & 0 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ services:
- MONGO_ADDRESS=${MONGO_ADDRESS}
- MONGO_USERNAME=${MONGO_USERNAME}
- MONGO_PASSWORD=${MONGO_PASSWORD}
- ARMA_DB_HOST=${ARMA_DB_HOST}
- ARMA_DB_TOKEN=${ARMA_DB_TOKEN}
- ARMA_HOST=${ARMA_HOST}
- ARMA_QUERY_PORT=${ARMA_QUERY_PORT}
- ARMA_SFTP_HOST=${ARMA_SFTP_HOST}
Expand Down
81 changes: 81 additions & 0 deletions src/commands/arma_get_db.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
import aiohttp
import disnake
from disnake.ext import commands
import logging
import os
import pandas as pd


async def get_players():
"""
Gets the list of players that have played on the Arma server.
"""
host = os.getenv("ARMA_DB_HOST")
api_key = os.getenv("ARMA_DB_TOKEN")
if not (host and api_key):
raise EnvironmentError("Missing configuration for Arma DB.")

# Prepare the url and headers for the request.
url = f"https://{host}/attendance"

headers = {
"content-type": "application/json",
"x-apikey": api_key,
"cache-control": "no-cache",
}

# Open asynchronous HTTP session and get the collection of players.
async with aiohttp.ClientSession(headers=headers) as session:
async with session.get(url) as response:
assert response.status == 200, f"Failed to get players: {response.status}"
records = await response.json()
new_records = []

# Check if the response is a list and if it is empty.
if type(records) is list:
if len(records) == 0:
return "No players found."

# Prepare the data for a pandas DataFrame.
for x, y in enumerate(records):
new_records.append(
(
y["Profile_Name"],
y["Missions_Attended"],
y["Date_Of_Last_Mission"].split("T")[0],
y["Steam_UID"],
)
)

df = pd.DataFrame(
new_records,
index=None,
columns=("Name", "Missions Attended", "Last Seen", "Steam UID"),
)

return f"```{df.to_string(index=False)}\nTotal Players Tracked: {len(df)}```"

else:
logging.error("Response is not a list.")
raise Exception("Response is not a list.")


class ArmAGetDB(commands.Cog):
"""
Class cog for ps2 squad markup help message.
"""

@commands.slash_command(dm_permission=True)
async def arma_get_players(self, inter: disnake.ApplicationCommandInteraction):
"""
Display a list of players that have played on the Arma server.
"""
await inter.response.defer(ephemeral=True)

message = await get_players()

await inter.edit_original_message(content=message)


def setup(bot: commands.Bot):
bot.add_cog(ArmAGetDB(bot))
2 changes: 1 addition & 1 deletion src/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,6 @@ async def on_connect():
# everything below here will only run on the first connect

try:

logging.info("Connected to Discord. Initializing Database.")
await init_database(get_mongo_uri(), "FUBot")
except Exception as e:
Expand All @@ -67,6 +66,7 @@ async def on_connect():
bot.load_extension("commands.squad_markup")
bot.load_extension("commands.ps2_lookup")
bot.load_extension("commands.arma_upload_map")
bot.load_extension("commands.arma_get_db")
# bot.load_extension("loggers.discord_logger")
# bot.load_extension("loggers.ps2_outfit_members")
# bot.load_extension("loggers.ps2_outfit_online_logger")
Expand Down
3 changes: 2 additions & 1 deletion src/requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@ SteamQuery==1.0.2
beanie==1.25.0
pydantic==1.10.14
aiofiles==23.2.1
asyncssh==2.14.2
asyncssh==2.14.2
pandas==2.2.1

0 comments on commit f716d8b

Please sign in to comment.