Génération d'une image avec les cantons capturés

This commit is contained in:
Emmy D'Anello 2025-03-10 21:40:36 +01:00
parent 88a2f12153
commit e28f1415e3
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85
4 changed files with 307 additions and 2 deletions

2
.gitignore vendored
View File

@ -3,3 +3,5 @@ __pycache__
.venv/ .venv/
config.py config.py
map.svg
map.png

56
bot.py Normal file → Executable file
View File

@ -1,18 +1,70 @@
#!/usr/bin/env python3 #!/usr/bin/env python3
from functools import partial
import random
from typing import Literal
from xml.dom import minidom
import cairosvg
import discord import discord
from discord.ext import commands from discord.ext import commands
from config import * 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 = discord.Intents.default()
intents.message_content = True intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents) bot = commands.Bot(command_prefix='$', intents=intents)
@bot.command() @bot.command()
async def test(ctx): async def capture(ctx: commands.Context, canton: CodeCanton):
await ctx.send("Hello world!") 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) bot.run(DISCORD_TOKEN)

250
map_blank.svg Normal file

File diff suppressed because one or more lines are too long

After

Width:  |  Height:  |  Size: 283 KiB

View File

@ -1 +1,2 @@
cairosvg
discord.py discord.py