forked from flaree/flare-cogs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathembedcreator.py
154 lines (136 loc) · 5.75 KB
/
embedcreator.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
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
import json
import re
import traceback
from abc import ABC
from typing import Optional
import discord
from redbot.core import Config, commands
from .embedmixin import EmbedMixin
from .globalembeds import EmbedGlobal
from .sending import EmbedSending
from .storing import EmbedStoring
from .update import EmbedUpdate
from .utils import EmbedUtils
START_CODE_BLOCK_RE = re.compile(r"^((```json)(?=\s)|(```))")
class CompositeMetaClass(type(commands.Cog), type(ABC)):
"""This allows the metaclass used for proper type detection to coexist with discord.py's
metaclass."""
class EmbedCreator(
EmbedMixin,
EmbedUtils,
EmbedGlobal,
EmbedSending,
EmbedStoring,
EmbedUpdate,
commands.Cog,
metaclass=CompositeMetaClass,
):
"""EmbedCreator"""
async def red_get_data_for_user(self, *, user_id: int):
# this cog does not story any data
return {}
async def red_delete_data_for_user(self, *, requester, user_id: int) -> None:
# this cog does not story any data
all_guilds = await self.config.all_guilds()
matches = []
for guild_id, guildconf in all_guilds.items():
for embed in guildconf["embeds"]:
if guildconf["embeds"][embed]["author"] == user_id:
matches.append((guild_id, embed))
for match in matches:
async with self.config.guild_from_id(match[0]).embeds() as embeds:
embeds[match[1]]["author"] = 00000000
all_embeds = await self.config.all()
all_matches = []
for embed in all_embeds["embeds"]:
if all_embeds["embeds"][embed]["author"] == user_id:
all_matches.append(embed)
async with self.config.embeds() as embeds:
for match in all_matches:
embeds[match]["author"] = 00000000
__version__ = "0.2.1"
def format_help_for_context(self, ctx):
pre_processed = super().format_help_for_context(ctx)
return f"{pre_processed}\nCog Version: {self.__version__}"
def __init__(self, bot) -> None:
self.bot = bot
self.config = Config.get_conf(self, 95932766180343808, force_registration=True)
self.config.register_guild(embeds={})
self.config.register_global(embeds={})
@staticmethod
def cleanup_code(content):
"""Automatically removes code blocks from the code."""
# remove ```py\n```
if content.startswith("```") and content.endswith("```"):
return START_CODE_BLOCK_RE.sub("", content)[:-3]
return content
async def build_embed(self, ctx, *, data, channel):
embed = await self.validate_data(ctx, data=data)
if not embed:
return
try:
await channel.send(embed=embed)
except discord.errors.HTTPException as error:
err = "\n".join(traceback.format_exception_only(type(error), error))
em = discord.Embed(
title="Parsing Error",
description=f"The following is an extract of the error:\n```py\n{err}``` \nValidate your input by using any available embed generator online.",
colour=discord.Color.red(),
)
await ctx.send(embed=em)
async def store_embed(self, ctx, is_global, *, name, data):
embed = await self.validate_data(ctx, data=data)
if not embed:
return
try:
await ctx.send("Here's how this will look.", embed=embed)
except discord.errors.HTTPException as error:
err = "\n".join(traceback.format_exception_only(type(error), error))
em = discord.Embed(
title="Parsing Error",
description=f"The following is an extract of the error:\n```py\n{err}``` \nValidate your input by using any available embed generator online.",
colour=discord.Color.red(),
)
await ctx.send(embed=em)
return
if not is_global:
async with self.config.guild(ctx.guild).embeds() as embeds:
embeds[name] = {"data": data, "author": ctx.author.id}
else:
async with self.config.embeds() as embeds:
embeds[name] = {"data": data, "author": ctx.author.id}
async def validate_data(self, ctx, *, data) -> Optional[discord.Embed]:
if not isinstance(data, dict):
try:
data = json.loads(data)
except json.decoder.JSONDecodeError:
return await ctx.send(
"Unable to read JSON, ensure it is correctly formatted and validated."
)
if not isinstance(data, dict):
await ctx.send("The JSON provided is not in a dictionary format.")
return False
if data.get("embed"):
data = data["embed"]
if data.get("embeds"):
data = data["embeds"][0]
if data.get("timestamp"):
data["timestamp"] = data["timestamp"].strip("Z")
try:
embed = discord.Embed().from_dict(data)
except Exception:
await ctx.send(
"Oops. An error occured turning the input to an embed. Please validate the file and ensure it is using the correct keys."
)
return False
else:
if not isinstance(embed, discord.Embed):
await ctx.send("Embed could not be built from the json provided.")
return False
if len(embed) < 1 or len(embed) > 6000:
if not any([embed.thumbnail, embed.image]):
await ctx.send(
"The returned embed does not fit within discords size limitations. The total embed length must be greater then 0 and less than 6000."
)
return False
return embed