-
-
Notifications
You must be signed in to change notification settings - Fork 47
/
Copy pathcog.py
81 lines (72 loc) · 2.85 KB
/
cog.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
"""
Functions and commands that communicate with the Grillbot API.
"""
import asyncio
import json
from io import BytesIO
import aiohttp
import disnake
from disnake.ext import commands
from cogs.base import Base
from rubbergod import Rubbergod
class GrillbotApi(Base, commands.Cog):
def __init__(self, bot: Rubbergod):
super().__init__()
self.bot = bot
self._owner_id = bot.owner_id
async def owner_id(self):
if not self._owner_id:
app_info = await self.bot.application_info()
self._owner_id = app_info.owner.id
return str(self._owner_id)
async def post_karma_store(self, karma_objects):
"""send karma objects to grillbot api"""
if len(karma_objects) == 0:
return
headers = {"ApiKey": self.config.grillbot_api_key, "Author": await self.owner_id()}
async with aiohttp.ClientSession(timeout=aiohttp.ClientTimeout(total=10), headers=headers) as session:
try:
url = f"{self.config.grillbot_api_url}/user/karma/store"
data = [
{
"member_ID": data.member_ID,
"karmaValue": data.karma,
"positive": data.positive,
"negative": data.negative,
}
for data in karma_objects
]
async with session.post(url, json=data):
pass
except (asyncio.exceptions.TimeoutError, aiohttp.client_exceptions.ClientConnectorError):
pass
@commands.Cog.listener()
async def on_message(self, message: disnake.Message):
"""Api point for grillbot"""
if message.author.id not in self.config.grillbot_ids:
return
lines = message.content.split("\n")
if not (lines[0] == "```json" and lines[-1] == "```"):
return
lines = lines[1:-1]
content = "\n".join(lines)
request = json.loads(content)
if "method" not in request or request["method"] not in self.config.grillbot_api_supported_methods:
await message.reply("Unsupported method")
return
params = request["parameters"]
cog = self.bot.get_cog(request["method"])
# check if cog is loaded
if cog:
status, content = await cog.api(message, params)
if status != 0:
await message.reply(content)
return
if isinstance(content, disnake.File):
await message.reply(file=content)
return
res_json = json.dumps(content)
with BytesIO(bytes(res_json, "utf-8")) as file_binary:
await message.reply(file=disnake.File(fp=file_binary, filename="res.json"))
else:
await message.reply("Extension not loaded")