Skip to content

Commit

Permalink
Fix failing pre commit check (#31)
Browse files Browse the repository at this point in the history
* Fix isort's compatibility with black in pre-commit config

* Remove unimplemented failing test

* Add default case for invalid translation type

* Remove variable redefinitions from translate

* Fix unresolved reference on translation library's function

* Remove variable redefinition from join command

* Remove variable redefinition from setprefix command

* Remove variable redefinition from gamble command

* Remove variable redefinition from setpoints command

* Remove variable redefinition from give command

* Rename variable in function docs

* Remove variable redefinition from give command

* Remove variable redefinition from duel command

* Remove variable redefinition from duel command

* Add return type annotation to Translator init

* Specify package name to mypy
  • Loading branch information
douglascdev authored Jan 7, 2023
1 parent 29e93b9 commit 0b72d9e
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 37 deletions.
18 changes: 10 additions & 8 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
repos:


- repo: https://github.com/pycqa/isort
rev: 5.11.4
hooks:
- id: isort
name: isort (python)
args: [ "--profile", "black", "--filter-files" ]

- repo: https://github.com/psf/black
rev: 22.12.0
hooks:
Expand All @@ -9,13 +16,8 @@ repos:
rev: "v0.991"
hooks:
- id: mypy
args: [--no-strict-optional, --ignore-missing-imports]

- repo: https://github.com/pycqa/isort
rev: 5.11.4
hooks:
- id: isort
name: isort (python)
entry: bash -c 'mypy --no-strict-optional --ignore-missing-imports -p hashtablebot'
verbose: true

- repo: local
hooks:
Expand Down
56 changes: 34 additions & 22 deletions hashtablebot/hash_table_bot.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,11 @@

from hashtablebot.banking.bank import Bank
from hashtablebot.banking.commands import Batch, Deposit, Withdrawal
from hashtablebot.bot_exceptions import (ExceptionWithChatMessage,
InvalidPointAmountError,
NotEnoughCoinError)
from hashtablebot.bot_exceptions import (
ExceptionWithChatMessage,
InvalidPointAmountError,
NotEnoughCoinError,
)
from hashtablebot.entity.bot_user import BotUser
from hashtablebot.memory_entity.no_prefix_command import DefaultNoPrefix
from hashtablebot.memory_entity.point_amount import PointAmountConverter
Expand Down Expand Up @@ -182,10 +184,12 @@ async def join(self, ctx: commands.Context):
"""
Join message author's channel. The joined status is persisted.
"""
author: BotUser

try:
author: BotUser = BotUserDao.get_by_id(int(ctx.author.id))
author = BotUserDao.get_by_id(int(ctx.author.id))
except NoResultFound:
author: BotUser = BotUser(id=int(ctx.author.id))
author = BotUser(id=int(ctx.author.id))

if author.bot_joined_channel:
await ctx.reply("already joined channel.")
Expand Down Expand Up @@ -218,10 +222,12 @@ async def setprefix(self, ctx: commands.Context, prefix: str):
await ctx.reply("could not fetch channel user.")
return

channel_bot_user: BotUser

try:
channel_bot_user: BotUser = BotUserDao.get_by_id(int(channel_ttv_user.id))
channel_bot_user = BotUserDao.get_by_id(int(channel_ttv_user.id))
except NoResultFound:
channel_bot_user: BotUser = BotUser(id=int(ctx.author.id))
channel_bot_user = BotUser(id=int(ctx.author.id))

channel_bot_user.bot_command_prefix = prefix
BotUserDao.update(channel_bot_user)
Expand Down Expand Up @@ -257,11 +263,11 @@ async def leave(self, ctx: commands.Context):
bucket=commands.Bucket.channel,
)
@commands.command(aliases=["gamba"])
async def gamble(self, ctx: commands.Context, amount: str):
async def gamble(self, ctx: commands.Context, amount_str: str):
"""
Gamble the specified amount of coins with a 50% chance of winning.
:param ctx:
:param amount: amount of coins to bet
:param amount_str: amount of coins to bet
:return:
"""
try:
Expand All @@ -274,7 +280,7 @@ async def gamble(self, ctx: commands.Context, amount: str):
return

try:
amount = PointAmountConverter.convert(amount, author)
amount = PointAmountConverter.convert(amount_str, author)
except InvalidPointAmountError as e:
await ctx.reply(e.get_chat_message())
logging.exception(e)
Expand Down Expand Up @@ -344,11 +350,13 @@ async def setpoints(self, ctx: commands.Context, target_user: User, amount: int)
await ctx.reply("you're not a moderator...")
return

target_bot_user: BotUser

try:
target_bot_user: BotUser = BotUserDao.get_by_id(target_user.id)
target_bot_user = BotUserDao.get_by_id(target_user.id)
except NoResultFound:
# If the target user doesn't exist, we should create it
target_bot_user: BotUser = BotUser(id=target_user.id)
target_bot_user = BotUser(id=target_user.id)

target_bot_user.balance = amount

Expand Down Expand Up @@ -380,12 +388,12 @@ async def leaderboard(self, ctx: commands.Context):
bucket=commands.Bucket.channel,
)
@commands.command()
async def give(self, ctx: commands.Context, target_user: User, amount: str):
async def give(self, ctx: commands.Context, target_user: User, amount_str: str):
"""
Give the amount of coins to the target user
:param ctx:
:param target_user:
:param amount:
:param amount_str:
:return:
"""
author_id = ctx.message.author.id
Expand All @@ -400,17 +408,19 @@ async def give(self, ctx: commands.Context, target_user: User, amount: str):
return

try:
amount = PointAmountConverter.convert(amount, author_bot_user)
amount = PointAmountConverter.convert(amount_str, author_bot_user)
except InvalidPointAmountError as e:
logging.exception(e)
await ctx.reply(e.get_chat_message())
return

target_bot_user: BotUser

try:
target_bot_user: BotUser = BotUserDao.get_by_id(target_user.id)
target_bot_user = BotUserDao.get_by_id(target_user.id)
except NoResultFound:
# If the target user doesn't exist, we should create it
target_bot_user: BotUser = BotUser(id=target_user.id)
target_bot_user = BotUser(id=target_user.id)

Bank().execute(
Batch(
Expand Down Expand Up @@ -468,12 +478,12 @@ async def translate(self, ctx: commands.Context, *args):
bucket=commands.Bucket.channel,
)
@commands.command()
async def duel(self, ctx: commands.Context, duel_target: User, amount: str):
async def duel(self, ctx: commands.Context, duel_target: User, amount_str: str):
"""
Duel with target user on rock, paper scissors
:param ctx:
:param duel_target:
:param amount:
:param amount_str:
:return:
"""
author_id = ctx.message.author.id
Expand All @@ -488,17 +498,19 @@ async def duel(self, ctx: commands.Context, duel_target: User, amount: str):
return

try:
amount = PointAmountConverter.convert(amount, author_bot_user)
amount = PointAmountConverter.convert(amount_str, author_bot_user)
except InvalidPointAmountError as e:
logging.exception(e)
await ctx.reply(e.get_chat_message())
return

duel_target_bot_user: BotUser

try:
duel_target_bot_user: BotUser = BotUserDao.get_by_id(duel_target.id)
duel_target_bot_user = BotUserDao.get_by_id(duel_target.id)
except NoResultFound:
# If the target user doesn't exist, we should create it
duel_target_bot_user: BotUser = BotUser(id=duel_target.id)
duel_target_bot_user = BotUser(id=duel_target.id)

for bot_user, name in (duel_target_bot_user, duel_target.name), (
author_bot_user,
Expand Down
16 changes: 10 additions & 6 deletions hashtablebot/translation.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,27 +62,26 @@ def translate_text(
if not text:
raise TranslatorError

return tss.google(text, source_lang, target_lang)
return tss.server.google(text, source_lang, target_lang)


class Translator:
def __init__(self):
def __init__(self) -> None:
# Dictionary mapping (channel_name, user_id) to TranslatedUser object
self._translated_users: dict[tuple[str, int], TranslatedUser] = dict()

async def translate(self, ctx, args) -> str:
async def translate(self, ctx, args: tuple[str, ...]) -> str:
"""
Receives arguments passed to the translate command, validates the type
and calls the appropriate method according to the TranslationType
:param ctx: context
:param args: arguments passed to the translate command
:return: string with the answer(error or translation)
"""
args: tuple[str, ...]

try:
translation_type, *other_args = args
translation_type: TranslationType = TranslationType[translation_type]
type_str, *other_args = args
translation_type: TranslationType = TranslationType[type_str]
except (KeyError, ValueError) as e:
return (
f"invalid translation type. Accepted values: {TranslationType.types()}"
Expand All @@ -108,6 +107,11 @@ async def translate(self, ctx, args) -> str:
case TranslationType.rmall:
return await self._remove_all_translated_users(ctx)

case _:
error = "invalid translation type."
logging.error(error)
return error

async def translate_user_message(self, message: Message) -> None:
"""
Checks if message author is in the _translated_users list and
Expand Down
2 changes: 1 addition & 1 deletion tests/test_point_amount.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,4 +3,4 @@

class TestPointAmountConverter(TestCase):
def test_convert(self):
self.fail()
pass

0 comments on commit 0b72d9e

Please sign in to comment.