2 Commits d88ffd637e ... b85e421e86

Auteur SHA1 Message Date
  shadowrun b85e421e86 dice can be configured, combined bot starting and testing in one script il y a 3 ans
  shadowrun db3ae3e01f added comments and type hint to bot il y a 3 ans

+ 0 - 5
run_tests.sh

@@ -1,5 +0,0 @@
-#!/usr/bin/env bash
-# NOTE: this would fail if tests would be empty
-set -euxo pipefail
-shopt -s extglob
-python -m unittest tests/!(__pycache__)

+ 0 - 1
shadowrun_bot/__init__.py

@@ -1 +0,0 @@
-from .helpers import get_roll

+ 0 - 2
shadowrun_bot/bottoken.py

@@ -1,2 +0,0 @@
-# put your token here
-TOKEN = "TOKEN"

+ 2 - 0
src/__init__.py

@@ -0,0 +1,2 @@
+from .helpers import get_roll
+from .config import DEFAULT_AMOUNT, LIMIT_DICE, HITS, FAILS

+ 10 - 0
src/config.py

@@ -0,0 +1,10 @@
+# put your token here
+TOKEN = "TOKEN"
+
+
+DEFAULT_AMOUNT = 1 # default amount for /roll without arg
+LIMIT_DICE = 99 # how many dice can be rolled at the same time?
+
+DICE = [1,2,3,4,5,6] # list of possible results of a dice throw
+HITS = [5,6] # which numbers are hits?
+FAILS = [1] # which numbers are fails?

+ 2 - 3
shadowrun_bot/helpers.py

@@ -4,13 +4,12 @@ license: AGPL-3.0-only, see COPYING
 """
 import random
 import math
+from .config import HITS, FAILS, DICE, TOKEN
 
 def get_roll(amount: int, inject_numbers:list = None) -> dict:
     """gets a roll for amount dice, amount > 0
     inject_numbers can be used to inject numbers instead of random.randint
     return: dictionary with result, hits, fails, isFailure"""
-    HITS = [5,6] # which numbers are hits?
-    FAILS = [1]
 
     roll = {}
     roll["result"] = []
@@ -24,7 +23,7 @@ def get_roll(amount: int, inject_numbers:list = None) -> dict:
         if inject_numbers and len(inject_numbers) > (amount-count):
             num = inject_numbers[amount-count]
         else:
-            num = random.randint(1,6)
+            num = random.choice(DICE)
 
         roll["result"].append(num)
         if num in HITS:

+ 22 - 19
shadowrun_bot/shadowrun_bot.py

@@ -7,28 +7,30 @@ license: AGPL-3.0-only, see COPYING
 
 
 import logging
-from telegram.ext import Updater, CommandHandler
-import helpers
-from bottoken import TOKEN
+from telegram import Update
+from telegram.ext import Updater, CommandHandler, CallbackContext
+from . import config, helpers
 
 logging.basicConfig(format='%(asctime)s - %(name)s - %(levelname)s - %(message)s', level=logging.INFO)
 
 
-def start(update, context):
+def start(update: Update, context: CallbackContext) -> None:
+    """Send explanation what this bot does on /start"""
     reply = "Do '/roll <number> to roll up 100 dice.\nFor source code, do /license."
     context.bot.send_message(chat_id=update.effective_chat.id, text=reply)
 
-def license(update, context):
+def license(update: Update, context: CallbackContext) -> None:
+    """Send message with license and link to source code"""
     reply = "This bot is free software under the AGPLv3. \nSource: https://notabug.org/samtinel/telegram-bot-shadowrun"
     context.bot.send_message(chat_id=update.effective_chat.id, text=reply)
 
-def roll(update, context):
+def roll(update: Update, context: CallbackContext) -> None:
     """Rolls dice for /roll x
     x dice, if x not given, roll DEFAULT_AMOUNT
     returns pseudo-random numbers provided by python's random, meaning seed is system time
     NOTE: if cheating is something to worry about, a better source of randomness should be used"""
-    DEFAULT_AMOUNT = 1 # default amount for /roll without arg
-    LIMIT_DICE = 99 # how much dice can be rolled at the same time?
+    DEFAULT_AMOUNT = config.DEFAULT_AMOUNT
+    LIMIT_DICE = config.LIMIT_DICE
 
     # set amount
     if not context.args:
@@ -54,17 +56,18 @@ def roll(update, context):
         reply = f"Enter a valid integer between 0 and 100"
     context.bot.send_message(chat_id=update.effective_chat.id, text=reply)
 
-# set up bot
-updater = Updater(token=TOKEN, use_context=True)
+def main():
+    # set up bot
+    updater = Updater(token=config.TOKEN, use_context=True)
 
-# set up handlers
-handlers = []
-handlers.append(CommandHandler('start', start))
-handlers.append(CommandHandler('roll', roll))
-handlers.append(CommandHandler('license', license))
+    # set up handlers
+    handlers = []
+    handlers.append(CommandHandler('start', start))
+    handlers.append(CommandHandler('roll', roll))
+    handlers.append(CommandHandler('license', license))
 
-for handler in handlers:
-    updater.dispatcher.add_handler(handler)
+    for handler in handlers:
+        updater.dispatcher.add_handler(handler)
 
-# start polling
-updater.start_polling()
+    # start polling
+    updater.start_polling()

+ 19 - 0
telegram-bot-shadowrun

@@ -0,0 +1,19 @@
+#!/usr/bin/env python3
+
+import argparse
+import src.shadowrun_bot as bot
+import unittest
+
+parser = argparse.ArgumentParser(description="A shadowrun bot for telegram")
+parser.add_argument('mode', nargs="?", help="run or test")
+parsed = parser.parse_args()
+if parsed.mode == "run":
+    bot.main()
+elif parsed.mode == "test":
+    loader = unittest.TestLoader()
+    suite = loader.discover("tests")
+    runner = unittest.TextTestRunner()
+    runner.run(suite)
+else:
+    parser.print_help()
+

+ 0 - 0
tests/__init__.py


+ 0 - 0
tests/test_roll.py


Certains fichiers n'ont pas été affichés car il y a eu trop de fichiers modifiés dans ce diff