Compare commits

..
17 Commits
Author SHA1 Message Date
Hermes Agent 7cde9361bb feat(hermes): surface de test locale du pont (POST /hermes/ask)
Docker / Deploy (push) Canceled after 0s
Docker / build (push) Canceled after 0s
Docker / build (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.9) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.9) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.9) (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (macos-latest, 3.9) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (ubuntu-latest, 3.9) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.10) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.8) (push) Canceled after 0s
Docker / test-python-versions (windows-latest, 3.9) (push) Canceled after 0s
Le pont n'etait verifiable qu'en parlant au bot depuis Discord, et l'API du bot
(/command/channel) ne peut pas renvoyer la reponse : py-cord n'expose pas la
valeur de retour d'une commande. Le cog ouvre donc son propre listener loopback
(127.0.0.1, defaut 8052, HERMES_BRIDGE_ASK_PORT) :

    GET  /live        healthcheck
    POST /hermes/ask  {"message": "...", "user_id": optionnel} -> {"answer": "..."}

Meme convention d'auth que cogs/api.py (Bearer DISCORD_BOT_API_TOKEN seulement
si la variable est posee). Historique par user_id comme en DM.
2026-09-27 16:58:46 -04:00
Hermes Agent 641bf79967 fix(hermes): pointer le pont sur le listener API server unique
L'API server Hermes est un listener unique par hote (profil default) : un profil
secondaire est expose sous /p/<profil>/. L'URL par defaut devient
http://127.0.0.1:8642/p/discord-bridge/v1/chat/completions (l'env systemd
HERMES_BRIDGE_URL garde la priorite).
2026-09-27 16:41:29 -04:00
Hermes Agent b793be5aa1 feat(hermes): bridge to a dedicated Hermes Agent gateway
Cog /hermes qui relaie les messages vers l'API server OpenAI-compatible d'un
gateway Hermes dedie (profil discord-bridge, loopback 127.0.0.1:8643 cote NixOS).
Reserve aux masters du bot et aux porteurs du role Discord hermes
(HERMES_BRIDGE_ROLE_NAME), en DM comme via !hermes <message>.
Historique par utilisateur (!hermes-reset), decoupage a 2000 caracteres,
!hermes-setup cree le role et l'attribue aux masters.

URL et cle viennent de l'environnement (HERMES_BRIDGE_URL, HERMES_BRIDGE_API_KEY),
injectees par systemd.
2026-09-27 16:24:27 -04:00
mazex f399cc3fed refactor(nixos): configure bot service with dedicated user
Replaces DynamicUser with a specific system user and group for the
discord bot service, enhancing security and predictability.

Updates the virtual environment path to use a runtime directory
and sets an explicit RuntimeDirectory with a size limit for the
systemd service.
2026-09-13 12:04:48 -04:00
mazex 5770b64a6b chore(flake): refine uv cache paths and add TERM env var
Update `UV_PROJECT_ENVIRONMENT` to use `CACHE_DIRECTORY`.
Adjust `UV_CACHE_DIR` to a more specific path.
Add `TERM=dumb` to the service environment for non-interactive
compatibility.
2026-09-13 11:57:07 -04:00
mazex d6087a2624 build(nix): update flake for bot runtime and service
add `ncurses` to runtime dependencies.
set working directory to bot's source directory for proper execution.
refactor systemd service configuration for environment variables and cache.
2026-09-13 11:52:08 -04:00
mazex 0bcc4ce56b fix(flake): set pythonpath for discord bot module
Ensure the discord_bot module can resolve internal imports by
dynamically setting PYTHONPATH to its parent directory.
2026-09-13 11:49:31 -04:00
mazex 03986dc63e chore(systemd): set cache and home for bot service
Configure systemd service to use a dedicated cache directory and
set `UV_CACHE_DIR` and `HOME` environment variables for the bot.
2026-09-13 11:41:17 -04:00
mazex 127670486f feat(nix): add NixOS module and upgrade docker python
Introduces a NixOS module for declarative deployment.
Upgrades Docker Python from 3.8 to 3.13, regenerating dependencies.
The Nix development environment remains on Python 3.12.

BREAKING CHANGE: Docker Python version upgraded from 3.8 to 3.13.
2026-09-13 11:35:58 -04:00
mazex e6eec52220 refactor: standardize cog setup and enhance api
Changed all cog `setup` functions to be synchronous, removing `await bot.add_cog()`.
Refactored the API cog for improved robustness, error handling, and security.
Added API token authentication for all endpoints except `/live`.
Implemented centralized JSON error handling for API responses.
Ensured idempotent server startup on bot `on_ready` events.
2026-09-13 01:53:02 -04:00
mazexandClaude Opus 4.5 0697fe1a29 Fix API endpoints error handling
- Initialize _server_started flag in API.__init__
- Fix /debug/bot to return proper JSON (not raw bot object)
- Add comprehensive error handling to /send/channel and /send/user
- Properly handle missing fields, permissions, and not found errors

All API endpoints now work correctly with proper HTTP status codes.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-09-13 01:28:29 -04:00
mazexandClaude Opus 4.5 9a60167b4b Fix HTTP API server startup and add tests
- Convert API cog to use aiohttp directly (removed dpy-http-server dependency)
- Fix cog loading: setup() must be non-async in py-cord 2.5.0
- Start HTTP server in Main.on_ready() after all cogs are loaded
- Remove server import from storage cog
- Add comprehensive API test suite (test_api.py)
- Add Justfile for convenient bot management

HTTP server now starts correctly on port 8050 and responds to requests.
Some endpoints return 500 errors which will be debugged separately.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-09-13 01:25:00 -04:00
mazexandClaude Opus 4.5 6438014bfa Add Nix flake for development environment
- Add flake.nix with Python 3.12, uv, just, ffmpeg, selenium
- Remove dpy-http-server dependency (conflicts with py-cord)
- Configure LD_LIBRARY_PATH for binary wheels
- Auto-setup venv on nix develop

This provides a reproducible development environment with all dependencies.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-09-13 00:49:58 -04:00
mazexandClaude Opus 4.5 99dc8759af Fix startup issues and make optional environment variables
- Add async-timeout dependency for music cog
- Fix randint TypeError in quiz model (floats to ints)
- Make WOLFRAM_ALPHA_ID optional in credentials
- Make CHANNEL_ID_NOTIF optional in api and storage cogs
- Make DOCKER_API_URL optional in docker cog

This allows the bot to start without requiring all optional service credentials.

Co-Authored-By: Claude Opus 4.5 <noreply@anthropic.com>
2026-09-13 00:38:20 -04:00
marc 9f8030c94d library cog to store yml conf (#28) 2024-12-28 01:15:32 +01:00
marc 971c87b3b6 Make different functions to divide http requests from actual search within discord attachments then generate HTTP GET url to retrieve file (#27)
* show channel id

* storage urls that work
2024-12-28 01:13:20 +01:00
marc 28dfb29bf2 update discord_bot/cogs/backup.py trigger str meanwhile 2024-09-16 19:56:15 +02:00
61 changed files with 1677 additions and 1833 deletions
+2 -2
View File
@@ -1,10 +1,10 @@
FROM python:3.8.13-alpine AS alpine
FROM python:3.13-alpine AS alpine
LABEL maintainer="marc.partensky@proton.me"
LABEL image="https://hub.docker.com/r/marcpartensky/discord-bot"
LABEL source="https://github.com/marcpartensky/discord-bot"
WORKDIR /app
COPY README.md LICENSE pyproject.toml uv.lock build.sh ./
COPY README.md LICENSE pyproject.toml uv.lock build.sh requirements.txt ./
COPY ./discord_bot discord_bot
RUN ./build.sh
WORKDIR /app/discord_bot
+28
View File
@@ -0,0 +1,28 @@
set dotenv-load := true
image := "marcpartensky/discord-bot"
# lance le bot en local
run:
uv run --directory discord_bot python .
# installe les deps
sync:
uv sync
# build l'image
build:
podman build -t {{image}}:local .
# lance l'image
docker-run:
podman run --rm -it --env-file .env {{image}}:local
# logs du service
logs:
journalctl -fu podman-discord-bot
# relance le service
restart:
sudo systemctl reset-failed podman-discord-bot
sudo systemctl restart podman-discord-bot
+2 -2
View File
@@ -9,8 +9,8 @@ wait
export PATH="$PATH:$HOME/.cargo/bin"
# uv sync -v &
uv pip compile pyproject.toml -o requirements.txt
# uv export > requirements.txt
# uv pip compile pyproject.toml -o requirements.txt
# uv export --format requirements.txt > requirements.txt
# sed -i '1,2d' requirements.txt
pip install -r requirements.txt &
apk add ffmpeg postgresql p7zip &
+28 -6
View File
@@ -51,11 +51,15 @@ class Main(commands.Bot):
self.good_invited = "bon invité"
self.bad_invited = "mauvais invité"
self.help_every = 5
self._status = itertools.cycle([]) # Will be populated in load_status()
async def setup_hook(self) -> None:
"""Automatically run by discord.py."""
self.load_status()
print("DEBUG: setup_hook called")
await self.load_cogs()
print("DEBUG: load_cogs finished")
self.load_status()
print("DEBUG: load_status finished")
def load_status(self):
"""Charge les statuts."""
@@ -79,7 +83,7 @@ class Main(commands.Bot):
bar.update(i)
if filename.endswith(".py"):
print(filename)
await self.load_extension(f"cogs.{filename[:-3]}")
self.load_extension(f"cogs.{filename[:-3]}")
@commands.command()
async def environment(self, ctx: commands.Context):
@@ -159,11 +163,16 @@ class Main(commands.Bot):
- discord.ActivityType.watching,
- discord.ActivityType.competing
"""
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching, name=next(self._status)
try:
status_name = next(self._status)
await self.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching, name=status_name
)
)
)
except StopIteration:
# Pas de statuts disponibles, on ne fait rien
pass
@commands.Cog.listener()
async def on_ready(self):
@@ -171,6 +180,18 @@ class Main(commands.Bot):
self.change_status.start()
print(f"{self.user} is connected to Discord!")
# Démarrer le serveur HTTP API
print(f"DEBUG: on_ready - Looking for API cog...")
api_cog = self.get_cog("API")
print(f"DEBUG: API cog = {api_cog}")
if api_cog and hasattr(api_cog, '_start_server'):
print(f"DEBUG: Starting HTTP server from on_ready...")
if not hasattr(api_cog, '_server_started') or not api_cog._server_started:
await api_cog._start_server()
api_cog._server_started = True
else:
print("DEBUG: API cog not found or no _start_server method")
@commands.Cog.listener()
async def on_member_join(self, member: discord.Member):
"""Venue un nouveau membre."""
@@ -288,6 +309,7 @@ async def main():
bot = Main(intents=intents, command_prefix=prefix, case_insensitive=False, client_id=client_id)
async with bot:
await bot.setup_hook()
await bot.start(token)
+2 -2
View File
@@ -363,5 +363,5 @@ class Admin(commands.Cog):
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(Admin(bot))
def setup(bot):
bot.add_cog(Admin(bot))
+2 -2
View File
@@ -70,5 +70,5 @@ class Advertisement(commands.Cog):
await ctx.send(f"Le prix de {name} est maintenant à {price}.")
async def setup(bot):
await bot.add_cog(Advertisement(bot))
def setup(bot):
bot.add_cog(Advertisement(bot))
+108 -104
View File
@@ -2,141 +2,145 @@
"""HTTP web service to interact with the bot."""
__author__ = "Marc Partensky"
# from config.config import cluster, access, check
# from utils.check import check
# from utils import tools
import os, time
import asyncio
import functools
import os
import traceback
import typing
import uuid
import traceback
import discord
import aiofiles
from aiohttp import web
from discord.ext import commands
import server
from aiohttp import web
MAX_CHUNK_SIZE = 8 * 1024 * 1024 # 8 MB
def json_errors(handler):
"""Mappe les exceptions Discord vers des réponses JSON."""
@functools.wraps(handler)
async def wrapper(self, request: web.Request):
try:
return await handler(self, request)
except KeyError as e:
return web.json_response({"error": f"Missing required field: {e}"}, status=400)
except discord.NotFound:
return web.json_response({"error": "Not found"}, status=404)
except discord.Forbidden:
return web.json_response({"error": "Forbidden"}, status=403)
except Exception as e:
traceback.print_exc()
return web.json_response({"error": str(e)}, status=500)
return wrapper
class API(commands.Cog):
"""HTTP web service to interact with the bot."""
def __init__(self, bot: commands.Bot):
self.bot = bot
self.server = server.HTTPServer(
bot=self.bot,
host=os.environ.get("DISCORD_BOT_HOST") or "localhost",
port=int(os.environ.get("DISCORD_BOT_PORT") or "8000"),
)
self.guild_id = int(os.environ.get(
"DISCORD_BOT_GUILD_ID"
) or "550332212340326428")
self.host = os.environ.get("DISCORD_BOT_HOST", "0.0.0.0")
self.port = int(os.environ.get("DISCORD_BOT_PORT", "8050"))
self.token = os.environ.get("DISCORD_BOT_API_TOKEN")
self.contexts: typing.Dict[str, commands.Context] = {}
self.channel_id = int(os.environ["CHANNEL_ID_NOTIF"])
self.bot.loop.create_task(self._start_server())
self.runner: typing.Optional[web.AppRunner] = None
self.site: typing.Optional[web.TCPSite] = None
self._lock = asyncio.Lock()
async def _start_server(self):
"""Task to run the HTTP server."""
await self.bot.wait_until_ready()
await self.server.start()
self.app = web.Application(middlewares=[self._auth])
self.app.router.add_post("/send/user", self.send_user)
self.app.router.add_post("/send/channel", self.send_channel)
self.app.router.add_post("/command/channel", self.command_channel)
self.app.router.add_get("/debug/bot", self.debug_bot)
self.app.router.add_get("/live", self.live)
@server.add_route(path="/send/user", method="POST", cog="API")
@web.middleware
async def _auth(self, request: web.Request, handler):
if self.token and request.path != "/live":
if request.headers.get("Authorization") != f"Bearer {self.token}":
return web.json_response({"error": "Unauthorized"}, status=401)
return await handler(request)
@commands.Cog.listener()
async def on_ready(self):
"""on_ready peut se redéclencher à chaque reconnexion: idempotent."""
async with self._lock:
if self.site is not None:
return
self.runner = web.AppRunner(self.app)
await self.runner.setup()
self.site = web.TCPSite(self.runner, self.host, self.port)
try:
await self.site.start()
except OSError:
self.site = None
await self.runner.cleanup()
self.runner = None
raise
print(f"HTTP server listening on {self.host}:{self.port}")
def cog_unload(self):
if self.runner:
asyncio.create_task(self.runner.cleanup())
async def _send(self, request: web.Request, fetch):
body = await request.json()
target = await fetch(int(body["id"]))
await target.send(body["message"])
return web.json_response({"text": "Successfully sent message."})
@json_errors
async def send_user(self, request: web.Request):
"""API home path."""
body = await request.json()
user: discord.User = await self.bot.fetch_user(body["id"])
await user.send(body["message"])
return web.json_response(
data={"text": "Successfully sent message."}, status=200
)
"""Send a DM to a Discord user."""
return await self._send(request, self.bot.fetch_user)
@server.add_route(path="/send/channel", method="POST", cog="API")
@json_errors
async def send_channel(self, request: web.Request):
"""API home path."""
body = await request.json()
channel: discord.TextChannel = await self.bot.fetch_channel(body["id"])
await channel.send(body["message"])
self.bot.commands
return web.json_response(
data={"text": "Successfully sent message."}, status=200
)
"""Send a message to a Discord channel."""
return await self._send(request, self.bot.fetch_channel)
@server.add_route(path="/command/channel", method="POST", cog="API")
@json_errors
async def command_channel(self, request: web.Request):
"""API home path."""
"""Invoke a bot command on a saved context."""
body = await request.json()
ctx_id = body["ctx"]
cmd = body["cmd"]
if "args" in body:
args = body["args"]
else:
args = []
if "kwargs" in body:
kwargs = body["kwargs"]
else:
kwargs = {}
channel: discord.TextChannel = await self.bot.fetch_channel(body["id"])
command_list = []
for command in self.bot.commands:
command_list.append(dict(name=command.name, doc=command.short_doc))
print(command.name)
if command.name == cmd:
# context = self.build_context(channel, args)
ctx = self.contexts[ctx_id]
try:
print(ctx.args)
print(ctx.kwargs)
ctx.args = args
ctx.kwargs = kwargs
# command.clean_params
# for param in command.params:
command = self.bot.get_command(body["cmd"])
if command is None:
return web.json_response({
"error": "No match found.",
"commands": [
{"name": c.name, "doc": c.short_doc} for c in self.bot.commands
],
}, status=404)
# print(command._parse_arguments(ctx))
# await command.prepare(ctx)
# await converter._construct_default(ctx)
# await command.callback(context, __p1=command.clean_params)
# print(command.clean_params)
# print(command.params)
# print(command.invoke())
# ctx.args = args
result = await command.invoke(ctx)
return web.json_response(data={"text": str(result)}, status=200)
except Exception as exception:
traceback.print_exc()
return web.json_response(
data={"error": str(exception), "doc": command.short_doc},
status=500,
)
return web.json_response(
data={"error": "No match found.", "commands": command_list}, status=404
)
ctx = self.contexts.get(body["ctx"])
if ctx is None:
return web.json_response(
{"error": f"Unknown context: {body['ctx']}"}, status=404
)
ctx.args = body.get("args", [])
ctx.kwargs = body.get("kwargs", {})
result = await command.invoke(ctx)
return web.json_response({"text": str(result)})
@server.add_route(path="/debug/bot", method="GET", cog="API")
async def debug_bot(self, request: web.Request):
"""API home path."""
body = await request.json()
return web.json_response(data=self.bot, status=200)
"""Return bot debug information."""
return web.json_response({
"id": str(self.bot.user.id) if self.bot.user else None,
"name": str(self.bot.user) if self.bot.user else None,
"guilds": len(self.bot.guilds),
"cogs": list(self.bot.cogs.keys()),
})
@server.add_route(path="/live", method="GET", cog="API")
async def live(self, request: web.Request):
"""API home path."""
return web.json_response(text="OK", status=200)
"""Healthcheck."""
return web.Response(text="OK")
@commands.command(name="save-context")
async def save_context(self, ctx: commands.Context, context_id: str or None):
async def save_context(self, ctx: commands.Context, context_id: typing.Optional[str] = None):
"""Save a context since it is really hard to build one from scratch."""
context_id = context_id or str(uuid.uuid1())
self.contexts[context_id] = ctx
await ctx.send(f"> Saved context as **{context_id}**")
@commands.Cog.listener()
async def on_message(self, msg: discord.Message):
"""Nettoie les messages des salons innapropriés, dès que
ceux-ci sont postés."""
if msg.author.bot:
return
async def setup(bot: commands.Bot):
await bot.add_cog(API(bot))
def setup(bot: commands.Bot):
bot.add_cog(API(bot))
+3 -3
View File
@@ -256,7 +256,7 @@ class Backup(commands.Cog):
def get_cron_expression(self, trigger: CronTrigger) -> str:
"""Retourne une représentation de l'expression Cron sous forme de chaîne de caractères."""
return f"{trigger.minute} {trigger.hour} {trigger.day} {trigger.month} {trigger.day_of_week}"
return str(trigger)
async def build_job_embed(self) -> discord.Embed:
"""Affiche les jobs configurés."""
@@ -351,5 +351,5 @@ class Backup(commands.Cog):
except Exception as e:
await ctx.send(f"Erreur: {str(e)}")
async def setup(bot):
await bot.add_cog(Backup(bot))
def setup(bot):
bot.add_cog(Backup(bot))
+2 -2
View File
@@ -541,5 +541,5 @@ class Bank(commands.Cog):
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(Bank(bot))
def setup(bot):
bot.add_cog(Bank(bot))
+7 -2
View File
@@ -371,5 +371,10 @@ class Basic(commands.Cog):
file = requests.get(url)
await self.bot.user.edit(avatar=file.content)
async def setup(bot):
await bot.add_cog(Basic(bot))
@commands.command(name="channel")
async def channel(self, ctx: commands.Context):
"""Affiche l'id du channel."""
await ctx.send("> "+str(ctx.channel.id))
def setup(bot):
bot.add_cog(Basic(bot))
+2 -2
View File
@@ -329,5 +329,5 @@ class BlackJack(commands.Cog):
await self.stop(ctx)
async def setup(bot):
await bot.add_cog(BlackJack(bot))
def setup(bot):
bot.add_cog(BlackJack(bot))
+2 -2
View File
@@ -186,5 +186,5 @@ class Casino(commands.Cog):
return account
async def setup(bot):
await bot.add_cog(Casino(bot))
def setup(bot):
bot.add_cog(Casino(bot))
+2 -2
View File
@@ -226,5 +226,5 @@ class Code(commands.Cog):
await ctx.send(text)
async def setup(bot):
await bot.add_cog(Code(bot))
def setup(bot):
bot.add_cog(Code(bot))
+2 -2
View File
@@ -220,8 +220,8 @@ class Commandes(commands.Cog):
await self.search(ctx, *map(lambda r: r.strip(), req.strip().split("|")), regex=False, show_cmd=False)
async def setup(bot: commands.Bot):
await bot.add_cog(Commandes(bot))
def setup(bot: commands.Bot):
bot.add_cog(Commandes(bot))
# TODO: Test the cog
+2 -2
View File
@@ -155,5 +155,5 @@ class Puissance4(commands.Cog):
await self.message.edit(content="Personne ne gagne." + msg)
async def setup(bot):
await bot.add_cog(Puissance4(bot))
def setup(bot):
bot.add_cog(Puissance4(bot))
+2 -2
View File
@@ -175,6 +175,6 @@ class CountDown(commands.Cog):
await self.bot.wait_until_ready()
async def setup(bot):
def setup(bot):
"""Setup the CountDown cog."""
await bot.add_cog(CountDown(bot))
bot.add_cog(CountDown(bot))
+2 -2
View File
@@ -58,5 +58,5 @@ class Debug(commands.Cog):
await ctx.send(embed=embed)
async def setup(bot):
await bot.add_cog(Debug(bot))
def setup(bot):
bot.add_cog(Debug(bot))
+3 -3
View File
@@ -23,7 +23,7 @@ class Docker(commands.Cog):
"""Initialize the ialab cog."""
super().__init__(**kwargs)
self.bot: commands.Bot = bot
self.url: str = os.environ["DOCKER_API_URL"]
self.url: str = os.environ.get("DOCKER_API_URL", "")
self.color = discord.Color(0x4EB3F2)
self.states = dict(
exited=discord.Color.red(),
@@ -324,6 +324,6 @@ class Docker(commands.Cog):
return await ctx.send(f"```md\n{text}\n```")
async def setup(bot: commands.Bot):
def setup(bot: commands.Bot):
"""Setup the ialab cog."""
await bot.add_cog(Docker(bot))
bot.add_cog(Docker(bot))
+2 -2
View File
@@ -40,8 +40,8 @@ class Farm(commands.Cog):
del self.farms[name]
async def setup(bot):
await bot.add_cog(Farm(bot))
def setup(bot):
bot.add_cog(Farm(bot))
"""
+2 -2
View File
@@ -145,5 +145,5 @@ class Game(commands.Cog):
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(Game(bot))
def setup(bot):
bot.add_cog(Game(bot))
+2 -2
View File
@@ -102,5 +102,5 @@ class Git(commands.Cog):
await ctx.send("\n".join([cmd] + out))
async def setup(bot):
await bot.add_cog(Git(bot))
def setup(bot):
bot.add_cog(Git(bot))
+2 -2
View File
@@ -109,6 +109,6 @@ class Groq(commands.Cog):
return answer
async def setup(bot):
def setup(bot):
"""Setup the Shell cog."""
await bot.add_cog(Groq(bot))
bot.add_cog(Groq(bot))
+250
View File
@@ -0,0 +1,250 @@
#!/usr/bin/env python
"""Pont vers un gateway Hermes Agent dedie (profil `discord-bridge`).
Reserve aux membres qui portent le role Discord `hermes` (nom configurable via
`HERMES_BRIDGE_ROLE_NAME`) et aux masters du bot. Parle a l'API server
OpenAI-compatible d'Hermes (services/hermes cote NixOS, loopback uniquement).
L'API server est un listener UNIQUE par hote : un profil secondaire n'a pas son
propre port, il est servi sur le listener du profil default sous /p/<profil>/...
Variables d'environnement (injectees par systemd, cf. services/discord-bot) :
HERMES_BRIDGE_URL defaut http://127.0.0.1:8642/p/discord-bridge/v1/chat/completions
HERMES_BRIDGE_API_KEY bearer token (sops secrets/discord-hermes-bridge.yml)
HERMES_BRIDGE_ROLE_NAME nom du role Discord autorise (defaut "hermes")
HERMES_BRIDGE_ASK_PORT port loopback de la surface de test (defaut 8052)
DISCORD_BOT_API_TOKEN si pose, exige en Bearer (convention de cogs/api.py)
"""
import asyncio
import os
import typing
import discord
import requests
from aiohttp import web
from discord.ext import commands
from config.config import prefix, masters
from utils.tools import create_role_if_missing
DEFAULT_BRIDGE_URL = "http://127.0.0.1:8642/p/discord-bridge/v1/chat/completions"
BRIDGE_URL = os.environ.get("HERMES_BRIDGE_URL", DEFAULT_BRIDGE_URL)
BRIDGE_KEY = os.environ.get("HERMES_BRIDGE_API_KEY")
ROLE_NAME = os.environ.get("HERMES_BRIDGE_ROLE_NAME", "hermes")
MAX_HISTORY = 20 # messages (user + assistant) gardes par utilisateur
REQUEST_TIMEOUT = 120
SYSTEM_PROMPT = (
"Tu es Hermes, relaye ici depuis Discord (pont dedie, profil discord-bridge). "
"Reponds de facon concise, adaptee au format Discord (2000 caracteres max par "
"message), sans mise en forme lourde."
)
class Hermes(commands.Cog):
"""Bridge to a dedicated Hermes Agent gateway (OpenAI-compatible API)."""
def __init__(self, bot: commands.Bot):
"""Initialize the cog with the bot."""
self.bot = bot
self.color: discord.Color = discord.Color.blurple()
self.histories: typing.Dict[int, list] = {}
# Surface de test locale (loopback) : joindre Hermes par HTTP sans client
# Discord, et LIRE sa reponse en JSON. Sert au test de bout en bout du
# pont depuis le serveur (systemd injecte le port).
self.ask_host = os.environ.get("HERMES_BRIDGE_ASK_HOST", "127.0.0.1")
self.ask_port = int(os.environ.get("HERMES_BRIDGE_ASK_PORT", "8052"))
self.api_token = os.environ.get("DISCORD_BOT_API_TOKEN")
self.ask_runner: typing.Optional[web.AppRunner] = None
self.ask_site: typing.Optional[web.TCPSite] = None
self.ask_lock = asyncio.Lock()
# --- autorisation ------------------------------------------------------
@staticmethod
def _has_role(user: discord.abc.User) -> bool:
"""Vrai si l'utilisateur porte le role Hermes (compare sans la casse).
En DM, py-cord rend un `Member` (avec ses roles) quand il a le serveur en
cache, sinon un simple `User` sans attribut `roles` : d'ou le getattr.
"""
return any(
role.name.lower() == ROLE_NAME.lower()
for role in getattr(user, "roles", None) or []
)
def _author_allowed(self, author: discord.abc.User) -> bool:
"""Autorise les masters du bot et les porteurs du role, en DM comme en serveur."""
if author.id in masters:
return True
if self._has_role(author):
return True
for guild in self.bot.guilds:
member = guild.get_member(author.id)
if member and self._has_role(member):
return True
return False
# --- appel Hermes ------------------------------------------------------
async def _ask(self, user_id: int, message: str) -> str:
"""Envoie un message a Hermes et renvoie sa reponse."""
if not BRIDGE_KEY:
return "> Pont Hermes non configure (HERMES_BRIDGE_API_KEY manquant)."
history = self.histories.setdefault(user_id, [])
history.append({"role": "user", "content": message})
payload = {
"model": "discord-bridge",
"messages": [{"role": "system", "content": SYSTEM_PROMPT}]
+ history[-MAX_HISTORY:],
"stream": False,
}
headers = {
"Authorization": f"Bearer {BRIDGE_KEY}",
"Content-Type": "application/json",
}
def _do_request():
"""Requete bloquante, executee dans un thread pour ne pas figer la boucle."""
response = requests.post(
BRIDGE_URL, json=payload, headers=headers, timeout=REQUEST_TIMEOUT
)
response.raise_for_status()
return response.json()
data = await asyncio.to_thread(_do_request)
answer = data["choices"][0]["message"]["content"] or "> (reponse vide)"
history.append({"role": "assistant", "content": answer})
return answer
# --- ecoute Discord ----------------------------------------------------
@commands.Cog.listener(name="on_message")
async def on_message(self, msg: discord.Message):
"""En DM : chaque message est transmis directement a Hermes."""
if msg.author == self.bot.user or msg.author.bot:
return
if not isinstance(msg.channel, discord.DMChannel):
return
if msg.content.startswith(prefix):
return
if not self._author_allowed(msg.author):
return
async with msg.channel.typing():
try:
answer = await self._ask(msg.author.id, msg.content)
except Exception as exc: # noqa: BLE001
answer = f"> Erreur pont Hermes : {exc}"
await self.send_long(msg.channel, answer)
# --- commandes ---------------------------------------------------------
@commands.command(name="hermes")
async def hermes(self, ctx: commands.Context, *, message: str):
"""Parle a Hermes (masters et porteurs du role hermes)."""
if ctx.guild and not self._author_allowed(ctx.author):
return await ctx.send(f"> Acces refuse : reserve au role `{ROLE_NAME}`.")
async with ctx.typing():
try:
answer = await self._ask(ctx.author.id, message)
except Exception as exc: # noqa: BLE001
answer = f"> Erreur pont Hermes : {exc}"
await self.send_long(ctx.channel, answer)
@commands.command(name="hermes-reset")
async def hermes_reset(self, ctx: commands.Context):
"""Oublie l'historique de conversation courant avec Hermes."""
if ctx.guild and not self._author_allowed(ctx.author):
return
self.histories.pop(ctx.author.id, None)
await ctx.send("> Historique Hermes reinitialise.")
@commands.command(name="hermes-setup")
async def hermes_setup(self, ctx: commands.Context):
"""Cree le role Discord hermes et l'attribue aux masters du bot."""
if ctx.author.id not in masters:
return await ctx.send("> Acces refuse : reserve aux masters.")
if not ctx.guild:
return await ctx.send("> Commande utilisable uniquement en serveur.")
await create_role_if_missing(ctx, ROLE_NAME, self.color)
await ctx.send(f"> Role `{ROLE_NAME}` pret.")
# --- surface de test locale (loopback) ---------------------------------
@web.middleware
async def _ask_auth(self, request: web.Request, handler):
"""Meme convention que cogs/api.py : Bearer exige seulement si le token est pose."""
if self.api_token and request.path != "/live":
if request.headers.get("Authorization") != f"Bearer {self.api_token}":
return web.json_response({"error": "Unauthorized"}, status=401)
return await handler(request)
@commands.Cog.listener(name="on_ready")
async def start_ask_server(self):
"""Demarre la surface de test (idempotent : on_ready peut se redeclencher)."""
async with self.ask_lock:
if self.ask_site is not None:
return
app = web.Application(middlewares=[self._ask_auth])
app.router.add_get("/live", self.ask_live)
app.router.add_post("/hermes/ask", self.ask_http)
self.ask_runner = web.AppRunner(app)
await self.ask_runner.setup()
site = web.TCPSite(self.ask_runner, self.ask_host, self.ask_port)
try:
await site.start()
except OSError:
await self.ask_runner.cleanup()
self.ask_runner = None
raise
self.ask_site = site
print(f"Hermes ask server listening on {self.ask_host}:{self.ask_port}")
def cog_unload(self):
"""Arrete la surface de test."""
if self.ask_runner:
asyncio.create_task(self.ask_runner.cleanup())
async def ask_live(self, request: web.Request):
"""Healthcheck."""
return web.Response(text="OK")
async def ask_http(self, request: web.Request):
"""POST {"message": "..."} -> {"answer": "..."} (historique par user_id)."""
try:
body = await request.json()
except Exception: # noqa: BLE001
return web.json_response({"error": "JSON invalide."}, status=400)
body = body or {}
message = body.get("message")
if not message:
return web.json_response({"error": "Champ 'message' manquant."}, status=400)
try:
user_id = int(body.get("user_id") or (masters[0] if masters else 0))
except (TypeError, ValueError):
return web.json_response({"error": "user_id invalide."}, status=400)
try:
answer = await self._ask(user_id, message)
except Exception as exc: # noqa: BLE001
return web.json_response(
{"error": f"{type(exc).__name__}: {exc}"}, status=502
)
return web.json_response({"answer": answer, "user_id": user_id})
# --- utilitaires -------------------------------------------------------
@staticmethod
async def send_long(channel, text: str):
"""Decoupe les reponses de plus de 2000 caracteres (limite Discord)."""
if not text:
text = "> (reponse vide)"
for i in range(0, len(text), 1990):
await channel.send(text[i : i + 1990])
def setup(bot: commands.Bot):
"""Setup du cog Hermes."""
bot.add_cog(Hermes(bot))
+2 -2
View File
@@ -81,6 +81,6 @@ class IALab(commands.Cog):
await self.say(ctx, d["answer"], "en")
async def setup(bot):
def setup(bot):
"""Setup the ialab cog."""
await bot.add_cog(IALab(bot))
bot.add_cog(IALab(bot))
+2 -2
View File
@@ -215,5 +215,5 @@ class Jail(commands.Cog):
# @commands.
async def setup(bot):
await bot.add_cog(Jail(bot))
def setup(bot):
bot.add_cog(Jail(bot))
+2 -2
View File
@@ -20,6 +20,6 @@ class Junior(commands.Cog):
self.bot = bot
async def setup(bot):
def setup(bot):
"""Setup the ialab cog."""
await bot.add_cog(Junior(bot))
bot.add_cog(Junior(bot))
+2 -2
View File
@@ -38,5 +38,5 @@ class LeaderBoard(commands.Cog):
await ctx.send(embed=embed)
async def setup(bot):
await bot.add_cog(LeaderBoard(bot))
def setup(bot):
bot.add_cog(LeaderBoard(bot))
+2 -2
View File
@@ -153,6 +153,6 @@ class Leekwars(commands.Cog):
await ctx.send(msg)
async def setup(bot):
def setup(bot):
"""Setup the leekwars cog."""
await bot.add_cog(Leekwars(bot))
bot.add_cog(Leekwars(bot))
+112
View File
@@ -0,0 +1,112 @@
#!/bin/env python
from config.config import access, cluster
from discord.ext import commands, tasks
from discord.ext.commands import MessageNotFound
import os
import discord
import yaml
ROLE = "Library"
class Library(commands.Cog):
"""Store config on discord servers as attachments."""
tmp_directory = os.environ.get("TMP_DIRECTORY") or "/tmp"
def __init__(self, bot: commands.Bot):
self.bot = bot
self.filename = "config.yml"
self.filepath = f"/tmp/{self.filename}"
self.channel_id = int(os.environ.get("CHANNEL_ID_CONFIG") or 0)
# @property
# def storage(self):
# return self.bot.get_cog("Storage")
@property
def channel(self) -> discord.TextChannel:
return self.bot.get_channel(self.channel_id)
async def create_config(self):
"""Créer un fichier de config pour la librairie si une existe pas déjà."""
config = dict(library=dict(channel_id=self.channel_id))
with open(self.filepath, "w") as f:
yaml.safe_dump(config, f)
if not self.channel:
raise ValueError("Discord channel not found")
with open(self.filepath, 'rb') as f:
await self.channel.send(file=discord.File(f, filename=self.filename))
@commands.group(aliases=["lib"])
async def library(self, ctx: commands.Context):
"""Groupe de commandes du cog 'Backup'."""
if not ctx.invoked_subcommand:
if not self.channel_id:
await ctx.send("> Un salon textuel doit être claim pour utiliser la config.")
else:
await ctx.send("> Aucune sous-commande invokée.")
@library.command(name="claim")
@commands.has_role(ROLE)
async def claim_channel(self, ctx: commands.Context):
"""Déclare le salon textuel de la library."""
self.channel_id = ctx.channel.id
await self.create_config()
await ctx.send(f"> Salon textuel {ctx.channel.id} sélectionné pour le stockage de la config.")
@library.command(name="write")
@commands.has_role(ROLE)
async def write_cmd(self, ctx: commands.Context, key: str, value):
"""Écrit une valeur dans la library."""
await self.write(key, value)
# await ctx.send(f"> Salon textuel {ctx.channel.id} sélectionné pour le stockage de la config.")
async def read(self, attribute):
return (await self.get_library())[attribute]
async def write(self, attribute, value):
library = await self.get_library()
print(1, library)
library[attribute] = value
print(2, library)
await self.set_library(library)
async def search_attachment(self, limit: int):
"""Search for the attachment and message."""
async for message in self.channel.history(limit=limit, oldest_first=False): # Adjust limit as needed
for attachment in message.attachments:
print(f"attachement: {attachment.filename}")
if attachment.filename == self.filename:
return (message, attachment)
raise MessageNotFound("No library file found.")
async def get_library(self, limit=10) -> dict:
"""Get the library from discord"""
_, attachment = await self.search_attachment(limit)
await attachment.save(self.filepath)
with open(self.filepath, "r") as f:
d = yaml.safe_load(f)
return d
async def set_library(self, library, limit=10):
"""Set the library to discord"""
message, attachment = await self.search_attachment(limit)
# await message.remove_attachments(attachment)
await message.delete()
with open(self.filepath, "w") as f:
yaml.safe_dump(library, f)
with open(self.filepath, "rb") as f:
# await message.add_files()
file = discord.File(fp=f, filename=self.filename)
await self.channel.send(file=file)
def setup(bot: commands.Bot):
bot.add_cog(Library(bot))
+2 -2
View File
@@ -181,6 +181,6 @@ class Marc(commands.Cog):
return await ctx.send(f"> Stored in **{args[0]}** : **{value}**")
async def setup(bot: commands.Bot):
def setup(bot: commands.Bot):
"""Setup le marc cog."""
await bot.add_cog(Marc(bot))
bot.add_cog(Marc(bot))
+2 -2
View File
@@ -185,5 +185,5 @@ class Messenger(commands.Cog):
return embed
async def setup(bot):
await bot.add_cog(Messenger(bot))
def setup(bot):
bot.add_cog(Messenger(bot))
+2 -2
View File
@@ -273,6 +273,6 @@ class Mongo(commands.Cog):
await ctx.send(f"> {text_path}")
async def setup(bot):
def setup(bot):
"""Setup the cog."""
await bot.add_cog(Mongo(bot))
bot.add_cog(Mongo(bot))
+2 -2
View File
@@ -668,5 +668,5 @@ class Music(commands.Cog):
print("voice_state ok")
async def setup(bot):
await bot.add_cog(Music(bot))
def setup(bot):
bot.add_cog(Music(bot))
+2 -2
View File
@@ -369,5 +369,5 @@ class marcMusic(commands.Cog):
await self.join(ctx)
async def setup(bot):
await bot.add_cog(marcMusic(bot))
def setup(bot):
bot.add_cog(marcMusic(bot))
+2 -2
View File
@@ -234,5 +234,5 @@ class Planning(commands.Cog):
return embed
async def setup(bot):
await bot.add_cog(Planning(bot))
def setup(bot):
bot.add_cog(Planning(bot))
+2 -2
View File
@@ -253,5 +253,5 @@ class PlayList(commands.Cog):
# @playlist.command(name)
async def setup(bot):
await bot.add_cog(PlayList(bot))
def setup(bot):
bot.add_cog(PlayList(bot))
+2 -2
View File
@@ -42,5 +42,5 @@ class Quest(commands.Cog, name="Quête"):
"""Retire une quête."""
async def setup(bot: commands.Bot):
await bot.add_cog(Quest(bot))
def setup(bot: commands.Bot):
bot.add_cog(Quest(bot))
+2 -2
View File
@@ -35,5 +35,5 @@ class Quiz(commands.Cog):
await self.quiz.next(ctx).embed
async def setup(bot):
await bot.add_cog(Quiz(bot))
def setup(bot):
bot.add_cog(Quiz(bot))
+2 -2
View File
@@ -62,5 +62,5 @@ class Record(commands.Cog):
def once_done(self, *args):
print("its done", args)
async def setup(bot):
await bot.add_cog(Record(bot))
def setup(bot):
bot.add_cog(Record(bot))
+2 -2
View File
@@ -110,5 +110,5 @@ class Role(commands.Cog):
# async def role_old(self, ctx:)
async def setup(bot):
await bot.add_cog(Role(bot))
def setup(bot):
bot.add_cog(Role(bot))
+2 -2
View File
@@ -99,6 +99,6 @@ class Shell(commands.Cog):
# shell.close()
async def setup(bot):
def setup(bot):
"""Setup the Shell cog."""
await bot.add_cog(Shell(bot))
bot.add_cog(Shell(bot))
+2 -2
View File
@@ -32,5 +32,5 @@ class Sherlock(commands.Cog):
await ctx.send(embed=embed)
async def setup(bot):
await bot.add_cog(Sherlock(bot))
def setup(bot):
bot.add_cog(Sherlock(bot))
+2 -2
View File
@@ -156,5 +156,5 @@ class Shop(commands.Cog):
pass
async def setup(bot):
await bot.add_cog(Shop(bot))
def setup(bot):
bot.add_cog(Shop(bot))
+2 -2
View File
@@ -129,5 +129,5 @@ class SoundDeck(commands.Cog):
sound.last_used_time = time.time()
async def setup(bot):
await bot.add_cog(SoundDeck(bot))
def setup(bot):
bot.add_cog(SoundDeck(bot))
+95 -43
View File
@@ -9,21 +9,23 @@ __author__ = "Marc Partensky"
import os, time, subprocess
import discord
import aiofiles
# from discord.errors import NotFound
from discord.ext.commands import MessageNotFound
from discord.ext import commands
import server
from aiohttp import web
MAX_CHUNK_SIZE = 8 * 1024 * 1024 # 8 MB
class Storage(commands.Cog):
"""Store files on discord using HTTP web service to interact with the bot."""
tmp_directory = os.environ.get("TMP_DIRECTORY") or "/tmp"
def __init__(self, bot: commands.Bot):
self.bot = bot
self.channel_id = int(os.environ["CHANNEL_ID_NOTIF"])
self.channel_id = int(os.environ.get("CHANNEL_ID_NOTIF", "0"))
self.password = "password"
self.tmp_directory = "/tmp"
# self.tmp_directory = "/tmp"
def get_timestamp(self):
"""Return a human readable timestamp."""
@@ -68,14 +70,14 @@ class Storage(commands.Cog):
if not chunk:
break
tmp = self.get_timestamp()
chunk_path = f"{self.tmp_directory}/{os.path.basename(filepath)}.{tmp}.{part_number}"
chunk_path = f"{Storage.tmp_directory}/{os.path.basename(filepath)}.{tmp}.{part_number}"
async with aiofiles.open(chunk_path, 'wb') as chunk_file:
await chunk_file.write(chunk)
chunk_paths.append(chunk_path)
part_number += 1
return chunk_paths
async def send_file_in_chunks(self, filepath: str, file_name: str, channel_id: int = 0):
async def send_file_in_chunks(self, filepath: str, file_name: str, channel_id: int = 0, timestamp: str=""):
"""
Send a file to Discord in chunks using filepath, file_name and optional channel_id.
"""
@@ -86,10 +88,11 @@ class Storage(commands.Cog):
if not channel:
raise ValueError("Discord channel not found")
tmp = self.get_timestamp()
if not timestamp:
timestamp = self.get_timestamp()
for i, chunk_path in enumerate(chunk_paths, start=1):
chunk_file_name = f"{file_name}.{tmp}.{i}"
chunk_file_name = f"{file_name}.{timestamp}.{i}"
with open(chunk_path, 'rb') as f:
await channel.send(file=discord.File(f, filename=chunk_file_name))
@@ -100,7 +103,11 @@ class Storage(commands.Cog):
for filepath in filepaths:
os.remove(filepath)
@server.add_route(path="/send/file", method="POST", cog="API")
def generate_url(self, filename: str, channel_id: int, timestamp: str):
"""Generate an url to retrieve a file already sent."""
PUBLIC_API_URL = os.environ["PUBLIC_API_URL"]
return f"{PUBLIC_API_URL}/retrieve?filename={filename}&channel_id={channel_id}&timestamp={timestamp}"
async def send_file_to_channel(self, request: web.Request):
"""Endpoint to send a file to a specific channel by ID."""
filename: str
@@ -116,7 +123,7 @@ class Storage(commands.Cog):
field = await reader.next()
if field.name == "file":
filename = field.filename
filepath = f"{self.tmp_directory}/{filename}"
filepath = f"{Storage.tmp_directory}/{filename}"
# Save the file temporarily
async with aiofiles.open(filepath, 'wb') as f:
@@ -133,40 +140,82 @@ class Storage(commands.Cog):
return web.json_response({"error": "Channel not found"}, status=404)
# await channel.send(file=discord.File(filepath))
await self.send_file_in_chunks(filepath, filename, channel_id)
storage = Storage(self.bot)
timestamp = storage.get_timestamp()
await storage.send_file_in_chunks(filepath, filename, channel_id, timestamp)
url = storage.generate_url(filename, channel_id, timestamp)
# Optionally, delete the temp file after sending it
os.remove(filepath)
message = f"Uploaded {filename} channel: {channel_id}"
message = f"Uploaded {filename}"
print(message)
return web.json_response({"message": message}, status=200)
print(url)
return web.json_response({"message": message, "url": url}, status=200)
@server.add_route(path="/retrieve/file", method="POST", cog="API")
async def retrieve_file_from_channel(self, request: web.Request):
async def retrieve_file_from_channel_request(self, request: web.Request):
"""
Endpoint to retrieve, reassemble, and download the original file from a channel
based on its name and the channel ID.
based on its filename, the timestamp and potentially channel_id.
"""
# Extract the JSON data from the request
body = await request.json()
body = request.query
# Extract the required information: channel ID and original filename
channel_id = body.get("channel_id") or self.channel_id
original_filename = body.get("filename")
chosen_timestamp = body.get("timestamp")
print('timestamp:', chosen_timestamp)
filename = body.get("filename")
timestamp = body.get("timestamp")
limit = body.get("limit")
print('body:', body)
print("channel_id:", channel_id)
print("filename:", filename)
print("timestamp:", timestamp)
print("limit:", limit)
if not channel_id or not original_filename:
return web.json_response({"error": "Channel ID or filename missing"}, status=400)
if not channel_id:
return web.json_response({"error": "channel_id missing"}, status=400)
if not filename:
return web.json_response({"error": "filename missing"}, status=400)
if limit:
try:
limit = int(limit)
except:
return web.json_response({"error": "limit is not a number"}, status=400)
# Fetch the Discord channel
channel: discord.TextChannel = await self.bot.fetch_channel(channel_id)
if not channel:
return web.json_response({"error": "Channel not found"}, status=404)
return web.json_response({"error": "channel not found"}, status=404)
print("channel:", channel)
try:
reassembled_filepath = await Storage(self.bot).retrieve_file_from_channel(
filename,
channel,
timestamp,
limit
)
headers = {'Content-Disposition': f'attachment; filename="{filename}"'}
return web.FileResponse(reassembled_filepath, headers=headers)
except MessageNotFound as e:
return web.json_response({"error": str(e)}, status=404)
async def retrieve_file_from_channel(
self,
filename: str,
channel: discord.TextChannel,
timestamp: str,
limit: int = 1000,
):
"""Retrieve a file from a channel using:
- filename
- channel
- timestamp
- limit [1000] (number of messages checked)
"""
# Prepare the path to save the reassembled file
reassembled_filepath = f"{self.tmp_directory}/{original_filename}"
reassembled_filepath = f"{Storage.tmp_directory}/{filename}"
print("reassembled_filepath:", reassembled_filepath)
# Initialize list to collect all file chunks
file_chunks = []
@@ -175,9 +224,9 @@ class Storage(commands.Cog):
found = False
def is_well_formed(dfilename: str):
if not dfilename.startswith(original_filename):
if not dfilename.startswith(filename):
return False
dextension = dfilename[len(original_filename)+1:]
dextension = dfilename[len(filename)+1:]
if dextension.count(".") != 1:
return False
dtimestamp, dpart = dextension.split(".")
@@ -192,13 +241,16 @@ class Storage(commands.Cog):
def deconstruct(dfilepath: str):
dfilename = dfilepath.split('/')[-1]
dextension = dfilename[len(original_filename)+1:]
dextension = dfilename[len(filename)+1:]
dtimestamp, dpart = dextension.split(".")
return dextension, dtimestamp, dpart
# Iterate through the channel's message history to find the file chunks
async for message in channel.history(limit=1000): # Adjust limit as needed
i = 0
async for message in channel.history(limit=limit, oldest_first=False): # Adjust limit as needed
i+=1
if i%100==0: print(i, "messages")
if stop and found: break
for attachment in message.attachments:
print(f"attachement found: {attachment.filename}")
@@ -207,8 +259,9 @@ class Storage(commands.Cog):
# print(f"skip and delete {dfilename} because malformed")
await message.delete()
continue
if dfilename.startswith(original_filename):
dextension = dfilename[len(original_filename)+1:]
if dfilename.startswith(filename):
dextension = dfilename[len(filename)+1:]
# print(filename, dfilename, dextension)
if dextension.count(".") != 1:
break
dtimestamp, dpart = dextension.split(".")
@@ -216,23 +269,23 @@ class Storage(commands.Cog):
# print("dextension", dextension)
# print("dtimestamp", dtimestamp)
# print("dpart", dpart)
# print("chosen_timestamp", chosen_timestamp)
if not chosen_timestamp:
chosen_timestamp = dtimestamp
if chosen_timestamp == dtimestamp:
# print("timestamp", timestamp)
if not timestamp:
timestamp = dtimestamp
if timestamp == dtimestamp:
# print("found", dtimestamp)
found = True
elif found:
# print("stop", chosen_timestamp, "!=", dtimestamp)
stop = True
break
# print("stop", timestamp, "!=", dtimestamp)
stop = True
break
chunk_path = f"{self.tmp_directory}/{dfilename}"
chunk_path = f"{Storage.tmp_directory}/{dfilename}"
await attachment.save(chunk_path)
file_chunks.append(chunk_path)
if not file_chunks:
return web.json_response({"error": "No file chunks found in the channel."}, status=404)
raise MessageNotFound("No file chunks found in the channel.")
# Sort the chunks by part number
print("sorting chunks", file_chunks)
@@ -250,8 +303,7 @@ class Storage(commands.Cog):
os.remove(chunk_path)
# Serve the reassembled file as a download response
headers = {'Content-Disposition': f'attachment; filename="{original_filename}"'}
return web.FileResponse(reassembled_filepath, headers=headers)
return reassembled_filepath
async def setup(bot: commands.Bot):
await bot.add_cog(Storage(bot))
def setup(bot: commands.Bot):
bot.add_cog(Storage(bot))
+2 -2
View File
@@ -110,5 +110,5 @@ class Test(commands.Cog):
await ctx.send(user.id)
async def setup(bot):
await bot.add_cog(Test(bot))
def setup(bot):
bot.add_cog(Test(bot))
+2 -2
View File
@@ -101,5 +101,5 @@ class TextToSpeech(commands.Cog):
ctx.voice_client.play(source, after=after)
async def setup(bot):
await bot.add_cog(TextToSpeech(bot))
def setup(bot):
bot.add_cog(TextToSpeech(bot))
+2 -2
View File
@@ -337,5 +337,5 @@ class TrafficSign(commands.Cog):
"""Compétition sous forme de quiz."""
async def setup(bot):
await bot.add_cog(TrafficSign(bot))
def setup(bot):
bot.add_cog(TrafficSign(bot))
+2 -2
View File
@@ -28,5 +28,5 @@ class Troll(commands.Cog):
await ctx.send("En développement")
async def setup(bot):
await bot.add_cog(Troll(bot))
def setup(bot):
bot.add_cog(Troll(bot))
+2 -2
View File
@@ -70,5 +70,5 @@ class TypingRace(commands.Cog):
await ctx.send(msg)
async def setup(bot):
await bot.add_cog(TypingRace(bot))
def setup(bot):
bot.add_cog(TypingRace(bot))
+2 -2
View File
@@ -269,6 +269,6 @@ class User(commands.Cog, name="Utilisateur"):
)
async def setup(bot):
def setup(bot):
"""Setup the user cog."""
await bot.add_cog(User(bot))
bot.add_cog(User(bot))
+2 -2
View File
@@ -164,5 +164,5 @@ class VoiceChannel(commands.Cog):
break
async def setup(bot):
await bot.add_cog(VoiceChannel(bot))
def setup(bot):
bot.add_cog(VoiceChannel(bot))
+2 -2
View File
@@ -653,5 +653,5 @@ class Web(commands.Cog):
return await ctx.send(shortened_url)
async def setup(bot):
await bot.add_cog(Web(bot))
def setup(bot):
bot.add_cog(Web(bot))
+1 -1
View File
@@ -6,6 +6,6 @@ client_id = os.environ["DISCORD_CLIENT_ID"]
mongo_username = os.environ["DISCORD_MONGO_USERNAME"]
mongo_password = os.environ["DISCORD_MONGO_PASSWORD"]
mongo_cluster = os.environ["DISCORD_MONGO_CLUSTER"]
wolfram_alpha_id = os.environ["WOLFRAM_ALPHA_ID"]
wolfram_alpha_id = os.environ.get("WOLFRAM_ALPHA_ID", "")
mongo_url = f"mongodb+srv://{mongo_username}:{mongo_password}@{mongo_cluster}"
+1 -1
View File
@@ -17,7 +17,7 @@ class Quiz:
owners: str,
timestamp: float = time.time(),
questions: list = [],
seed: int = random.randint(1e9, 1e10 - 1),
seed: int = random.randint(int(1e9), int(1e10) - 1),
n=0,
):
"""Create a quiz using the list of questions."""
Generated
+61
View File
@@ -0,0 +1,61 @@
{
"nodes": {
"flake-utils": {
"inputs": {
"systems": "systems"
},
"locked": {
"lastModified": 1731533236,
"narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=",
"owner": "numtide",
"repo": "flake-utils",
"rev": "11707dc2f618dd54ca8739b309ec4fc024de578b",
"type": "github"
},
"original": {
"owner": "numtide",
"repo": "flake-utils",
"type": "github"
}
},
"nixpkgs": {
"locked": {
"lastModified": 1789149629,
"narHash": "sha256-H6GwaZzZf+4npqv0tph94w9tZddSjFjmQrVsW0z78uk=",
"owner": "NixOS",
"repo": "nixpkgs",
"rev": "eaad089433ca2bb662274377d33df3d0e51ef28b",
"type": "github"
},
"original": {
"owner": "NixOS",
"ref": "nixos-unstable",
"repo": "nixpkgs",
"type": "github"
}
},
"root": {
"inputs": {
"flake-utils": "flake-utils",
"nixpkgs": "nixpkgs"
}
},
"systems": {
"locked": {
"lastModified": 1681028828,
"narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=",
"owner": "nix-systems",
"repo": "default",
"rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e",
"type": "github"
},
"original": {
"owner": "nix-systems",
"repo": "default",
"type": "github"
}
}
},
"root": "root",
"version": 7
}
+146
View File
@@ -0,0 +1,146 @@
{
description = "discord-bot";
inputs = {
nixpkgs.url = "github:NixOS/nixpkgs/nixos-unstable";
flake-utils.url = "github:numtide/flake-utils";
};
outputs = {
self,
nixpkgs,
flake-utils,
...
}:
flake-utils.lib.eachDefaultSystem (system: let
pkgs = nixpkgs.legacyPackages.${system};
python = pkgs.python312;
src = ./.;
libs = with pkgs; [
stdenv.cc.cc.lib
zlib
openssl
libffi
libopus
libxcrypt-legacy
postgresql.lib
];
in {
packages.default = pkgs.writeShellApplication {
name = "discord-bot";
runtimeInputs = [
python
pkgs.uv
pkgs.ffmpeg
pkgs.chromedriver
pkgs.chromium
pkgs.ncurses
];
text = ''
export UV_PYTHON=${python.interpreter}
export UV_PYTHON_DOWNLOADS=never
export UV_PYTHON_PREFERENCE=only-system
export UV_PROJECT_ENVIRONMENT="''${RUNTIME_DIRECTORY:-$PWD}/venv"
export LD_LIBRARY_PATH=${pkgs.lib.makeLibraryPath libs}
uv sync --frozen --no-dev --project ${src}
sync
VENV="$UV_PROJECT_ENVIRONMENT"
BOT_DIR="$("$VENV/bin/python" -c 'import discord_bot, pathlib; print(pathlib.Path(discord_bot.__file__).parent)')"
export PYTHONPATH="$BOT_DIR"
cd "$BOT_DIR"
exec "$VENV/bin/python" -m discord_bot
'';
};
devShells.default = pkgs.mkShell {
packages = [
python
pkgs.uv
pkgs.just
pkgs.ffmpeg
pkgs.chromedriver
pkgs.chromium
];
env = {
UV_PYTHON = python.interpreter;
UV_PYTHON_DOWNLOADS = "never";
UV_PYTHON_PREFERENCE = "only-system";
LD_LIBRARY_PATH = pkgs.lib.makeLibraryPath libs;
};
shellHook = ''
echo "python $(python --version), uv $(uv --version)"
[ -d .venv ] || uv venv
source .venv/bin/activate
'';
};
})
// {
nixosModules.default = {
config,
lib,
pkgs,
...
}: let
cfg = config.services.discord-bot;
in {
options.services.discord-bot = {
enable = lib.mkEnableOption "discord-bot";
package = lib.mkOption {
type = lib.types.package;
default = self.packages.${pkgs.stdenv.hostPlatform.system}.default;
};
environmentFile = lib.mkOption {type = lib.types.path;};
host = lib.mkOption {
type = lib.types.str;
default = "127.0.0.1";
};
port = lib.mkOption {
type = lib.types.port;
default = 8050;
};
};
config = lib.mkIf cfg.enable {
users.users.discord-bot = {
isSystemUser = true;
group = "discord-bot";
};
users.groups.discord-bot = {};
systemd.services.discord-bot = {
wantedBy = ["multi-user.target"];
wants = ["network-online.target"];
after = ["network-online.target"];
environment = {
DISCORD_BOT_HOST = cfg.host;
DISCORD_BOT_PORT = toString cfg.port;
UV_CACHE_DIR = "/var/cache/discord-bot/uv";
HOME = "/var/lib/discord-bot";
TERM = "dumb";
};
serviceConfig = {
ExecStart = lib.getExe cfg.package;
EnvironmentFile = cfg.environmentFile;
User = "discord-bot";
Group = "discord-bot";
StateDirectory = "discord-bot";
CacheDirectory = "discord-bot";
RuntimeDirectory = "discord-bot";
RuntimeDirectorySize = "4G";
WorkingDirectory = "/var/lib/discord-bot";
Restart = "always";
RestartSec = 10;
ProtectSystem = "strict";
ProtectHome = true;
PrivateTmp = true;
NoNewPrivileges = true;
};
};
};
};
};
}
+4 -3
View File
@@ -3,8 +3,8 @@ name = "discord-bot"
version = "0.1.0"
description = "Add your description here"
readme = "README.md"
requires-python = "==3.8.13"
# requires-python = "==3.12"
# requires-python = "==3.8.13"
requires-python = ">=3.12"
dependencies = [
"beautifulsoup4",
"fbchat",
@@ -23,7 +23,6 @@ dependencies = [
"pyyaml",
"youtube-dl",
"python-dotenv",
"dpy-http-server",
"pexpect",
"dnspython",
"requests-futures",
@@ -37,8 +36,10 @@ dependencies = [
"websockets==12.0",
"py-cord[voice]==2.5.0",
"psycopg2-binary",
# "psycopg2",
"aiofiles>=24.1.0",
"apscheduler>=3.10.4",
"async-timeout>=5.0.1",
]
[build-system]
+182 -248
View File
@@ -1,213 +1,168 @@
# This file was autogenerated by uv via the following command:
# uv pip compile requirements.txt --universal --output-file requirements.txt
aenum==2.2.6 ; python_full_version <= '3.8.13'
# uv pip compile pyproject.toml -o requirements.txt
aenum==2.2.6
# via fbchat
aiofiles==24.1.0
# via discord-bot (pyproject.toml)
aiohappyeyeballs==2.4.0
# via aiohttp
aiohttp==3.10.5
# via py-cord
aiosignal==1.3.1
# via aiohttp
annotated-types==0.7.0
# via pydantic
anyio==4.4.0
# via
# -r requirements.txt
# fbchat
aiohttp==3.8.5 ; python_full_version <= '3.8.13'
# groq
# httpx
apscheduler==3.10.4
# via discord-bot (pyproject.toml)
async-timeout==5.0.1
# via discord-bot (pyproject.toml)
attrs==24.2.0
# via
# -r requirements.txt
# discord-py
# dpy-http-server
# py-cord
aiosignal==1.3.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
async-timeout==4.0.3 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
attrs==23.1.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
# fbchat
# outcome
# trio
beautifulsoup4==4.12.2 ; python_full_version <= '3.8.13'
beautifulsoup4==4.12.3
# via
# -r requirements.txt
# discord-bot (pyproject.toml)
# fbchat
# wikipedia
certifi==2023.7.22 ; python_full_version <= '3.8.13'
certifi==2024.8.30
# via
# -r requirements.txt
# httpcore
# httpx
# requests
# selenium
cffi==1.15.1 ; python_full_version <= '3.8.13'
cffi==1.17.1
# via
# -r requirements.txt
# cryptography
# pynacl
# trio
charset-normalizer==3.2.0 ; python_full_version <= '3.8.13'
charset-normalizer==3.3.2
# via requests
click==7.1.2
# via
# -r requirements.txt
# aiohttp
# requests
click==7.1.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# gtts
# nightcore
# translate
colorama==0.4.6 ; python_full_version <= '3.8.13'
# via -r requirements.txt
cryptography==41.0.3 ; python_full_version <= '3.8.13'
colorama==0.4.6
# via discord-bot (pyproject.toml)
cryptography==43.0.1
# via pyjwt
deprecated==1.2.14
# via pygithub
distro==1.9.0
# via groq
dnspython==2.6.1
# via
# -r requirements.txt
# pyjwt
deprecated==1.2.14 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# pygithub
discord-py==2.3.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# dpy-http-server
dnspython==2.4.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# discord-bot (pyproject.toml)
# pymongo
dpy-http-server==0.1.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
exceptiongroup==1.1.2 ; python_full_version <= '3.8.13'
fake-useragent==1.5.1
# via discord-bot (pyproject.toml)
fbchat==1.9.7
# via discord-bot (pyproject.toml)
frozenlist==1.4.1
# via
# -r requirements.txt
# trio
# trio-websocket
fake-useragent==1.2.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
fbchat==1.9.7 ; python_full_version <= '3.8.13'
# via -r requirements.txt
frozenlist==1.4.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
# aiosignal
future==0.18.3 ; python_full_version <= '3.8.13'
# via -r requirements.txt
gtts==2.2.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
h11==0.14.0 ; python_full_version <= '3.8.13'
future==1.0.0
# via discord-bot (pyproject.toml)
groq==0.9.0
# via discord-bot (pyproject.toml)
gtts==2.2.1
# via discord-bot (pyproject.toml)
h11==0.14.0
# via
# -r requirements.txt
# httpcore
# wsproto
html2text==2020.1.16 ; python_full_version <= '3.8.13'
# via -r requirements.txt
idna==3.4 ; python_full_version <= '3.8.13'
html2text==2024.2.26
# via discord-bot (pyproject.toml)
httpcore==1.0.5
# via httpx
httpx==0.27.2
# via
# -r requirements.txt
# groq
# wolframalpha
idna==3.8
# via
# anyio
# httpx
# requests
# trio
# yarl
importlib-resources==6.0.1 ; python_full_version <= '3.8.13'
jaraco-context==6.0.1
# via wolframalpha
libretranslatepy==2.1.1
# via translate
lxml==5.3.0
# via translate
markdown-it-py==3.0.0
# via rich
mdurl==0.1.2
# via markdown-it-py
more-itertools==10.5.0
# via wolframalpha
multidict==6.0.5
# via
# -r requirements.txt
# fake-useragent
jaraco-context==4.3.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# wolframalpha
joblib==1.3.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# scikit-learn
libretranslatepy==2.1.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# translate
lxml==4.9.3 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# translate
markdown-it-py==3.0.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# rich
mdurl==0.1.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# markdown-it-py
more-itertools==10.1.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# wolframalpha
multidict==6.0.4 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
# wolframalpha
# yarl
nightcore==1.1.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
numpy==1.24.4 ; python_full_version <= '3.8.13'
nightcore==1.1.0
# via discord-bot (pyproject.toml)
outcome==1.3.0.post0
# via trio
paho-mqtt==1.6.1
# via fbchat
pexpect==4.9.0
# via discord-bot (pyproject.toml)
pillow==10.4.0
# via discord-bot (pyproject.toml)
progressbar2==4.5.0
# via discord-bot (pyproject.toml)
psycopg2==2.9.13
# via discord-bot (pyproject.toml)
ptyprocess==0.7.0
# via pexpect
py-cord==2.5.0
# via discord-bot (pyproject.toml)
pycparser==2.22
# via cffi
pydantic==2.9.0
# via groq
pydantic-core==2.23.2
# via pydantic
pydub==0.23.1
# via nightcore
pygithub==2.4.0
# via discord-bot (pyproject.toml)
pygments==2.18.0
# via rich
pyjwt==2.9.0
# via pygithub
pymongo==4.8.0
# via discord-bot (pyproject.toml)
pynacl==1.5.0
# via
# -r requirements.txt
# scikit-learn
# scipy
outcome==1.2.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# trio
paho-mqtt==1.6.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# fbchat
pexpect==4.8.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
pillow==10.0.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
profanity-check==1.0.3 ; python_full_version <= '3.8.13'
# via -r requirements.txt
progressbar2==4.2.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
ptyprocess==0.7.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# pexpect
py-cord==2.4.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
pycparser==2.21 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# cffi
pydub==0.23.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# nightcore
pygithub==1.59.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
pygments==2.16.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# rich
pyjwt==2.8.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# py-cord
# pygithub
pymongo==4.4.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
pynacl==1.5.0 ; python_full_version <= '3.8.13'
pysocks==1.7.1
# via
# -r requirements.txt
# pygithub
pysocks==1.7.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# torrequest
# urllib3
python-dotenv==1.0.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
python-utils==3.7.0 ; python_full_version <= '3.8.13'
python-dotenv==1.0.1
# via discord-bot (pyproject.toml)
python-utils==3.8.2
# via progressbar2
pytz==2024.2
# via apscheduler
pyyaml==6.0.2
# via discord-bot (pyproject.toml)
requests==2.32.3
# via
# -r requirements.txt
# progressbar2
pyyaml==6.0.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
requests==2.31.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# discord-bot (pyproject.toml)
# fbchat
# gtts
# pygithub
@@ -215,93 +170,72 @@ requests==2.31.0 ; python_full_version <= '3.8.13'
# torrequest
# translate
# wikipedia
requests-futures==1.0.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
rich==13.5.2 ; python_full_version <= '3.8.13'
# via -r requirements.txt
scikit-learn==1.3.0 ; python_full_version <= '3.8.13'
requests-futures==1.0.1
# via discord-bot (pyproject.toml)
rich==13.8.0
# via discord-bot (pyproject.toml)
selenium==4.24.0
# via discord-bot (pyproject.toml)
six==1.16.0
# via
# -r requirements.txt
# profanity-check
scipy==1.10.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# scikit-learn
selenium==4.11.2 ; python_full_version <= '3.8.13'
# via -r requirements.txt
six==1.16.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# apscheduler
# gtts
sniffio==1.3.0 ; python_full_version <= '3.8.13'
sniffio==1.3.1
# via
# -r requirements.txt
# anyio
# groq
# httpx
# trio
sortedcontainers==2.4.0 ; python_full_version <= '3.8.13'
sortedcontainers==2.4.0
# via trio
soupsieve==2.6
# via beautifulsoup4
stem==1.8.2
# via torrequest
torrequest==0.1.0
# via discord-bot (pyproject.toml)
translate==3.6.1
# via discord-bot (pyproject.toml)
trio==0.26.2
# via
# -r requirements.txt
# trio
soupsieve==2.4.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# beautifulsoup4
stem==1.8.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# torrequest
threadpoolctl==3.2.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# scikit-learn
torrequest==0.1.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
translate==3.6.1 ; python_full_version <= '3.8.13'
# via -r requirements.txt
trio==0.22.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# selenium
# trio-websocket
trio-websocket==0.10.3 ; python_full_version <= '3.8.13'
trio-websocket==0.11.1
# via selenium
typing-extensions==4.12.2
# via
# -r requirements.txt
# selenium
typing-extensions==4.7.1 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# py-cord
# groq
# pydantic
# pydantic-core
# pygithub
# python-utils
# rich
unidecode==1.3.6 ; python_full_version <= '3.8.13'
# via -r requirements.txt
urllib3==2.0.4 ; python_full_version <= '3.8.13'
# selenium
tzdata==2024.1
# via pydantic
tzlocal==5.2
# via apscheduler
unidecode==1.3.8
# via discord-bot (pyproject.toml)
urllib3==2.2.2
# via
# -r requirements.txt
# pygithub
# requests
# selenium
wikipedia==1.4.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
wolframalpha==5.0.0 ; python_full_version <= '3.8.13'
# via -r requirements.txt
wrapt==1.15.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# deprecated
wsproto==1.2.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# trio-websocket
xmltodict==0.13.0 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# wolframalpha
yarl==1.9.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# aiohttp
youtube-dl==2021.12.17 ; python_full_version <= '3.8.13'
# via -r requirements.txt
zipp==3.16.2 ; python_full_version <= '3.8.13'
# via
# -r requirements.txt
# importlib-resources
websocket-client==1.8.0
# via selenium
websockets==12.0
# via discord-bot (pyproject.toml)
wikipedia==1.4.0
# via discord-bot (pyproject.toml)
wolframalpha==5.1.3
# via discord-bot (pyproject.toml)
wrapt==1.16.0
# via deprecated
wsproto==1.2.0
# via trio-websocket
xmltodict==0.13.0
# via wolframalpha
yarl==1.10.0
# via aiohttp
youtube-dl==2021.12.17
# via discord-bot (pyproject.toml)
Executable
+90
View File
@@ -0,0 +1,90 @@
#!/usr/bin/env python
"""Tests pour l'API HTTP du bot Discord."""
import requests
import time
import sys
API_BASE = "http://localhost:8050"
def test_server_running():
"""Vérifie que le serveur répond."""
try:
response = requests.get(f"{API_BASE}/live", timeout=5)
print(f"✓ Serveur actif - Status: {response.status_code}")
return True
except requests.exceptions.ConnectionError:
print("✗ Serveur non accessible")
return False
except Exception as e:
print(f"✗ Erreur: {e}")
return False
def test_send_channel():
"""Test d'envoi de message dans un channel."""
data = {
"id": 1548552725808283748, # CHANNEL_ID_NOTIF
"message": "Test API - Message de test automatique"
}
try:
response = requests.post(f"{API_BASE}/send/channel", json=data, timeout=5)
if response.status_code == 200:
print(f"✓ send/channel - {response.json()}")
return True
else:
print(f"✗ send/channel - Status {response.status_code}: {response.text}")
return False
except Exception as e:
print(f"✗ send/channel - Erreur: {e}")
return False
def test_debug_bot():
"""Test de l'endpoint debug."""
try:
response = requests.get(f"{API_BASE}/debug/bot", timeout=5)
if response.status_code == 200:
data = response.json()
print(f"✓ debug/bot - Bot ID: {data.get('id', 'N/A')}")
return True
else:
print(f"✗ debug/bot - Status {response.status_code}")
return False
except Exception as e:
print(f"✗ debug/bot - Erreur: {e}")
return False
def main():
"""Lance tous les tests."""
print("=" * 50)
print("Tests API Discord Bot")
print("=" * 50)
tests = [
("Serveur actif", test_server_running),
("Debug bot", test_debug_bot),
("Send channel", test_send_channel),
]
results = []
for name, test_func in tests:
print(f"\n[{name}]")
result = test_func()
results.append((name, result))
time.sleep(0.5)
print("\n" + "=" * 50)
print("Résumé:")
print("=" * 50)
passed = sum(1 for _, result in results if result)
total = len(results)
for name, result in results:
status = "✓ PASS" if result else "✗ FAIL"
print(f"{status} - {name}")
print(f"\nRésultat: {passed}/{total} tests réussis")
return 0 if passed == total else 1
if __name__ == "__main__":
sys.exit(main())
Generated
+470 -1331
View File
File diff suppressed because it is too large Load Diff