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

Buttons #13

Merged
merged 12 commits into from
Feb 8, 2023
32 changes: 14 additions & 18 deletions cogs/error.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
# import logging
import logging

from discord import Message
from discord.ext import commands


# for finding errors with the code.
# TODO: SPRAV TO UŽ!!!!!

class Error(commands.Cog):
"""Basic class for catching errors and sending a message"""

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

# TODO: spravit logger

# self.logger = logging.getLogger('discord')
# self.logger.setLevel(logging.WARN)
# handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
# handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
# self.logger.addHandler(handler)
self.logger = logging.getLogger('discord')
self.logger.setLevel(logging.WARN)
handler = logging.FileHandler(filename='discord.log', encoding='utf-8', mode='w')
handler.setFormatter(logging.Formatter('%(asctime)s:%(levelname)s:%(name)s: %(message)s'))
self.logger.addHandler(handler)

@commands.Cog.listener()
async def on_command_error(self, ctx: commands.Context, error: commands.CommandError) -> Message | None:
async def on_command_error(self, ctx: commands.Context, error: commands.CommandError):
if isinstance(error, commands.ExpectedClosingQuoteError):
return await ctx.send(f"Pozor! Chybí tady: {error.close_quote} uvozovka!")

Expand All @@ -35,15 +35,11 @@ async def on_command_error(self, ctx: commands.Context, error: commands.CommandE
else:
# self.logger.critical(f"{ctx.message.id}, {ctx.message.content} | {error}")

return await ctx.send(
f"O této chybě ještě nevím a nebyla zaznamenána. Napiš The Xero#1273 o této chybě.\n"
f"Text chyby: `{error}`\n"
f"Číslo chyby: `{ctx.message.id}`"
)
print(error)

# @commands.Cog.listener()
# async def on_command(self, ctx: commands.Context):
# self.logger.info(f"{ctx.message.id} {ctx.message.content}")
@commands.Cog.listener()
async def on_command(self, ctx: commands.Context):
self.logger.info(f"{ctx.message.id} {ctx.message.content}")


async def setup(bot):
Expand Down
279 changes: 0 additions & 279 deletions cogs/event.py

This file was deleted.

43 changes: 43 additions & 0 deletions cogs/newpollstyle.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
from discord.ext import commands

from db_folder.sqldatabase import PollDatabase, VoteButtonDatabase
from poll_design.poll import Poll
from poll_design.poll_view import PollView
from ui.poll_embed import PollEmbed, PollEmbedBase


def error_handling(answer: tuple[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}!"


class PollCreate(commands.Cog):
def __init__(self, bot):
self.bot = bot

@commands.command()
async def poll(self, ctx: commands.Context, question: str, *answer: str):
message = await ctx.send(embed=PollEmbedBase("Dělám na tom, vydrž!"))
if error_handling(answer):
return await message.edit(embed=PollEmbedBase(error_handling(answer)))

poll = Poll(
message_id=message.id,
channel_id=message.channel.id,
question=question,
options=answer,
user_id=ctx.message.author.id
)

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)

await message.edit(embed=embed, view=view)


async def setup(bot):
await bot.add_cog(PollCreate(bot))
Loading