This repository has been archived by the owner on Jun 24, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
boiler.py
90 lines (69 loc) · 2.34 KB
/
boiler.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
"""
Util functions. Cuts down on boilerplate code, ideally.
"""
import json
import random
from typing import Dict
import discord
from discord.ext import commands
import footers
def embed_template(
title: str = "Someone messed up!", color: discord.Color = discord.Color.gold
) -> discord.Embed:
em = discord.Embed(title=title).set_footer(
text=random.choice(footers.quotes_e10),
icon_url="https://i.imgur.com/2VepakW.png",
)
em.colour = color
return em
def comma_sep(values: str) -> list:
r = []
for value in values.split(","):
r.append(value.strip())
return r
def cleanup_code(content: str) -> str:
"""Automatically removes code blocks from the code."""
# remove ```py\n```
if content.startswith("```") and content.endswith("```"):
return "\n".join(content.split("\n")[1:-1])
# remove `foo`
return content.strip("` \n")
def bot_and_invoke_hasperms(**perms):
def predicate(ctx):
msg = ctx.message
ch = msg.channel
permissions_invoker = ch.permissions_for(msg.author)
permissions_bot = ch.permissions_for(ctx.me)
bot_missing = [
perm
for perm, value in perms.items()
if getattr(permissions_bot, perm, None) != value
]
invoke_missing = [
perm
for perm, value in perms.items()
if getattr(permissions_invoker, perm, None) != value
]
if not bot_missing and not invoke_missing:
return True
elif invoke_missing:
raise commands.MissingPermissions(invoke_missing)
elif bot_missing:
raise commands.BotMissingPermissions(bot_missing)
return discord.ext.commands.check(predicate)
def guild_only_localcheck(ctx: commands.Context) -> bool:
if ctx.guild is None:
raise commands.NoPrivateMessage(
"This command cannot be used in private messages."
)
return True
def perms_todict(perms: discord.PermissionOverwrite) -> Dict[str, bool]:
r = {}
for key, value in iter(perms):
if value is not None:
r[key] = value
return r
def perms_tojson(perms: discord.PermissionOverwrite) -> str:
return json.dumps(perms_todict(perms))
def perms_fromjson(j: str) -> discord.PermissionOverwrite:
return discord.PermissionOverwrite(**(json.loads(j)))