Skip to content

Commit 9e50b0b

Browse files
committed
Added broadcast command
1 parent a76288f commit 9e50b0b

File tree

1 file changed

+35
-0
lines changed

1 file changed

+35
-0
lines changed

cogs/admin.py

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,12 @@
1+
import asyncio
12
from logging import getLogger
3+
import os
24
from pathlib import Path
35
from urllib.parse import quote
46

7+
import aiohttp
8+
import requests
9+
510
import discord
611
import discord.ext.commands as commands
712

@@ -67,6 +72,36 @@ async def lesson(self, ctx: commands.Context, repo: str = "", lesson: str = ""):
6772
await channel.send(file=logo, embed=emb)
6873

6974

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+
70105

71106
@commands.command(aliases=["down"])
72107
@commands.has_role(ADMIN)

0 commit comments

Comments
 (0)