Skip to content

Commit

Permalink
Echo/Roll commands (Not required for bot configuration) cutted into s…
Browse files Browse the repository at this point in the history
…eparated plugin
  • Loading branch information
MJaroslav committed Jan 6, 2022
1 parent abce60c commit 02d7d3d
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 45 deletions.
1 change: 1 addition & 0 deletions locales/en_US.json
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@
"commands.text.locale.not_found": "That locale can not be found '{}'",
"commands.text.locale.list": "Locales: {}",
"commands.text.roll": "You rolled: {}!",
"commands.text.roll.bad_range": "<min> must be less than <max>",
"commands.text.weather.response.bad": "Error on weather service side! API response code: {}",
"commands.text.weather.bad_token": "Weather service API token not found! Please write to bot owner/developer",
"commands.text.weather.header": "{icon} Weather in {city}: {desc}",
Expand Down
47 changes: 47 additions & 0 deletions plugins/rine_base.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
import random

import vkrine
import vkrine.commands as cmds
import vkrine.exceptions as exceptions
from vkrine.modules import BotModule


class CommandEcho(cmds.Command):
def __init__(self, module):
super().__init__(module, "echo")

def run(self, event, bot, line, args):
vkrine.MessageBuilder(line).send(event)


class CommandRoll(cmds.Command):
def __init__(self, module):
super().__init__(module, "roll")

def run(self, event, bot, line, args):
if args:
s = len(args)
min_roll = 0
if s == 1:
max_roll = cmds.parse_int(args[0])
elif s == 2:
min_roll = cmds.parse_int(args[0])
max_roll = cmds.parse_int(args[1])
if min_roll >= max_roll:
raise exceptions.CommandSyntaxError("commands.text.roll.bad_range")
else:
raise exceptions.CommandWrongUsageException(None)
roll = random.randint(min_roll, max_roll)
vkrine.MessageBuilder().translated_text("commands.text.roll", roll).send(event)
else:
roll = random.randint(0, 100)
vkrine.MessageBuilder().translated_text("commands.text.roll", roll).send(event)


class BaseModule(BotModule):
def __init__(self, bot):
super().__init__("base", bot)

def commands(self):
return [CommandEcho(self),
CommandRoll(self)]
55 changes: 12 additions & 43 deletions vkrine/commands.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
import random
import re
import sys

Expand Down Expand Up @@ -120,24 +119,16 @@ def parse_mention(arg):
else:
uid = arg
users = vkrine.bot.get_vk().users.get(user_ids=uid)
l = len(users)
if l > 0:
if l > 1:
s = len(users)
if s > 0:
if s > 1:
return users
else:
return users[0]
else:
raise exceptions.UserNotFoundException(None, arg)


class CommandEcho(Command):
def __init__(self, module):
super().__init__(module, "echo")

def run(self, event, bot, line, args):
vkrine.MessageBuilder(line).send(event)


class CommandReload(Command):
def __init__(self, module):
super().__init__(module, "reload")
Expand All @@ -161,12 +152,12 @@ def __init__(self, module):
super().__init__(module, "locale")

def run(self, event, bot, line, args):
l = len(args)
if l == 0:
s = len(args)
if s == 0:
raise exceptions.CommandWrongUsageException(None)
if is_arg(event, "global", self.get_arg_key("global"), args[0]):
have_permission(event, self.get_arg_permission("global"))
if l == 2:
if s == 2:
locale = args[1]
if locale == "default":
bot.L10N.set_locale("@main", "en_US")
Expand All @@ -176,14 +167,14 @@ def run(self, event, bot, line, args):
done(event)
else:
vkrine.MessageBuilder().translated_text("commands.text.locale.not_found", locale).send(event)
elif l == 1:
elif s == 1:
locale = bot.L10N.get_locale_key("@main")
vkrine.MessageBuilder().translated_text("commands.text.locale.current", locale).send(event)
else:
raise exceptions.CommandWrongUsageException(None)
elif is_arg(event, "chat", self.get_arg_key("chat"), args[0]):
have_permission(event, self.get_arg_permission("chat"))
if l == 2:
if s == 2:
locale = args[1]
if locale == "default":
bot.L10N.reset_locale(str(event.peer_id))
Expand All @@ -193,14 +184,14 @@ def run(self, event, bot, line, args):
done(event)
else:
vkrine.MessageBuilder().translated_text("commands.text.locale.not_found", locale).send(event)
elif l == 1:
elif s == 1:
locale = bot.L10N.get_locale_key(event.peer_id)
vkrine.MessageBuilder().translated_text("commands.text.locale.current", locale).send(event)
else:
raise exceptions.CommandWrongUsageException(None)
elif is_arg(event, "personal", self.get_arg_key("personal"), args[0]):
have_permission(event, self.get_arg_permission("personal"))
if l == 2:
if s == 2:
locale = args[1]
if locale == "default":
bot.L10N.reset_locale(str(event.user_id))
Expand All @@ -210,13 +201,13 @@ def run(self, event, bot, line, args):
done(event)
else:
vkrine.MessageBuilder().translated_text("commands.text.locale.not_found", locale).send(event)
elif l == 1:
elif s == 1:
locale = bot.L10N.get_locale_key(event.user_id)
vkrine.MessageBuilder().translated_text("commands.text.locale.current", locale).send(event)
else:
raise exceptions.CommandWrongUsageException(None)
elif is_arg(event, "list", self.get_arg_key("list"), args[0]):
if l == 1:
if s == 1:
vkrine.MessageBuilder().translated_text("commands.text.locale.list",
", ".join(bot.L10N.locales())).send(event)
else:
Expand Down Expand Up @@ -279,25 +270,3 @@ def run(self, event, bot, line, args):
vkrine.MessageBuilder().translated_text("text.captcha.none").send(event)
else:
raise exceptions.CommandWrongUsageException(None)


class CommandRoll(Command):
def __init__(self, module):
super().__init__(module, "roll")

def run(self, event, bot, line, args):
if args:
l = len(args)
min_roll = 0
if l == 1:
max_roll = parse_int(args[0])
elif l == 2:
min_roll = parse_int(args[0])
max_roll = parse_int(args[1])
else:
raise exceptions.CommandWrongUsageException(None)
roll = random.randint(min_roll, max_roll)
vkrine.MessageBuilder().translated_text("commands.text.roll", roll).send(event)
else:
roll = random.randint(0, 100)
vkrine.MessageBuilder().translated_text("commands.text.roll", roll).send(event)
2 changes: 0 additions & 2 deletions vkrine/modules.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,10 +122,8 @@ def listeners(self):
def commands(self):
return [
CommandCaptcha(self),
CommandEcho(self),
CommandHelp(self),
CommandLocale(self),
CommandReload(self),
CommandStop(self),
CommandRoll(self),
]

0 comments on commit 02d7d3d

Please sign in to comment.