-
Notifications
You must be signed in to change notification settings - Fork 3
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Nové funkcionality, novější věci! (#30)
Hlavní pull-request na to, aby se nakonec všechny věci zrecenzovaly a byly na jednom místě, než tyto věci dáme na produkci. Co je nového? - #16 - #21 - #23 - #24 - Optimalizace hlasování - #29 - Vytvořena CI pipeline Co dál dělat? - Počkat, až bude PR #24 hotový, teprve pak můžeme kód otestovat a mergnout ho. Kdo si zaslouží dík? - @Martian-0007 Díky moc! :)
- Loading branch information
Showing
26 changed files
with
525 additions
and
209 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,25 @@ | ||
on: push | ||
|
||
jobs: | ||
build: | ||
runs-on: ubuntu-latest | ||
|
||
steps: | ||
- uses: actions/checkout@v1 | ||
|
||
- name: Set up CPython | ||
uses: actions/setup-python@v4 | ||
|
||
- name: Install dependencies | ||
id: install-deps | ||
run: | | ||
python -m pip install --upgrade pip setuptools wheel ruff | ||
- name: Black format | ||
uses: psf/black@stable | ||
|
||
- name: Ruff Check | ||
run: ruff check . | ||
|
||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -5,3 +5,5 @@ __pycache__ | |
/discord.log | ||
.DS_STORE | ||
.vscode | ||
.ruff_cache | ||
.pytest_cache |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,73 +1,97 @@ | ||
import re | ||
import asyncio | ||
import datetime | ||
|
||
import discord | ||
from discord import app_commands | ||
from discord.ext import commands | ||
from discord.app_commands import Transform | ||
from discord.ext import commands, tasks | ||
from loguru import logger | ||
|
||
from src.db_folder.databases import PollDatabase, VoteButtonDatabase | ||
from src.jachym import Jachym | ||
from src.ui.embeds import PollEmbed, PollEmbedBase | ||
from src.ui.poll import Poll | ||
from src.ui.poll_view import PollView | ||
|
||
|
||
def error_handling(answer: list[str]) -> str: | ||
if len(answer) > Poll.MAX_OPTIONS: | ||
return f"Zadal jsi příliš mnoho odpovědí, můžeš maximálně {Poll.MAX_OPTIONS}!" | ||
elif len(answer) < Poll.MIN_OPTIONS: | ||
return f"Zadal jsi příliš málo odpovědí, můžeš alespoň {Poll.MIN_OPTIONS}!" | ||
from src.ui.transformers import DatetimeTransformer, OptionsTransformer | ||
|
||
|
||
class PollCreate(commands.Cog): | ||
POLL_PARAMETERS = { | ||
"name": "anketa", | ||
"description": "Anketa pro hlasování. Jsou vidět všichni hlasovatelé.", | ||
"question": "Otázka, na kterou potřebuješ vědět odpověď", | ||
"answer": 'Odpovědi, rozděluješ odpovědi uvozovkou ("), maximálně pouze 10 možností', | ||
"help": """ | ||
Jednoduchá anketa, která obsahuje otázku a odpovědi. Povoleno je 10 možností. | ||
""", | ||
} | ||
|
||
# Bugfix for iPhone users who have different font for aposthrofe | ||
REGEX_PATTERN = ['"', "”", "“", "„"] | ||
|
||
def __init__(self, bot: Jachym): | ||
self.bot = bot | ||
|
||
@app_commands.command(name="anketa", description="Anketa pro hlasování. Jsou vidět všichni hlasovatelé.") | ||
@app_commands.rename(question="otázka", answer="odpovědi") | ||
@app_commands.command( | ||
name="anketa", | ||
description="Anketa pro hlasování. Jsou vidět všichni hlasovatelé.", | ||
) | ||
@app_commands.rename( | ||
question="otázka", | ||
answer="odpovědi", | ||
date_time="datum", | ||
) | ||
@app_commands.describe( | ||
question="Otázka, kterou chceš položit.", | ||
answer='Odpovědi, rozděluješ odpovědi uvozovkou ("), maximálně pouze 10 možností', | ||
date_time="Den, na který anketa skončí.", | ||
) | ||
async def pool(self, interaction: discord.Interaction, question: str, answer: str) -> discord.Message: | ||
await interaction.response.send_message(embed=PollEmbedBase("Dělám na tom, vydrž!")) | ||
async def pool( | ||
self, | ||
interaction: discord.Interaction, | ||
question: str, | ||
answer: Transform[list[str, ...], OptionsTransformer], | ||
date_time: Transform[datetime.datetime, DatetimeTransformer] | None, | ||
): | ||
await interaction.response.send_message(embed=PollEmbedBase("Nahrávám anketu...")) | ||
message = await interaction.original_response() | ||
|
||
answers = re.split("|".join(self.REGEX_PATTERN), answer) | ||
if error_handling(answers): | ||
return await message.edit(embed=PollEmbedBase(error_handling(answers))) | ||
|
||
poll = Poll( | ||
message_id=message.id, | ||
channel_id=message.channel.id, | ||
question=question, | ||
options=answers, | ||
options=answer, | ||
user_id=interaction.user.id, | ||
date_created=date_time, | ||
) | ||
|
||
embed = PollEmbed(poll) | ||
view = PollView(poll, embed, db_poll=self.bot.pool) | ||
await PollDatabase(self.bot.pool).add(poll) | ||
await VoteButtonDatabase(self.bot.pool).add_options(poll) | ||
|
||
self.bot.active_discord_polls.add(poll) | ||
await self.bot.set_presence() | ||
logger.info(f"Successfully added Pool - {message.id}") | ||
return await message.edit(embed=embed, view=view) | ||
|
||
await message.edit(embed=embed, view=view) | ||
self.bot.active_discord_polls.add((poll, message)) | ||
|
||
|
||
class PollTaskLoops(commands.Cog): | ||
def __init__(self, bot: Jachym): | ||
self.bot = bot | ||
self.send_completed_pool.start() | ||
|
||
@tasks.loop(seconds=5) | ||
async def send_completed_pool(self): | ||
for poll, message in self.bot.active_discord_polls.copy(): | ||
if poll.created_at is None or datetime.datetime.now() < poll.created_at: | ||
continue | ||
|
||
embed = message.embeds[0] | ||
embed_copy = embed.copy() | ||
embed_copy.title = f"{embed.title[0]} [UZAVŘENO] {embed.title[1:]}" | ||
embed_copy.remove_field(len(embed_copy.fields) - 1) | ||
|
||
channel = self.bot.get_channel(poll.channel_id) | ||
await channel.send(embed=embed_copy) | ||
|
||
asyncio.create_task(PollDatabase(self.bot.pool).remove(poll.message_id)) | ||
asyncio.create_task(message.delete()) | ||
self.bot.active_discord_polls.remove((poll, message)) | ||
|
||
@send_completed_pool.before_loop | ||
async def prepare_loop(self): | ||
await self.bot.wait_until_ready() | ||
|
||
|
||
async def setup(bot): | ||
await bot.add_cog(PollCreate(bot)) | ||
await bot.add_cog(PollTaskLoops(bot)) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,23 @@ | ||
[tool.ruff] | ||
|
||
select = [ | ||
"E", # pycodestyle | ||
"F", # pyflakes | ||
"UP", # pyupgrade, | ||
"I", # isort | ||
"UP", # pyupgrade | ||
"ASYNC", | ||
"BLE", # Blind Exception | ||
"T20", # Found a print! | ||
"RET", # Unnecessary return | ||
"SIM", # Simplify | ||
] | ||
exclude = [ | ||
"tests", | ||
] | ||
|
||
line-length = 120 | ||
|
||
[tool.black] | ||
|
||
line-length = 120 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,3 @@ | ||
|
||
<h1 align=center> | ||
<img src="fotky/Jáchym.png" alt="Logo Jáchyma"> | ||
<br> | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,4 +2,5 @@ python-dotenv==0.17.1 | |
|
||
aiomysql>=0.0.22 | ||
pytest>=7.3.1 | ||
loguru>=0.7.0 | ||
loguru>=0.7.0 | ||
dateparser>=1.1.8 |
Empty file.
Oops, something went wrong.