Compare commits
15
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
f22f1d46d1 | ||
|
|
7bcdae7b79 | ||
|
|
d51bc4965e | ||
|
|
d7a6ca31d1 | ||
|
|
6cf112be49 | ||
|
|
f20a4abc81 | ||
|
|
175eeced0c | ||
|
|
2be9cc5795 | ||
|
|
07a2bf7e38 | ||
|
|
1ee949eae2 | ||
|
|
8bec9a159b | ||
|
|
ee77279d6e | ||
|
|
1b67240976 | ||
|
|
16a19e6913 | ||
|
|
4ae03c9b9d |
@@ -2,6 +2,7 @@
|
|||||||
cogs made for our Royal butt.
|
cogs made for our Royal butt.
|
||||||
|
|
||||||
## Our Cogs:
|
## Our Cogs:
|
||||||
|
- [FitnessCog](./fitnessCog)
|
||||||
- [RecruitmentCog](./recruitmentCog/)
|
- [RecruitmentCog](./recruitmentCog/)
|
||||||
- [ReginaldCog](./reginaldCog/)
|
- [ReginaldCog](./reginaldCog/)
|
||||||
- [TrafficCog](./trafficCog)
|
- [TrafficCog](./trafficCog)
|
||||||
|
|||||||
@@ -0,0 +1,6 @@
|
|||||||
|
from redbot.core.bot import Red
|
||||||
|
from .fitness import FitnessCog
|
||||||
|
|
||||||
|
async def setup(bot: Red):
|
||||||
|
cog = FitnessCog(bot)
|
||||||
|
await bot.add_cog(cog)
|
||||||
@@ -0,0 +1,48 @@
|
|||||||
|
{
|
||||||
|
"convertable": true,
|
||||||
|
"units": {
|
||||||
|
"meter": {
|
||||||
|
"factor": 1,
|
||||||
|
"aliases": [
|
||||||
|
"meter",
|
||||||
|
"meters",
|
||||||
|
"m",
|
||||||
|
"m."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"kilometer": {
|
||||||
|
"factor": 1000,
|
||||||
|
"aliases": [
|
||||||
|
"kilometer",
|
||||||
|
"kilometers",
|
||||||
|
"km",
|
||||||
|
"km."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"foot": {
|
||||||
|
"factor": 0.3048,
|
||||||
|
"aliases": [
|
||||||
|
"foot",
|
||||||
|
"feet",
|
||||||
|
"ft",
|
||||||
|
"ft."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"mile": {
|
||||||
|
"factor": 1609.344,
|
||||||
|
"aliases": [
|
||||||
|
"mile",
|
||||||
|
"miles",
|
||||||
|
"mi",
|
||||||
|
"mi."
|
||||||
|
]
|
||||||
|
},
|
||||||
|
"step": {
|
||||||
|
"factor": 0.18,
|
||||||
|
"aliases": [
|
||||||
|
"step",
|
||||||
|
"steps"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,3 @@
|
|||||||
|
{
|
||||||
|
"convertable": false
|
||||||
|
}
|
||||||
@@ -0,0 +1,105 @@
|
|||||||
|
import csv
|
||||||
|
import json
|
||||||
|
from pathlib import Path
|
||||||
|
from typing import Optional
|
||||||
|
|
||||||
|
|
||||||
|
def activity_log_path(activity: str, logs_dir: Optional[Path] = None) -> Path:
|
||||||
|
"""
|
||||||
|
Returns the path of the activity log file in .csv format.
|
||||||
|
"""
|
||||||
|
logs_dir = logs_dir or (Path.cwd() / "activity_logs")
|
||||||
|
return logs_dir / f"{activity}.csv"
|
||||||
|
|
||||||
|
|
||||||
|
def activity_units_path(activity: str, units_dir: Optional[Path] = None) -> Path:
|
||||||
|
"""
|
||||||
|
Returns the path of the activity units file in .json format.
|
||||||
|
"""
|
||||||
|
units_dir = units_dir or (Path.cwd() / "activities")
|
||||||
|
return units_dir / f"{activity}.json"
|
||||||
|
|
||||||
|
|
||||||
|
def is_convertable(activity: str, units_dir: Optional[Path] = None) -> bool:
|
||||||
|
filename = activity_units_path(activity, units_dir)
|
||||||
|
with filename.open("r", encoding="utf-8") as f:
|
||||||
|
raw_data = json.load(f)
|
||||||
|
return bool(raw_data.get("convertable"))
|
||||||
|
|
||||||
|
|
||||||
|
def create_activity_log_file(
|
||||||
|
activity: str,
|
||||||
|
*,
|
||||||
|
units_dir: Optional[Path] = None,
|
||||||
|
logs_dir: Optional[Path] = None,
|
||||||
|
) -> None:
|
||||||
|
if not activity_units_path(activity, units_dir).exists():
|
||||||
|
raise ValueError(f"{activity} is not a valid activity")
|
||||||
|
|
||||||
|
logs_dir = logs_dir or (Path.cwd() / "activity_logs")
|
||||||
|
logs_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
filename = activity_log_path(activity, logs_dir)
|
||||||
|
if filename.exists():
|
||||||
|
return # don't re-add headers
|
||||||
|
|
||||||
|
with filename.open("w", newline="", encoding="utf-8") as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow(["timestamp", "username", "value"])
|
||||||
|
|
||||||
|
|
||||||
|
def convert_units(
|
||||||
|
activity: str,
|
||||||
|
value: int | float,
|
||||||
|
unit: str | None,
|
||||||
|
*,
|
||||||
|
units_dir: Optional[Path] = None,
|
||||||
|
) -> float:
|
||||||
|
filename = activity_units_path(activity, units_dir)
|
||||||
|
if not filename.exists():
|
||||||
|
raise ValueError(f"{activity} is not a valid activity")
|
||||||
|
|
||||||
|
# Consider None unit value as factor of 1
|
||||||
|
if unit is None:
|
||||||
|
return float(value)
|
||||||
|
|
||||||
|
with filename.open("r", encoding="utf-8") as f:
|
||||||
|
raw_data = json.load(f)
|
||||||
|
|
||||||
|
units_data = raw_data.get("units") or {}
|
||||||
|
unit_l = unit.lower()
|
||||||
|
|
||||||
|
for _, data in units_data.items():
|
||||||
|
aliases = [a.lower() for a in (data.get("aliases") or [])]
|
||||||
|
if unit_l in aliases:
|
||||||
|
return float(value) * float(data["factor"])
|
||||||
|
|
||||||
|
raise ValueError(f"{unit} is not a valid unit")
|
||||||
|
|
||||||
|
|
||||||
|
def log_activity(
|
||||||
|
timestamp: int,
|
||||||
|
username: str,
|
||||||
|
activity: str,
|
||||||
|
value: int | float,
|
||||||
|
unit: str | None,
|
||||||
|
*,
|
||||||
|
units_dir: Optional[Path] = None,
|
||||||
|
logs_dir: Optional[Path] = None,
|
||||||
|
) -> None:
|
||||||
|
|
||||||
|
if not isinstance(value, (int, float)) or value <= 0:
|
||||||
|
raise ValueError(f"{value} is not a valid value")
|
||||||
|
|
||||||
|
filename = activity_log_path(activity, logs_dir)
|
||||||
|
if not filename.exists():
|
||||||
|
create_activity_log_file(activity, units_dir=units_dir, logs_dir=logs_dir)
|
||||||
|
|
||||||
|
if is_convertable(activity, units_dir):
|
||||||
|
converted_value = round(convert_units(activity, value, unit, units_dir=units_dir), 2)
|
||||||
|
else:
|
||||||
|
converted_value = round(float(value), 2)
|
||||||
|
|
||||||
|
with filename.open("a", newline="", encoding="utf-8") as f:
|
||||||
|
writer = csv.writer(f)
|
||||||
|
writer.writerow([timestamp, username, converted_value])
|
||||||
@@ -0,0 +1,143 @@
|
|||||||
|
import os
|
||||||
|
from pathlib import Path
|
||||||
|
|
||||||
|
import discord
|
||||||
|
from redbot.core import commands
|
||||||
|
from redbot.core.data_manager import cog_data_path
|
||||||
|
|
||||||
|
from .activity_logger import log_activity
|
||||||
|
|
||||||
|
|
||||||
|
class FitnessCog(commands.Cog):
|
||||||
|
def __init__(self, bot):
|
||||||
|
self.bot = bot
|
||||||
|
|
||||||
|
default_threads = "1457402451530350727,1328363409648910356"
|
||||||
|
threads_raw = os.getenv("THREADS_ID", default_threads)
|
||||||
|
|
||||||
|
self.threads_id: list[int] = []
|
||||||
|
for x in threads_raw.split(","):
|
||||||
|
x = x.strip()
|
||||||
|
if x.isdigit():
|
||||||
|
self.threads_id.append(int(x))
|
||||||
|
|
||||||
|
self.confirm_reactions = os.getenv("CONFIRMATION_REACTIONS", "🐈💨")
|
||||||
|
|
||||||
|
# Units are shipped with the cog in: fitnessCog/activities/*.json
|
||||||
|
self.units_dir: Path = Path(__file__).parent / "activities"
|
||||||
|
|
||||||
|
# Logs are writable and belong in Red's data dir
|
||||||
|
self.logs_dir: Path = cog_data_path(self) / "activity_logs"
|
||||||
|
self.logs_dir.mkdir(parents=True, exist_ok=True)
|
||||||
|
|
||||||
|
@commands.Cog.listener()
|
||||||
|
async def on_message(self, message: discord.Message):
|
||||||
|
if message.author.bot:
|
||||||
|
return
|
||||||
|
if message.channel.id not in self.threads_id:
|
||||||
|
return
|
||||||
|
|
||||||
|
content = (message.content or "").strip()
|
||||||
|
if not content.startswith("!"):
|
||||||
|
return
|
||||||
|
|
||||||
|
cmdline = content[1:].strip().lower()
|
||||||
|
if not cmdline:
|
||||||
|
return
|
||||||
|
|
||||||
|
cmd, *rest = cmdline.split(" ", 1)
|
||||||
|
arg = rest[0].strip() if rest else ""
|
||||||
|
|
||||||
|
if cmd == "getunits":
|
||||||
|
await self.get_units_file(message, arg)
|
||||||
|
return
|
||||||
|
|
||||||
|
if cmd == "getlog":
|
||||||
|
await self.get_log_file(message, arg)
|
||||||
|
return
|
||||||
|
|
||||||
|
await self.log_fitness(message)
|
||||||
|
|
||||||
|
async def get_units_file(self, message: discord.Message, activity: str):
|
||||||
|
try:
|
||||||
|
if not activity:
|
||||||
|
await message.channel.send(
|
||||||
|
"Usage: `!getunits <activity>` (example: `!getunits pushups`)"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
activity = activity.lower().strip()
|
||||||
|
path = self.units_dir / f"{activity}.json"
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
await message.channel.send(f"No units JSON found for `{activity}`.")
|
||||||
|
return
|
||||||
|
|
||||||
|
await message.channel.send(file=discord.File(fp=str(path)))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
await message.channel.send(str(e))
|
||||||
|
|
||||||
|
async def get_log_file(self, message: discord.Message, activity: str):
|
||||||
|
try:
|
||||||
|
if not activity:
|
||||||
|
await message.channel.send(
|
||||||
|
"Usage: `!getlog <activity>` (example: `!getlog pushups`)"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
activity = activity.lower().strip()
|
||||||
|
path = self.logs_dir / f"{activity}.csv"
|
||||||
|
|
||||||
|
if not path.exists():
|
||||||
|
await message.channel.send(f"No log CSV found for `{activity}`.")
|
||||||
|
return
|
||||||
|
|
||||||
|
await message.channel.send(file=discord.File(fp=str(path)))
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
await message.channel.send(str(e))
|
||||||
|
|
||||||
|
async def log_fitness(self, message: discord.Message):
|
||||||
|
try:
|
||||||
|
parts = message.content[1:].strip().split()
|
||||||
|
if len(parts) < 2:
|
||||||
|
await message.reply(
|
||||||
|
"Usage: `!<activity> <value> [unit]` (example: `!pushups 20` or `!run 5 km`)"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
activity = parts[0]
|
||||||
|
|
||||||
|
try:
|
||||||
|
value = float(parts[1])
|
||||||
|
except ValueError:
|
||||||
|
await message.reply(
|
||||||
|
"Invalid number. Usage: `!<activity> <value> [unit]` (example: `!run 5 km`)"
|
||||||
|
)
|
||||||
|
return
|
||||||
|
|
||||||
|
unit = parts[2] if len(parts) > 2 else "m"
|
||||||
|
|
||||||
|
timestamp = int(message.created_at.timestamp())
|
||||||
|
username = message.author.name
|
||||||
|
|
||||||
|
log_activity(
|
||||||
|
timestamp,
|
||||||
|
username,
|
||||||
|
activity,
|
||||||
|
value,
|
||||||
|
unit,
|
||||||
|
units_dir=self.units_dir,
|
||||||
|
logs_dir=self.logs_dir,
|
||||||
|
)
|
||||||
|
|
||||||
|
for r in self.confirm_reactions:
|
||||||
|
await message.add_reaction(r)
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
await message.reply(str(e))
|
||||||
|
|
||||||
|
|
||||||
|
def setup(bot):
|
||||||
|
bot.add_cog(FitnessCog(bot))
|
||||||
Reference in New Issue
Block a user