Files
.gitignore
bot.py
f.txt
f(1).txt
f(2).txt
live-builders-counter-ip-shield-CyXxr9U1.png
README.md
requirements.txt
Code
8 737
bot.py
import logging
import os
import sqlite3
from datetime import datetime, timezone
from typing import Optional
import discord
from discord import app_commands
from discord.ext import commands
from dotenv import load_dotenv
load_dotenv()
logging.basicConfig(
level=logging.INFO,
format="%(asctime)s | %(levelname)s | %(name)s | %(message)s",
)
logger = logging.getLogger("fortnite-team-finder")
TOKEN = os.getenv("DISCORD_TOKEN")
DATABASE_PATH = os.getenv("DATABASE_PATH", "teamfinder.sqlite3")
TEST_GUILD_ID = os.getenv("TEST_GUILD_ID")
MAX_PR = 100_000
def now_iso() -> str:
return datetime.now(timezone.utc).isoformat()
def format_time(value: Optional[str]) -> str:
if not value:
return "Not available"
try:
moment = datetime.fromisoformat(value)
return discord.utils.format_dt(moment, style="R")
except ValueError:
return "Not available"
def pr_bar(pr: int, width: int = 12) -> str:
filled = round((pr / MAX_PR) * width)
return "โฐ" * filled + "โฑ" * (width - filled)
PR_TIERS = (
(0, "Unranked"),
(500, "Starter"),
(1_500, "Rookie"),
(3_000, "Prospect"),
(5_000, "Challenger"),
(8_300, "Semi-Pro"),
(10_000, "Rising"),
(25_000, "Contender"),
(50_000, "Elite"),
(75_000, "Champion"),
(100_000, "Legend"),
)
def pr_tier(pr: int) -> tuple[str, Optional[int]]:
tier = PR_TIERS[0][1]
next_threshold: Optional[int] = PR_TIERS[1][0]
for threshold, name in PR_TIERS:
if pr >= threshold:
tier = name
else:
next_threshold = threshold
break
else:
next_threshold = None
return tier, next_threshold
class Database:
def __init__(self, path: str):
self.connection = sqlite3.connect(path)
self.connection.row_factory = sqlite3.Row
self.connection.execute("PRAGMA journal_mode=WAL")
self.connection.executescript(
"""
CREATE TABLE IF NOT EXISTS players (
discord_id INTEGER PRIMARY KEY,
epic_name TEXT NOT NULL,
platform TEXT NOT NULL,
pr INTEGER NOT NULL DEFAULT 0,
pr_updated_at TEXT NOT NULL,
created_at TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS team_posts (
guild_id INTEGER NOT NULL,
discord_id INTEGER NOT NULL,
mode TEXT NOT NULL,
region TEXT NOT NULL,
mic INTEGER NOT NULL DEFAULT 1,
note TEXT NOT NULL DEFAULT '',
created_at TEXT NOT NULL,
PRIMARY KEY (guild_id, discord_id)
);
CREATE TABLE IF NOT EXISTS pr_history (
id INTEGER PRIMARY KEY AUTOINCREMENT,
discord_id INTEGER NOT NULL,
pr INTEGER NOT NULL,
recorded_at TEXT NOT NULL
);
"""
)
self.connection.commit()
def close(self) -> None:
self.connection.close()
def get_player(self, discord_id: int) -> Optional[sqlite3.Row]:
return self.connection.execute(
"SELECT * FROM players WHERE discord_id = ?", (discord_id,)
).fetchone()
def save_player(self, discord_id: int, epic_name: str, platform: str) -> None:
existing = self.get_player(discord_id)
if existing:
self.connection.execute(
"UPDATE players SET epic_name = ?, platform = ? WHERE discord_id = ?",
(epic_name, platform, discord_id),
)
else:
timestamp = now_iso()
self.connection.execute(
"""
INSERT INTO players
(discord_id, epic_name, platform, pr, pr_updated_at, created_at)
VALUES (?, ?, ?, 0, ?, ?)
""",
(discord_id, epic_name, platform, timestamp, timestamp),
)
self.connection.commit()
def set_pr(self, discord_id: int, pr: int) -> None:
timestamp = now_iso()
self.connection.execute(
"UPDATE players SET pr = ?, pr_updated_at = ? WHERE discord_id = ?",
(pr, timestamp, discord_id),
)
self.connection.execute(
"INSERT INTO pr_history (discord_id, pr, recorded_at) VALUES (?, ?, ?)",
(discord_id, pr, timestamp),
)
self.connection.commit()
def delete_player(self, discord_id: int) -> None:
self.connection.execute("DELETE FROM players WHERE discord_id = ?", (discord_id,))
self.connection.execute("DELETE FROM team_posts WHERE discord_id = ?", (discord_id,))
self.connection.execute("DELETE FROM pr_history WHERE discord_id = ?", (discord_id,))
self.connection.commit()
def pr_history(self, discord_id: int, limit: int = 8) -> list[sqlite3.Row]:
return self.connection.execute(
"""
SELECT pr, recorded_at
FROM pr_history
WHERE discord_id = ?
ORDER BY id DESC
LIMIT ?
""",
(discord_id, limit),
).fetchall()
def save_post(
self,
guild_id: int,
discord_id: int,
mode: str,
region: str,
mic: bool,
note: str,
) -> None:
self.connection.execute(
"""
INSERT INTO team_posts
(guild_id, discord_id, mode, region, mic, note, created_at)
VALUES (?, ?, ?, ?, ?, ?, ?)
ON CONFLICT(guild_id, discord_id) DO UPDATE SET
mode = excluded.mode,
region = excluded.region,
mic = excluded.mic,
note = excluded.note,
created_at = excluded.created_at
""",
(guild_id, discord_id, mode, region, int(mic), note, now_iso()),
)
self.connection.commit()
def delete_post(self, guild_id: int, discord_id: int) -> None:
self.connection.execute(
"DELETE FROM team_posts WHERE guild_id = ? AND discord_id = ?",
(guild_id, discord_id),
)
self.connection.commit()