plugins/dice: Fix LMC (use origin id), reorganize things a bit to stop using tab.last_sent_message

Signed-off-by: Maxime Buquet's avatarMaxime “pep” Buquet <pep@bouah.net>
parent cf43a36b
Pipeline #3206 passed with stages
in 4 minutes and 59 seconds
...@@ -29,6 +29,7 @@ Configuration ...@@ -29,6 +29,7 @@ Configuration
""" """
import random import random
from typing import Optional
from poezio import tabs from poezio import tabs
from poezio.decorators import command_args_parser from poezio.decorators import command_args_parser
...@@ -40,17 +41,16 @@ DICE = '\u2680\u2681\u2682\u2683\u2684\u2685' ...@@ -40,17 +41,16 @@ DICE = '\u2680\u2681\u2682\u2683\u2684\u2685'
class DiceRoll: class DiceRoll:
__slots__ = [ __slots__ = [
'duration', 'total_duration', 'dice_number', 'msgtype', 'jid', 'duration', 'total_duration', 'dice_number', 'msgtype', 'jid',
'last_msgid', 'increments' 'msgid', 'increments'
] ]
def __init__(self, total_duration, dice_number, is_muc, jid, msgid, def __init__(self, total_duration, dice_number, msgtype, jid, msgid, increments):
increments):
self.duration = 0 self.duration = 0
self.total_duration = total_duration self.total_duration = total_duration
self.dice_number = dice_number self.dice_number = dice_number
self.msgtype = "groupchat" if is_muc else "chat" self.msgtype = msgtype
self.jid = jid self.jid = jid
self.last_msgid = msgid self.msgid = msgid
self.increments = increments self.increments = increments
def reroll(self): def reroll(self):
...@@ -60,8 +60,11 @@ class DiceRoll: ...@@ -60,8 +60,11 @@ class DiceRoll:
return self.duration >= self.total_duration return self.duration >= self.total_duration
def roll_dice(num_dice: int) -> str:
return ''.join(random.choice(DICE) for _ in range(num_dice))
class Plugin(BasePlugin): class Plugin(BasePlugin):
default_config = {"dice": {"refresh": 0.5, "default_duration": 5}} default_config = {"dice": {"refresh": 0.75, "default_duration": 7.5}}
def init(self): def init(self):
for tab_t in [tabs.MucTab, tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab]: for tab_t in [tabs.MucTab, tabs.DynamicConversationTab, tabs.StaticConversationTab, tabs.PrivateTab]:
...@@ -90,13 +93,17 @@ class Plugin(BasePlugin): ...@@ -90,13 +93,17 @@ class Plugin(BasePlugin):
self.core.command.help("roll") self.core.command.help("roll")
return return
firstroll = ''.join(random.choice(DICE) for _ in range(num_dice)) msgtype = 'groupchat' if isinstance(tab, tabs.MucTab) else 'chat'
tab.command_say(firstroll)
is_muctab = isinstance(tab, tabs.MucTab) message = self.core.xmpp.make_message(tab.jid)
msg_id = tab.last_sent_message["id"] message['type'] = msgtype
message['body'] = roll_dice(num_dice)
message.send()
increment = self.config.get('refresh') increment = self.config.get('refresh')
roll = DiceRoll(duration, num_dice, is_muctab, tab.jid, msg_id, msgid = message['id']
increment)
roll = DiceRoll(duration, num_dice, msgtype, tab.jid, msgid, increment)
event = self.api.create_delayed_event(increment, self.delayed_event, event = self.api.create_delayed_event(increment, self.delayed_event,
roll) roll)
self.api.add_timed_event(event) self.api.add_timed_event(event)
...@@ -107,11 +114,9 @@ class Plugin(BasePlugin): ...@@ -107,11 +114,9 @@ class Plugin(BasePlugin):
roll.reroll() roll.reroll()
message = self.core.xmpp.make_message(roll.jid) message = self.core.xmpp.make_message(roll.jid)
message["type"] = roll.msgtype message["type"] = roll.msgtype
message["body"] = ''.join( message["body"] = roll_dice(roll.dice_number)
random.choice(DICE) for _ in range(roll.dice_number)) message["replace"]["id"] = roll.msgid
message["replace"]["id"] = roll.last_msgid
message.send() message.send()
roll.last_msgid = message['id']
event = self.api.create_delayed_event(roll.increments, event = self.api.create_delayed_event(roll.increments,
self.delayed_event, roll) self.delayed_event, roll)
self.api.add_timed_event(event) self.api.add_timed_event(event)
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment