forked from Cog-Creators/Red-DiscordBot
-
Notifications
You must be signed in to change notification settings - Fork 20
Expand file tree
/
Copy pathspoilers.py
More file actions
169 lines (153 loc) · 6.36 KB
/
Copy pathspoilers.py
File metadata and controls
169 lines (153 loc) · 6.36 KB
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
"""Spoilers cog
Filters out messages that start with a certain prefix, and store them for
later retrieval.
"""
from datetime import datetime, timedelta
import logging
import json
import os
import re
import discord
from discord.ext import commands
from redbot.core import Config, checks, commands, data_manager
from redbot.core.commands.context import Context
from redbot.core.bot import Red
# Global variables
KEY_MESSAGE = "message"
KEY_AUTHOR_ID = "authorid"
KEY_AUTHOR_NAME = "author"
KEY_TIMESTAMP = "timestamp"
KEY_EMBED = "embed"
LOGGER = None
PREFIX = "spoiler"
COOLDOWN = 60
BASE = {"messages": {}}
class Spoilers(commands.Cog):
"""Store messages for later retrieval."""
# Class constructor
def __init__(self, bot: Red):
self.bot = bot
self.config = Config.get_conf(self, identifier=5842647, force_registration=True)
# Register default (empty) settings.
self.config.register_global(**BASE)
self.onCooldown = {}
# Initialize logger, and save to cog folder.
saveFolder = data_manager.cog_data_path(cog_instance=self)
self.logger = logging.getLogger("red.luicogs.Spoilers")
if not self.logger.handlers:
logPath = os.path.join(saveFolder, "info.log")
handler = logging.FileHandler(filename=logPath, encoding="utf-8", mode="a")
handler.setFormatter(
logging.Formatter("%(asctime)s %(message)s", datefmt="[%d/%m/%Y %H:%M:%S]")
)
self.logger.addHandler(handler)
@commands.command(name="spoiler")
async def spoiler(self, ctx: Context, *, msg: str):
"""Create a message spoiler."""
wordFilter = self.bot.get_cog("WordFilter")
if not wordFilter:
await ctx.send(
"This cog requires the word filter cog to be loaded. Please load "
"the cog and try again"
)
return
if await wordFilter.containsFilterableWords(ctx.message):
await ctx.send(
"You have filtered words in your spoiler! Please check it and try again!"
)
return
try:
store = {}
store[KEY_MESSAGE] = msg
store[KEY_AUTHOR_ID] = str(ctx.message.author.id)
store[KEY_AUTHOR_NAME] = "{0.name}#{0.discriminator}".format(ctx.message.author)
store[KEY_TIMESTAMP] = ctx.message.created_at.strftime("%s")
if ctx.message.embeds:
data = discord.Embed.from_data(ctx.message.embeds[0])
if data.type == "image":
store[KEY_EMBED] = data.url
else:
imglinkPattern = r"(?i)http[^ ]+\.(?:png|jpg|jpeg|gif)"
match = re.search(imglinkPattern, msg)
if match:
store[KEY_EMBED] = match.group(0)
await ctx.message.delete()
newMsg = await ctx.send(
":warning: {} created a spoiler! React to see "
"the message!".format(ctx.message.author.mention)
)
async with self.config.messages() as messages:
messages[str(newMsg.id)] = store
await newMsg.add_reaction("\N{INFORMATION SOURCE}")
self.logger.info(
"%s#%s (%s) added a spoiler: %s",
ctx.message.author.name,
ctx.message.author.discriminator,
ctx.message.author.id,
msg,
)
except discord.errors.Forbidden as error:
await ctx.send("I'm not able to do that.")
await newMsg.delete()
self.logger.error(
"Could not create a spoiler in server %s channel %s",
ctx.message.server.name,
ctx.message.channel.name,
)
self.logger.error(error)
@commands.Cog.listener("on_raw_reaction_add")
async def checkForReaction(self, payload):
"""Reaction listener (using socket data)
Checks to see if a spoilered message is reacted, and if so, send a DM to the
user that reacted.
"""
spoilerMessages = await self.config.messages()
# make sure the reaction is proper
msgId = payload.message_id
if str(msgId) in spoilerMessages:
server = discord.utils.get(self.bot.guilds, id=payload.guild_id)
reactedUser = discord.utils.get(server.members, id=payload.user_id)
if reactedUser.bot:
return
channel = discord.utils.get(server.channels, id=payload.channel_id)
message = await channel.fetch_message(int(msgId))
if payload.emoji.id:
emoji = discord.Emoji(name=payload.emoji.name, id=payload.emoji.id, server=server)
else:
emoji = payload.emoji.name
await message.remove_reaction(emoji, reactedUser)
if (
msgId in self.onCooldown.keys()
and reactedUser.id in self.onCooldown[msgId].keys()
and self.onCooldown[msgId][reactedUser.id] > datetime.now()
):
return
msg = spoilerMessages[str(msgId)]
embed = discord.Embed()
userObj = discord.utils.get(server.members, id=int(msg[KEY_AUTHOR_ID]))
if userObj:
embed.set_author(
name="{0.name}#{0.discriminator}".format(userObj),
icon_url=userObj.display_avatar.url,
)
else:
embed.set_author(name=msg[KEY_AUTHOR_NAME])
if KEY_EMBED in msg:
embed.set_image(url=msg[KEY_EMBED])
embed.description = msg[KEY_MESSAGE]
embed.timestamp = datetime.fromtimestamp(int(msg[KEY_TIMESTAMP]))
try:
await reactedUser.send(embed=embed)
if msgId not in self.onCooldown.keys():
self.onCooldown[msgId] = {}
self.onCooldown[msgId][reactedUser.id] = datetime.now() + timedelta(
seconds=COOLDOWN
)
except (discord.errors.Forbidden, discord.errors.HTTPException) as error:
self.logger.error(
"Could not send DM to %s#%s (%s).",
reactedUser.name,
reactedUser.discriminator,
reactedUser.id,
)
self.logger.error(error)