From 3953bcd6528dda7d39fc32cd7c68595e4d3b2385 Mon Sep 17 00:00:00 2001 From: Thromax Date: Tue, 14 Sep 2021 21:54:58 +0200 Subject: [PATCH 1/2] Added !roll command --- brainbot.ini.example | 3 +++ main.py | 62 ++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+) diff --git a/brainbot.ini.example b/brainbot.ini.example index 5e4c209..7baae8d 100644 --- a/brainbot.ini.example +++ b/brainbot.ini.example @@ -5,5 +5,8 @@ repeat=45 phon=45 poll=100 +[limits] +max_dice=99 + [misc] poll_reactions=zero;one;two;three;four;five;six;seven;eight;nine;keycap_ten \ No newline at end of file diff --git a/main.py b/main.py index c866e22..e0f9a2f 100644 --- a/main.py +++ b/main.py @@ -3,6 +3,7 @@ from datetime import datetime, timedelta from os import getenv, system from random import choice +import random from sys import executable from urllib.parse import quote @@ -37,6 +38,8 @@ config = ConfigParser() config.read("brainbot.ini") +max_dice_limit = config.getint("limits", "max_dice", fallback=99) + tell_me_to_cooldown = Cooldown(config.getint("cooldowns", "tell_me_to", fallback=200)) topic_cooldown = Cooldown(config.getint("cooldowns", "topic", fallback=100)) repeat_cooldown = Cooldown(config.getint("cooldowns", "repeat", fallback=45)) @@ -486,6 +489,65 @@ async def _on_chat(msg): "Check out [my wiki](https://github.com/brainbotdev/brainbot/wiki) to learn what commands I understand.", bot_chat, ) + # Roll a custom dice + elif msg.text.lower().startswith("!roll"): + """ + Command usage: !roll : Returns same number + !roll d : Rolls a single -faced dice + !roll d : Rolls the amount of -faced dice + + Maximum dice and faces are limited by setting limits->max_dice (default: 99) + """ + # Get potential arguments + inputs = [value.strip() for value in msg.text[6:].split("d")] + + # Remove any empty arguments + while "" in inputs: + inputs.remove("") + + if len(inputs) < 1 or len(inputs) > 2: + await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) + else: + try: + if any(int(i) > max_dice_limit for i in inputs): + str = "Dice values cannot be greater than the setting: `{0}`".format(max_dice_limit) + await send_message(str, bot_chat, ) + elif any(int(i) <= 0 for i in inputs): + await send_message("Dice values cannot be less than 1", bot_chat, ) + else: + roll = 0 + str = "" + if len(inputs)==1: + # Casting "!roll " will return same + if 'd' not in msg.text[6:]: + roll = int(inputs[0]) + str = "Rolled `{0}`: `{1}`".format(msg.text[6:], roll) + # Print rolled number + await send_message(str, bot_chat, ) + else: + # Calling "!roll d" will roll a -faced dice + if msg.text[6:].startswith("d"): + roll = random.randint(1,int(inputs[0])) + str = "Rolled `{0}`: `{1}`".format(msg.text[6:], roll) + # Print rolled number + await send_message(str, bot_chat, ) + else: + await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) + else: + # Calling "!roll d" will roll dices of faces each + str = "Rolled `{0}`: `".format(msg.text[6:]) + for i in range(0, int(inputs[0])): + current = random.randint(1, int(inputs[1])) + roll += current + str += "{0}".format(current) + if i != int(inputs[0])-1: + str +=" + " + str += " = {0}`".format(roll) + + # Print rolled number + await send_message(str, bot_chat, ) + except ValueError: + await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) # Pull the latest changes from GitHub elif msg.text.lower().startswith("!pull"): if user in bot_admins: From 91dd49ca67f91f265e676e8e029201e75f2e5505 Mon Sep 17 00:00:00 2001 From: Thromax Date: Tue, 14 Sep 2021 21:58:34 +0200 Subject: [PATCH 2/2] main.py formatted --- main.py | 74 ++++++++++++++++++++++++++++++++++++++++----------------- 1 file changed, 52 insertions(+), 22 deletions(-) diff --git a/main.py b/main.py index e0f9a2f..d4d4fee 100644 --- a/main.py +++ b/main.py @@ -1,9 +1,9 @@ +import random from asyncio import get_event_loop from configparser import ConfigParser from datetime import datetime, timedelta from os import getenv, system from random import choice -import random from sys import executable from urllib.parse import quote @@ -134,7 +134,7 @@ async def _on_chat(msg): console.log( f"[bold red]{user.get_username()} attempted to bypass the topic cooldown" ) - + # Get a conversation starter elif msg.text.lower().startswith("!topic"): console.log(f"{user.get_username()} used the !topic command") @@ -165,11 +165,11 @@ async def _on_chat(msg): await send_message(f"@{user.get_username()}: {to_do}", bot_chat) else: console.log("Cancelled due to cooldown") - + # Repeat after the user elif msg.text.lower().startswith("!repeat"): msg_text = msg.text[8:] - updatedmsg_text = msg_text.replace("!","\!") + updatedmsg_text = msg_text.replace("!", "\!") console.log(f"Repeating {user.get_username()}") if repeat_cooldown.run(username=user.get_username()): await send_message( @@ -494,7 +494,7 @@ async def _on_chat(msg): """ Command usage: !roll : Returns same number !roll d : Rolls a single -faced dice - !roll d : Rolls the amount of -faced dice + !roll d : Rolls the amount of -faced dice Maximum dice and faces are limited by setting limits->max_dice (default: 99) """ @@ -504,35 +504,59 @@ async def _on_chat(msg): # Remove any empty arguments while "" in inputs: inputs.remove("") - + if len(inputs) < 1 or len(inputs) > 2: - await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) + await send_message( + "Please enter a valid dice roll. You can try using: `d`", + bot_chat, + ) else: try: if any(int(i) > max_dice_limit for i in inputs): - str = "Dice values cannot be greater than the setting: `{0}`".format(max_dice_limit) - await send_message(str, bot_chat, ) + str = "Dice values cannot be greater than the setting: `{0}`".format( + max_dice_limit + ) + await send_message( + str, + bot_chat, + ) elif any(int(i) <= 0 for i in inputs): - await send_message("Dice values cannot be less than 1", bot_chat, ) + await send_message( + "Dice values cannot be less than 1", + bot_chat, + ) else: roll = 0 str = "" - if len(inputs)==1: + if len(inputs) == 1: # Casting "!roll " will return same - if 'd' not in msg.text[6:]: + if "d" not in msg.text[6:]: roll = int(inputs[0]) - str = "Rolled `{0}`: `{1}`".format(msg.text[6:], roll) + str = "Rolled `{0}`: `{1}`".format( + msg.text[6:], roll + ) # Print rolled number - await send_message(str, bot_chat, ) + await send_message( + str, + bot_chat, + ) else: # Calling "!roll d" will roll a -faced dice if msg.text[6:].startswith("d"): - roll = random.randint(1,int(inputs[0])) - str = "Rolled `{0}`: `{1}`".format(msg.text[6:], roll) + roll = random.randint(1, int(inputs[0])) + str = "Rolled `{0}`: `{1}`".format( + msg.text[6:], roll + ) # Print rolled number - await send_message(str, bot_chat, ) + await send_message( + str, + bot_chat, + ) else: - await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) + await send_message( + "Please enter a valid dice roll. You can try using: `d`", + bot_chat, + ) else: # Calling "!roll d" will roll dices of faces each str = "Rolled `{0}`: `".format(msg.text[6:]) @@ -540,14 +564,20 @@ async def _on_chat(msg): current = random.randint(1, int(inputs[1])) roll += current str += "{0}".format(current) - if i != int(inputs[0])-1: - str +=" + " + if i != int(inputs[0]) - 1: + str += " + " str += " = {0}`".format(roll) # Print rolled number - await send_message(str, bot_chat, ) + await send_message( + str, + bot_chat, + ) except ValueError: - await send_message("Please enter a valid dice roll. You can try using: `d`", bot_chat,) + await send_message( + "Please enter a valid dice roll. You can try using: `d`", + bot_chat, + ) # Pull the latest changes from GitHub elif msg.text.lower().startswith("!pull"): if user in bot_admins: