Génération d'une image avec les cantons capturés
This commit is contained in:
		
							
								
								
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							
							
						
						
									
										2
									
								
								.gitignore
									
									
									
									
										vendored
									
									
								
							@@ -3,3 +3,5 @@ __pycache__
 | 
			
		||||
.venv/
 | 
			
		||||
 | 
			
		||||
config.py
 | 
			
		||||
map.svg
 | 
			
		||||
map.png
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										56
									
								
								bot.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							
							
						
						
									
										56
									
								
								bot.py
									
									
									
									
									
										
										
										Normal file → Executable file
									
								
							@@ -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)
 | 
			
		||||
 
 | 
			
		||||
							
								
								
									
										250
									
								
								map_blank.svg
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										250
									
								
								map_blank.svg
									
									
									
									
									
										Normal file
									
								
							
										
											
												File diff suppressed because one or more lines are too long
											
										
									
								
							| 
		 After Width: | Height: | Size: 283 KiB  | 
@@ -1 +1,2 @@
 | 
			
		||||
cairosvg
 | 
			
		||||
discord.py
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user