From 1462089c42b9f626736efce60f83952fff0985a1 Mon Sep 17 00:00:00 2001 From: Angerstoner Date: Mon, 11 Feb 2019 12:53:11 +0100 Subject: [PATCH] added reddit img command --- misc/commandlist | 4 ++++ reddit.py | 24 +++++++++++++++++------- schneiderbot.py | 7 ++++--- 3 files changed, 25 insertions(+), 10 deletions(-) diff --git a/misc/commandlist b/misc/commandlist index 36b784d..a4a655c 100644 --- a/misc/commandlist +++ b/misc/commandlist @@ -1,8 +1,12 @@ mensa - [TAG/full/veggy/fleisch/fisch] essen in der mensa, filter möglich cat - cat pic dog - dog pic +wrongdog - post from /r/whatswrongwithyourdog +hamster - hamster pic catdog - catdog pic codinglove - random gif from the coding love +reddit - [sub] random post from given subreddit +reddit_img - [sub] random image post from given subreddit manta - boah ey, geile Karre! magie - Magie ist Physik durch wollen kill - kill me pls diff --git a/reddit.py b/reddit.py index 411c5bd..556213f 100644 --- a/reddit.py +++ b/reddit.py @@ -33,13 +33,7 @@ def wrong_dog(bot, update): def hamster(bot, update): - reddit_instance = get_reddit_instance() - - sub = reddit_instance.subreddit('hamsters') - post = sub.random() - while "image" not in get_post_type(post.url): - post = sub.random() - bot.send_photo(chat_id=update.message.chat_id, photo=post.url) + reddit_img(bot, update, ['hamsters']) def reddit(bot, update, args): @@ -51,3 +45,19 @@ def reddit(bot, update, args): post = reddit_instance.subreddit(sub).random() update.message.reply_text(post.url) + + +def reddit_img(bot, update, args): + print(args) + + if not args: + update.message.reply_text("Geile, sag sub") + return + desired_sub = args[0] + reddit_instance = get_reddit_instance() + + sub = reddit_instance.subreddit(desired_sub) + post = sub.random() + while "image" not in get_post_type(post.url): + post = sub.random() + bot.send_photo(chat_id=update.message.chat_id, photo=post.url) diff --git a/schneiderbot.py b/schneiderbot.py index 77007e5..8f6896c 100755 --- a/schneiderbot.py +++ b/schneiderbot.py @@ -10,7 +10,7 @@ from telegram import ParseMode from telegram.ext import Updater, CommandHandler from coding_love import send_coding_love_gif -from reddit import wrong_dog, hamster, reddit +from reddit import wrong_dog, hamster, reddit, reddit_img from db import Db from mensa import mensa @@ -205,13 +205,14 @@ def init_commands(dp): dp.add_handler(CommandHandler("wrongdog", wrong_dog)) dp.add_handler(CommandHandler("hamster", hamster)) dp.add_handler(CommandHandler("reddit", reddit, pass_args=True)) + dp.add_handler(CommandHandler("reddit_img", reddit_img, pass_args=True)) def main(): """Start the bot.""" # Create the EventHandler and pass it your bot's token. - token = open("token").read() - # token = open("test_token").read() + # token = open("token").read() + token = open("test_token").read() updater = Updater(token.strip()) # Get the dispatcher to register handlers