Rework: Rework memes and audio

This commit is contained in:
Marco Thomas
2021-05-24 14:10:31 +02:00
parent b506fba338
commit d58f901cda
7 changed files with 85 additions and 73 deletions

View File

@@ -2,20 +2,42 @@
Fetch pictures from subreddits
"""
import discord
from discord.ext import commands
import random
import discord
import praw
import random
from __main__ import REDDIT_CLIENT_ID
from __main__ import REDDIT_CLIENT_SECRET
from __main__ import REDDIT_CLIENT_USERAGENT
class Reddit(commands.Cog):
def __init__(self, bot):
self.bot = bot
@staticmethod
def find_post(posts):
rand_post = random.randint(1, 100)
# Make sure you're not sending a pinned post
for i in range(0, rand_post):
submission = next(post for post in posts if not post.stickied)
return submission
async def output(self, ctx, submission):
output = f"'{submission.title}' by {submission.author.name} - 🔼 {submission.score}"
if submission.over_18:
if not ctx.channel.is_nsfw():
await ctx.send("The post is marked as NSFW, but your channel isn't!")
return
else:
output = f"❗ NSFW ❗ " + output
await ctx.send(output)
await ctx.send(submission.url)
@commands.command(name="reddit")
async def reddit(self, ctx, sub: str, sorting="hot", time_filter="all"):
"""
@@ -23,12 +45,13 @@ class Reddit(commands.Cog):
"""
reddit = praw.Reddit(client_id=REDDIT_CLIENT_ID,
client_secret=REDDIT_CLIENT_SECRET,
user_agent=REDDIT_CLIENT_USERAGENT)
user_agent=REDDIT_CLIENT_USERAGENT,
check_for_async=False)
try:
reddit.subreddits.search_by_name(sub, exact=True)
except:
await ctx.send(f"Subreddit {sub} not found!")
await ctx.send(f"Subreddit '{sub}' not found!")
if sorting == "hot":
posts = reddit.subreddit(sub).hot()
@@ -38,21 +61,11 @@ class Reddit(commands.Cog):
posts = reddit.subreddit(sub).new()
else:
await ctx.send(f"Invalid Argument: {sorting}")
return
rand_post = random.randint(1, 100)
# Make sure you're not sending a pinned post
for i in range(0, rand_post):
submission = next(x for x in posts if not x.stickied)
if submission.over_18 and not ctx.channel.is_nsfw():
await ctx.send("The post is marked as NSFW, but your text channel isn't!")
else:
text = ""
if submission.over_18:
text += "❗ NSFW ❗"
text += f"'{submission.title}' by {submission.author.name} - 🔼 {submission.score}"
await ctx.send(text)
await ctx.send(submission.url)
await ctx.trigger_typing()
submission = self.find_post(posts)
await self.output(ctx, submission)
def setup(bot):