|
| 1 | +import asyncio |
1 | 2 | from logging import getLogger |
| 3 | +import os |
2 | 4 | from pathlib import Path |
3 | 5 | from urllib.parse import quote |
4 | 6 |
|
| 7 | +import aiohttp |
| 8 | +import requests |
| 9 | + |
5 | 10 | import discord |
6 | 11 | import discord.ext.commands as commands |
7 | 12 |
|
@@ -67,6 +72,36 @@ async def lesson(self, ctx: commands.Context, repo: str = "", lesson: str = ""): |
67 | 72 | await channel.send(file=logo, embed=emb) |
68 | 73 |
|
69 | 74 |
|
| 75 | + @commands.command(hidden=True) |
| 76 | + @commands.has_role(BUREAU) |
| 77 | + async def broadcast(self, ctx: commands.Context, text: str): |
| 78 | + SHARE_URL = "https://codingame-share.insalgo.fr/api/broadcast" |
| 79 | + SHARE_TOKEN = os.environ['SHARE_TOKEN'] |
| 80 | + headers = { |
| 81 | + 'Authorization': f'Bearer {SHARE_TOKEN}', |
| 82 | + 'Content-Type': 'application/json' |
| 83 | + } |
| 84 | + data = { |
| 85 | + 'content': text |
| 86 | + } |
| 87 | + |
| 88 | + try: |
| 89 | + async with aiohttp.ClientSession() as session: |
| 90 | + async with session.post(SHARE_URL, headers=headers, json=data) as response: |
| 91 | + # Raise an exception for HTTP status 4xx/5xx |
| 92 | + response.raise_for_status() |
| 93 | + response_data = await response.text() |
| 94 | + await ctx.send(f'Broadcast successful!') |
| 95 | + |
| 96 | + except aiohttp.ClientResponseError as e: |
| 97 | + await ctx.send(f'HTTP Error: {e.status} - {e.message}') |
| 98 | + except aiohttp.ClientConnectionError as e: |
| 99 | + await ctx.send(f'Connection Error: {e}') |
| 100 | + except asyncio.TimeoutError: |
| 101 | + await ctx.send('Request timed out') |
| 102 | + except Exception as e: |
| 103 | + await ctx.send(f'Unexpected error: {e}') |
| 104 | + |
70 | 105 |
|
71 | 106 | @commands.command(aliases=["down"]) |
72 | 107 | @commands.has_role(ADMIN) |
|
0 commit comments