Implemented #28
This commit is contained in:
parent
7d214523c6
commit
80391a61ac
|
@ -44,8 +44,8 @@ ImaginaryFriend can:
|
|||
|
||||
* `/ping`,
|
||||
* `/get_stats`: get information on how many pairs are known by ImaginaryFriend,
|
||||
* `/set_chance`: set the probability that ImaginaryFriend would reply to a random message (must be in range 1-50, default: 5),
|
||||
* `/get_chance`: get current probability that ImaginaryFriend would reply to a message,
|
||||
* `/chance n`: set the probability that ImaginaryFriend would reply to a random message (must be in range 1-50, default: 5),
|
||||
* `/сhance`: get current probability that ImaginaryFriend would reply to a message,
|
||||
* `/meow`, `/woof`, `/borscht`, `/boobs`, `/butts`: make ImaginaryFriend send a picture.
|
||||
|
||||
## Installation and Setup
|
||||
|
|
|
@ -4,9 +4,7 @@ from src.handler.commands.help import Help
|
|||
from src.handler.commands.ping import Ping
|
||||
from src.handler.commands.get_stats import GetStats
|
||||
from src.handler.commands.moderate import Moderate
|
||||
from src.handler.commands.get_chance import GetChance
|
||||
from src.handler.commands.set_chance import SetChance
|
||||
|
||||
from src.handler.commands.chance import Chance
|
||||
|
||||
commands = {}
|
||||
for clazz in Base.__subclasses__():
|
||||
|
|
|
@ -0,0 +1,22 @@
|
|||
from .base import Base
|
||||
from src.config import chance_manager
|
||||
|
||||
|
||||
class Chance(Base):
|
||||
name = 'chance'
|
||||
chance_manager = chance_manager
|
||||
|
||||
@staticmethod
|
||||
def execute(bot, command):
|
||||
try:
|
||||
new_chance = int(command.args[0])
|
||||
|
||||
if new_chance < 1 or new_chance > 50:
|
||||
return Chance.reply(bot, command, 'Usage: /chance 1-50.')
|
||||
|
||||
old_chance = Chance.chance_manager.set_chance(chat_id=command.chat_id, new_chance=new_chance)
|
||||
|
||||
Chance.reply(bot, command, 'Change chance from {} to {}'.format(old_chance, new_chance))
|
||||
except (IndexError, ValueError):
|
||||
Chance.reply(bot, command, 'Current chance: {}'
|
||||
.format(Chance.chance_manager.get_chance(command.chat_id)))
|
|
@ -1,12 +0,0 @@
|
|||
from .base import Base
|
||||
from src.config import chance_manager
|
||||
|
||||
|
||||
class GetChance(Base):
|
||||
name = 'get_chance'
|
||||
chance_manager = chance_manager
|
||||
|
||||
@staticmethod
|
||||
def execute(bot, command):
|
||||
GetChance.reply(bot, command, 'Current chance: {}'
|
||||
.format(GetChance.chance_manager.get_chance(command.chat_id)))
|
|
@ -1,21 +0,0 @@
|
|||
from .base import Base
|
||||
from src.config import chance_manager
|
||||
|
||||
|
||||
class SetChance(Base):
|
||||
name = 'set_chance'
|
||||
chance_manager = chance_manager
|
||||
|
||||
@staticmethod
|
||||
def execute(bot, command):
|
||||
try:
|
||||
chance = int(command.args[0])
|
||||
|
||||
if chance < 1 or chance > 50:
|
||||
raise ValueError
|
||||
|
||||
SetChance.chance_manager.set_chance(chat_id=command.chat_id, chance=chance)
|
||||
|
||||
SetChance.reply(bot, command, 'Set chance to: {}'.format(chance))
|
||||
except (IndexError, ValueError):
|
||||
SetChance.reply(bot, command, 'Usage: /set_chance 1-50.')
|
|
@ -4,7 +4,7 @@ from src.config import config, redis
|
|||
class ChanceManager:
|
||||
def __init__(self):
|
||||
self.redis = redis
|
||||
self.key = "chance:{}"
|
||||
self.key = 'chance:{}'
|
||||
self.default_chance = config.getint('bot', 'default_chance')
|
||||
|
||||
def get_chance(self, chat_id):
|
||||
|
@ -12,5 +12,7 @@ class ChanceManager:
|
|||
|
||||
return int(result.decode("utf-8")) if result is not None else self.default_chance
|
||||
|
||||
def set_chance(self, chat_id, chance):
|
||||
self.redis.instance().set(self.key.format(chat_id), chance)
|
||||
def set_chance(self, chat_id, new_chance):
|
||||
old_chance = self.redis.instance().getset(self.key.format(chat_id), new_chance)
|
||||
|
||||
return int(old_chance.decode("utf-8")) if old_chance is not None else self.default_chance
|
||||
|
|
Loading…
Reference in New Issue