-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathreddit.py
101 lines (83 loc) · 3.87 KB
/
reddit.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
import config
import discord
from discord.ext import commands
from discord.ui import View
from discord import ButtonStyle
from util.reddit_scraper import get_subreddit_drama, get_aita
class Reddit(commands.Cog):
def __init__(self, bot):
self.bot = bot
reddit_group = discord.SlashCommandGroup('reddit', 'Commands which retrieve top posts from subreddits', guild_ids=[config.GUILD_ID])
@reddit_group.command(description='What\'s trending on r/AmITheAsshole')
async def aita(self, ctx: discord.ApplicationContext):
try:
await ctx.defer()
post = await get_aita()
except Exception as e:
return await ctx.respond('Unable to find an asshole. Try the mirror.', ephemeral=True)
# must truncate embed descriptions to 4096 chars
if len(post['desc']) > 4096:
post['desc'] = post['desc'][:4096-3] + '...'
embed = discord.Embed(
title=post['title'],
url=f"https://reddit.com{post['link']}",
description=post['desc']
)
embed.set_footer(text=f"{post['upvotes']} upvotes")
embed.set_author(name=post['author'])
await ctx.respond(embed=embed, view=AitaView(post))
@reddit_group.command(description='What\'s trending on r/SubredditDrama')
async def subreddit_drama(self, ctx: discord.ApplicationContext):
try:
await ctx.defer()
post = await get_subreddit_drama()
except Exception as e:
return await ctx.respond('Unable to find a spicy story :/', ephemeral=True)
# must truncate embed descriptions to 4096 chars
if len(post['desc']) > 4096:
post['desc'] = post['desc'][:4096-3] + '...'
embed = discord.Embed(
title=post['title'],
url=f"https://reddit.com{post['link']}",
description=post['desc']
)
embed.set_footer(text=f"{post['upvotes']} upvotes")
embed.set_author(name=post['author'])
await ctx.respond(embed=embed)
class AitaView(View):
def __init__(self, post):
super().__init__()
self.post = post
@discord.ui.button(label='YTA', style=ButtonStyle.red)
async def yta_callback(self, button, interaction):
await interaction.response.send_message(
content=self.get_response(button.label), ephemeral=True)
@discord.ui.button(label='NTA', style=ButtonStyle.green)
async def nta_callback(self, button, interaction):
await interaction.response.send_message(
content=self.get_response(button.label), ephemeral=True)
@discord.ui.button(label='ESH', style=ButtonStyle.blurple)
async def esh_callback(self, button, interaction):
await interaction.response.send_message(
content=self.get_response(button.label), ephemeral=True)
@discord.ui.button(label='NAH', style=ButtonStyle.gray)
async def nah_callback(self, button, interaction):
await interaction.response.send_message(
content=self.get_response(button.label), ephemeral=True)
@discord.ui.button(label='INFO', style=ButtonStyle.grey)
async def info_callback(self, button, interaction):
await interaction.response.send_message(
content=self.get_response(button.label), ephemeral=True)
# must truncate it to 2000 characters
def get_response(self, guess):
if self.post['sdec'] == "None":
content = f"Unable to locate a top comment with a valid decision. {self.post['decision']}"
elif guess == self.post['sdec']:
content = f"{guess} was **correct**! {self.post['decision']}"
else:
content = f"{guess} was **incorrect**! {self.post['decision']}"
if len(content) > 2000:
content = content[:2000-3] + '...'
return content
def setup(bot):
bot.add_cog(Reddit(bot))