Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
29 commits
Select commit Hold shift + click to select a range
ed578f3
feat: add rfr core/db
stefan-cooper May 20, 2022
ed259c1
test: fix get_embed test
JayDwee Jun 22, 2022
4a19ef6
fix: move cog to core
stefan-cooper Jun 27, 2022
c19fda5
Merge branch 'master' into feat/rfr-api-cog
Oct 31, 2022
29c3bf9
Remove db2.py and move the code to db.py
VottonDev Oct 31, 2022
5bb8a15
More re-factoring
VottonDev Oct 31, 2022
30626e4
Fix double importing in core.py
VottonDev Oct 31, 2022
1716a07
Fix referencing to db.py
VottonDev Oct 31, 2022
060667e
Fix db manager referencing in testing
VottonDev Oct 31, 2022
3e932fe
Remove references to ReactForRole DBManager
VottonDev Oct 31, 2022
b3b90d0
Remove DBManager references in test_cog.py
VottonDev Oct 31, 2022
8b7458e
Remove all import and remove a unused session import
VottonDev Oct 31, 2022
27904d0
Add session as a param to get_rfr_reaction_roles
VottonDev Oct 31, 2022
b439acb
Move the session param as added to wrong method
VottonDev Oct 31, 2022
1f95586
Add session as param to get_rfr_reaction_role
VottonDev Oct 31, 2022
3c06626
Add session as param to get_rfr_reaction_role_by_emoji_str
VottonDev Oct 31, 2022
2867030
Move session to end of get_rfr_reaction_role_by_emoji_str
VottonDev Oct 31, 2022
999ece3
Write a test for test_get_first_emoji_from_str
VottonDev Nov 1, 2022
09e2aa5
Try statement for setting permissions
VottonDev Nov 20, 2022
b46a23b
Revert "Try statement for setting permissions"
VottonDev Mar 13, 2023
3f71479
Merge remote-tracking branch 'origin/master' into feat/rfr-api-cog
JayDwee Mar 18, 2023
8504925
chore: add rfr create API
JayDwee Mar 18, 2023
01f39cd
fix: breaking tests
JayDwee Mar 21, 2023
e8c5808
chore: add all RFR endpoints and tests
JayDwee Mar 28, 2023
7c73d93
temp: change dpytest requirement
JayDwee Mar 28, 2023
247b934
chore: update dpytest version
JayDwee Apr 4, 2023
039c606
Merge branch 'master' into feat/rfr-api-cog
JayDwee Apr 4, 2023
74dd9ee
test: fix failing tests
JayDwee Apr 4, 2023
c8d4a8b
Merge branch 'master' into feat/rfr-api-cog
JayDwee Apr 4, 2023
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions koala/cogs/react_for_role/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from . import utils, db, models
from .cog import ReactForRole, setup
from . import utils, db, models, cog, core, api
from .cog import ReactForRole


async def setup(bot):
await cog.setup(bot)
api.setup(bot)
240 changes: 240 additions & 0 deletions koala/cogs/react_for_role/api.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,240 @@
# Futures
# Built-in/Generic Imports
# Libs
from typing import List

import discord
from aiohttp import web
from discord.ext.commands import Bot

import koalabot
from koala.rest.api import parse_request

# Own modules
from . import core
from .dto import ReactRole
from .log import logger
from ... import colours

# Constants
RFR_ENDPOINT = 'react-for-role'

MESSAGE = 'message'
REQUIRED_ROLES = 'required-roles'


class RfrEndpoint:
_bot: koalabot.KoalaBot
"""
The API endpoints for BaseCog
"""

def __init__(self, bot):
self._bot = bot

def register(self, app):
"""
Register the routes for the given application
:param app: The aiohttp.web.Application (likely of the sub app)
:return: app
"""
app.add_routes([web.post('/{}'.format(MESSAGE), self.post_message),
web.get('/{}'.format(MESSAGE), self.get_message),
web.put('/{}'.format(MESSAGE), self.put_message),
web.patch('/{}'.format(MESSAGE), self.patch_message),
web.delete('/{}'.format(MESSAGE), self.delete_message),
web.put('/{}'.format(REQUIRED_ROLES), self.put_required_roles),
web.get('/{}'.format(REQUIRED_ROLES), self.get_required_roles)])
return app

@parse_request
async def post_message(self,
guild_id: int,
channel_id: int,
title: str,
description: str = "",
colour: str = colours.KOALA_GREEN.__str__(),
thumbnail: str = None,
inline: bool = None,
roles: List[dict] = None
):
"""
Create a React For Role message

:param guild_id: Guild ID of RFR message
:param channel_id: Channel ID of RFR message
:param title: Title of RFR message
:param description: Description of RFR message
:param colour: Hex colour code of RFR message
:param thumbnail: thumbnail URL
:param inline: fields should be inline
:param roles: roles for RFR message
:return:
"""
guild = self._bot.get_guild(guild_id)
if roles is not None:
roles = [ReactRole(r["emoji"], r["role_id"]).to_tuple(guild) for r in roles]

return await core.create_rfr_message(bot=self._bot,
guild_id=guild_id,
channel_id=channel_id,
title=title,
description=description,
colour=discord.Colour.from_str(colour),
thumbnail=thumbnail,
inline=inline,
roles=roles)

@parse_request
async def get_message(self,
message_id: int,
guild_id: int,
channel_id: int
):
"""
Get a React For Role message

:param message_id: Message ID of RFR message
:param guild_id: Guild ID of RFR message
:param channel_id: Channel ID of RFR message
:return:
"""
return await core.get_rfr_message_dto(self._bot, int(message_id), int(guild_id), int(channel_id))

@parse_request
async def put_message(self,
message_id: int,
guild_id: int,
channel_id: int,
title: str,
description: str,
colour: str,
thumbnail: str,
inline: bool,
roles: List[dict]
):
"""
Edit a React For Role message

:param message_id: Message ID of RFR message
:param guild_id: Guild ID of RFR message
:param channel_id: Channel ID of RFR message
:param title: Title of RFR message
:param description: Description of RFR message
:param colour: Hex colour code of RFR message
:param thumbnail: thumbnail URL
:param inline: fields should be inline
:param roles: roles for RFR message
:return:
"""
guild = self._bot.get_guild(guild_id)
if roles is not None:
roles = [ReactRole(r["emoji"], r["role_id"]).to_tuple(guild) for r in roles]
return await core.update_rfr_message(bot=self._bot,
message_id=message_id,
guild_id=guild_id,
channel_id=channel_id,
title=title,
description=description,
colour=discord.Colour.from_str(colour),
thumbnail=thumbnail,
inline=inline,
roles=roles)

@parse_request
async def patch_message(self,
message_id: int,
guild_id: int,
channel_id: int,
title: str = None,
description: str = None,
colour: str = None,
thumbnail: str = None,
inline: bool = None,
roles: List[dict] = None
):
"""
Edit a React For Role message

:param message_id: Message ID of RFR message
:param guild_id: Guild ID of RFR message
:param channel_id: Channel ID of RFR message
:param title: Title of RFR message
:param description: Description of RFR message
:param colour: Hex colour code of RFR message
:param thumbnail: thumbnail URL
:param inline: fields should be inline
:param roles: roles for RFR message
:return:
"""
guild = self._bot.get_guild(guild_id)
if roles is not None:
roles = [ReactRole(r["emoji"], r["role_id"]).to_tuple(guild) for r in roles]

if colour is not None:
colour = discord.Colour.from_str(colour)

return await core.update_rfr_message(bot=self._bot,
message_id=message_id,
guild_id=guild_id,
channel_id=channel_id,
title=title,
description=description,
colour=colour,
thumbnail=thumbnail,
inline=inline,
roles=roles)

@parse_request
async def delete_message(self,
message_id: int,
guild_id: int,
channel_id: int
):
"""
Delete a React For Role message

:param message_id: Message ID of RFR message
:param guild_id: Guild ID of RFR message
:param channel_id: Channel ID of RFR message
:return:
"""
await core.delete_rfr_message(self._bot, int(message_id), int(guild_id), int(channel_id))
return {"status": "DELETED", "message_id": message_id}

@parse_request
async def put_required_roles(self,
guild_id: int,
role_ids: List[int] = None
):
"""
Set or edit RFR required roles for a guild

:param guild_id: Guild ID of RFR message
:param role_ids: List of required role IDs
:return:
"""
core.edit_guild_rfr_required_roles(self._bot, guild_id, role_ids)
return core.rfr_list_guild_required_roles(self._bot.get_guild(int(guild_id)))

@parse_request
async def get_required_roles(self, guild_id: int):
"""
Get RFR required roles for a guild

:param guild_id: Guild ID of RFR message
:return:
"""
return core.rfr_list_guild_required_roles(self._bot.get_guild(int(guild_id)))


def setup(bot: Bot):
"""
Load this cog to the KoalaBot.
:param bot: the bot client for KoalaBot
"""
sub_app = web.Application()
endpoint = RfrEndpoint(bot)
endpoint.register(sub_app)
getattr(bot, "koala_web_app").add_subapp('/{}'.format(RFR_ENDPOINT), sub_app)
logger.info("RFR API is ready.")
Loading