From e28f1415e34994f3da907cb22003f75a5eb968af Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 10 Mar 2025 21:40:36 +0100 Subject: [PATCH] =?UTF-8?q?G=C3=A9n=C3=A9ration=20d'une=20image=20avec=20l?= =?UTF-8?q?es=20cantons=20captur=C3=A9s?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .gitignore | 2 + bot.py | 56 ++++++++++- map_blank.svg | 250 +++++++++++++++++++++++++++++++++++++++++++++++ requirements.txt | 1 + 4 files changed, 307 insertions(+), 2 deletions(-) mode change 100644 => 100755 bot.py create mode 100644 map_blank.svg diff --git a/.gitignore b/.gitignore index 8eac53d..c95b7c3 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,5 @@ __pycache__ .venv/ config.py +map.svg +map.png diff --git a/bot.py b/bot.py old mode 100644 new mode 100755 index e077673..31808ef --- a/bot.py +++ b/bot.py @@ -1,18 +1,70 @@ #!/usr/bin/env python3 +from functools import partial +import random +from typing import Literal +from xml.dom import minidom + +import cairosvg import discord from discord.ext import commands from config import * +CANTONS = { + "AG": "Argovie", + "AI": "Appenzell Rhodes-Intérieures", + "AR": "Appenzell Rhodes-Extérieures", + "BE": "Berne", + "BL": "Bâle-Campagne", + "BS": "Bâle-Ville", + "FR": "Fribourg", + "GE": "Genève", + "GL": "Glaris", + "GR": "Grisons", + "JU": "Jura", + "LU": "Lucerne", + "NE": "Neuchâtel", + "NW": "Nidwald", + "OW": "Obwald", + "SG": "Saint-Gall", + "SH": "Schaffhouse", + "SO": "Soleure", + "SZ": "Schwytz", + "TH": "Thurgovie", + "TI": "Tessin", + "UR": "Uri", + "VD": "Vaud", + "VS": "Valais", + "ZG": "Zoug", + "ZH": "Zurich", +} + +CodeCanton = Literal["AG", "AI", "AR", "BE", "BL", "BS", "FR", "GE", "GL", "GR", "JU", "LU", "NE", + "NW", "OW", "SG", "SH", "SO", "SZ", "TH", "TI", "UR", "VD", "VS", "ZG", "ZH"] + + intents = discord.Intents.default() intents.message_content = True bot = commands.Bot(command_prefix='$', intents=intents) @bot.command() -async def test(ctx): - await ctx.send("Hello world!") +async def capture(ctx: commands.Context, canton: CodeCanton): + doc = minidom.parse("map_blank.svg") + path = next(e for e in doc.getElementsByTagName('path') if e.getAttribute('id') == canton) + path.setAttribute('class', "captured-green") + with open('map.svg', 'w') as f: + doc.writexml(f) + cairosvg.svg2png(url='map.svg', write_to='map.png') + with open('map.png', 'rb') as f: + await ctx.send(file=discord.File(f, filename="battle4suisse.png"), ephemeral=True) + +@capture.error +async def capture_error(ctx, error): + if isinstance(error, commands.BadLiteralArgument): + await ctx.send(f"Canton inconnu : {error.argument}, valeurs possibles : {", ".join(error.literals)}") + bot.run(DISCORD_TOKEN) diff --git a/map_blank.svg b/map_blank.svg new file mode 100644 index 0000000..4118831 --- /dev/null +++ b/map_blank.svg @@ -0,0 +1,250 @@ + + + + + + image/svg+xml + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + BE + ZH + LU + UR + SZ + OW + NW + GL + ZG + FR + SO + BS + BL + SH + AR + AI + SG + GR + AG + TG + TI + VD + VS + NE + GE + JU + + + Delémont + + + + Berne + + + + Neuchâtel + + + + Bâle + + + + Fribourg + + + + Genève + + + + Lucerne + + + + Aarau + + + + Zurich + + + + Frauenfeld + + + + Lausanne + + + + Soleure + + + + Saint-Gall + + + + Glaris + + + + Herisau + + + + Appenzell + + + + Schaffhouse + + + + Bellinzone + + + + Sion + + + + Altdorf + + + + Sarnen + + + + Stans + + + + Zoug + + + + Schwytz + + + + Coire + + + Liestal + + + diff --git a/requirements.txt b/requirements.txt index 844f49a..bc98b8c 100644 --- a/requirements.txt +++ b/requirements.txt @@ -1 +1,2 @@ +cairosvg discord.py