Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Voting Core Migration #425

Open
wants to merge 44 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
44 commits
Select commit Hold shift + click to select a range
1191b86
feat: core migration
abluey May 2, 2023
8ceebb4
test: initial core tests
abluey May 2, 2023
dca52a4
debug: peek
abluey May 2, 2023
d705e79
fix: voting group message issue
JayDwee May 2, 2023
4a74419
test: fix cog tests
abluey May 2, 2023
893fa85
test: core chair and channel tests
abluey May 2, 2023
6345aa4
test: core options, end time, stubs
abluey May 2, 2023
24ee0a6
test: add remaining core tests
abluey May 7, 2023
024866b
feat: api skeleton
abluey May 7, 2023
bdaaec2
refactor: use id as params
abluey May 22, 2023
358ad62
feat: set up vote api
abluey May 22, 2023
1e9b092
test: add no vote test for current_votes
abluey May 22, 2023
77fff96
feat: vote_manager (vm) all in core
abluey May 22, 2023
228d9f1
refactor: vote options as dict inst of str
abluey May 23, 2023
03d9174
test: api tests FAILING
abluey May 23, 2023
795dae1
test: failing not fixed at all cry
abluey May 26, 2023
e8aca0c
fix: slight change
abluey Jun 5, 2023
88448a2
fix: voting api; failing tests still exist
abluey Jun 17, 2023
4e31908
fix: pass Options as objects into API
abluey Jul 4, 2023
88af161
test: fix most failing tests
abluey Jul 4, 2023
58ac018
feat: core migration
abluey May 2, 2023
325f007
test: initial core tests
abluey May 2, 2023
394254c
debug: peek
abluey May 2, 2023
188b081
fix: voting group message issue
JayDwee May 2, 2023
3b452af
test: fix cog tests
abluey May 2, 2023
9bcada1
test: core chair and channel tests
abluey May 2, 2023
a637193
test: core options, end time, stubs
abluey May 2, 2023
5b14ee3
test: add remaining core tests
abluey May 7, 2023
dea11bf
feat: api skeleton
abluey May 7, 2023
212b357
refactor: use id as params
abluey May 22, 2023
9599849
feat: set up vote api
abluey May 22, 2023
929f7ca
test: add no vote test for current_votes
abluey May 22, 2023
662d7e8
feat: vote_manager (vm) all in core
abluey May 22, 2023
fac5d8c
refactor: vote options as dict inst of str
abluey May 23, 2023
4577759
test: api tests FAILING
abluey May 23, 2023
1ae3524
test: failing not fixed at all cry
abluey May 26, 2023
f5b8d54
fix: slight change
abluey Jun 5, 2023
6f0dec8
fix: voting api; failing tests still exist
abluey Jun 17, 2023
82230a3
fix: pass Options as objects into API
abluey Jul 4, 2023
649803c
test: fix most failing tests
abluey Jul 4, 2023
42e20cd
Merge branch 'feature/voting-core-migration' of https://github.com/Ko…
abluey Jul 17, 2023
915a1ed
Merge master into feature/voting-core-migration
abluey Oct 16, 2023
4c0870b
Merge branch 'master' into feature/voting-core-migration
abluey Oct 16, 2023
c7560fb
chore: changelog
abluey Oct 16, 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
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ administrators
- Fix an issue where multiple emails can be passed to bypass domain-specific verification
### Insights
- Added new commands `insights` and `servers` under the insights cog
### Voting
- Add backend API

## [0.6.0] - 01-01-2023
- Upgraded to discord.py 2.1.0
Expand Down
2 changes: 1 addition & 1 deletion koala/cogs/verification/api.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,4 +85,4 @@ def setup(bot: Bot):
endpoint = VerifyEndpoint(bot)
endpoint.register(sub_app)
getattr(bot, "koala_web_app").add_subapp('/{}'.format(VERIFY_ENDPOINT), sub_app)
logger.info("Base API is ready.")
logger.info("Verify API is ready.")
9 changes: 7 additions & 2 deletions koala/cogs/voting/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,7 @@
from . import utils, db, log, models
from .cog import Voting, setup
from . import api
from . import cog
from .cog import Voting

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

import discord
from aiohttp import web
from discord.ext.commands import Bot
from koala.cogs.voting.db import VoteManager
from koala.cogs.voting.option import Option

from koala.rest.api import parse_request, build_response
# Own modules
from . import core
from .log import logger

# Constants
VOTING_ENDPOINT = 'vote'
CONFIG_ENDPOINT = 'config'
RESULTS_ENDOPINT = 'results'

class VotingEndpoint:
"""
The API endpoints for Voting
"""
def __init__(self, bot):
self._bot = bot

def register(self, app):
"""
Register the routes for the given application
todo: review aiohttp 'views' and see if they are a better idea
:param app: The aiohttp.web.Application (likely of the sub app)
:return: app
"""
app.add_routes([web.post('/{endpoint}'.format(endpoint=CONFIG_ENDPOINT), self.post_new_vote),
web.get('/{endpoint}'.format(endpoint=CONFIG_ENDPOINT), self.get_current_votes),
web.post('/{endpoint}'.format(endpoint=RESULTS_ENDOPINT), self.post_close_results),
web.get('/{endpoint}'.format(endpoint=RESULTS_ENDOPINT), self.get_results)])
return app


@parse_request(raw_response=True)
async def post_new_vote(self, title, author_id, guild_id, options: List[dict],
roles=None, chair_id=None, channel_id=None, end_time=None):
"""
Create a new vote.
:param title: The name of the vote
:param author_id: The author id of the vote
:param guild_id: The guild id of the vote
:param options: The options for the votes
:param roles: The target roles for the votes
:param chair_id: The chair id of the vote
:param channel_id: Channel id of the vote
:param end_time: The end time of the vote
:return:
"""
try:
core.start_vote(self._bot, title, author_id, guild_id)

if channel_id is not None:
await core.set_channel(self._bot, author_id, channel_id)

for item in options:
core.add_option(author_id, item["header"], item["body"])

if roles is not None:
for item in roles:
core.set_roles(self._bot, author_id, guild_id, item, "add")

if chair_id is not None:
await core.set_chair(self._bot, author_id, chair_id)

if end_time is not None:
core.set_end_time(author_id, end_time)

await core.send_vote(self._bot, author_id, guild_id)

except Exception as e:
logger.error(e)
raise web.HTTPUnprocessableEntity()

return build_response(CREATED, {'message': f'Vote {title} created'})


@parse_request(raw_response=True)
async def get_current_votes(self, author_id, guild_id):
"""
Gets list of open votes.
:param author_id: The author id of the vote
:param guild: The guild id of the vote
:return:
"""
try:
embed = core.current_votes(author_id, guild_id)

if embed.description:
body = embed.description
else:
body = embed.fields[0].value

return build_response(OK, {'embed_title': f'{embed.title}', 'embed_body': f'{body}'})

except Exception as e:
logger.error(e)
raise web.HTTPUnprocessableEntity()


@parse_request(raw_response=True)
async def post_close_results(self, author_id, title):
"""
Gets results and closes the vote.
:param author_id: The author id of the vote
:param title: The title of the vote
:return:
"""
try:
embed = await core.close(self._bot, author_id, title)
if embed.fields[0].name == "No votes yet!":
body = embed.fields[0].value
else:
body = ""
for item in embed.fields:
body += item.name + ", " + item.value + "\n"

return build_response(OK, {'embed_title': f'{embed.title}',
'embed_body': f'{body}'})

except Exception as e:
logger.error(e)
raise web.HTTPUnprocessableEntity()



@parse_request(raw_response=True)
async def get_results(self, author_id, title):
"""
Gets results, but does not close the vote.
:param author_id: The author id of the vote
:param title: The title of the vote
:return:
"""
try:
message = await core.results(self._bot, author_id, title)
if type(message) is discord.Embed:

if message.fields[0].name == "No votes yet!":
body = message.fields[0].value

else:
body = ""
for item in message.fields:
body += item.name + ", " + item.value + "\n"

return build_response(OK, {'embed_title': f'{message.title}',
'embed_body': f'{body}'})

else:
return build_response(BAD_REQUEST, {'message': message})

except ValueError as e:
raise web.HTTPUnprocessableEntity(reason="{}".format(e))

except Exception as e:
logger.error(e)
raise web.HTTPUnprocessableEntity()


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