#!/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 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)