-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
228 lines (184 loc) · 6.49 KB
/
main.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
import asyncio
import json
import logging
import socket
import sys
from logging.handlers import SysLogHandler
import boto3
import discord
from discord.ext import commands
from cogs.utils import get_color
def get_credentials():
s3 = boto3.client("s3")
bucket = "kanyeweastcredentials"
s3.download_file(bucket, "secrets.json", "secrets.json")
s3.download_file(bucket, "database.db", "database.db")
s3.download_file(bucket, "banned_users.pkl", "banned_users.pkl")
get_credentials()
with open("secrets.json") as f:
keys = json.load(f)
help_command = commands.DefaultHelpCommand(no_category="Commands")
owners = {int(keys["ID_BENNY"]), int(keys["ID_STARBOY"])}
class Bot_With_Sniped_Messages(commands.Bot):
def __init__(self, **kwargs):
super().__init__(**kwargs)
self.sniped_messages = {}
self.sniped_len = 5
bot = Bot_With_Sniped_Messages(
command_prefix="&",
help_command=help_command,
owner_ids=owners,
intents=discord.Intents.all(),
case_insensitive=True,
)
@bot.event
async def on_message(message: discord.Message):
await bot.process_commands(message)
if bot.user is not None:
return
if message.author.id == bot.user.id:
return
what_responses = {"what", "wat", "wht", "wot", "whot", "waht"}
if (
not (
await bot.is_owner(message.author)
or message.author.id == int(keys["ID_TINA"])
)
and message.content.lower() in what_responses
):
await message.reply("smb")
if message.author.id == 630492967018430489:
if "<:lemean:903117276587376710>" in message.content:
await message.reply("<:lemean:903117276587376710>")
@bot.event
async def on_message_delete(message):
if bot.sniped_messages.get(message.guild.id) is None:
bot.sniped_messages[message.guild.id] = []
color = get_color(message.author.avatar.url)
sniped_content = (
message.content,
message.author,
message.channel.name,
message.created_at,
color,
)
if message.attachments:
sniped_content = (message.attachments[0].proxy_url,) + sniped_content
if len(bot.sniped_messages[message.guild.id]) == bot.sniped_len:
bot.sniped_messages[message.guild.id].pop(0)
bot.sniped_messages[message.guild.id].append(sniped_content)
async def build_sniped_message(ctx: commands.Context, sniped_content: tuple):
proxy_url = None
if len(sniped_content) == 6:
proxy_url = sniped_content[0]
(contents, author, channel_name, time, color) = sniped_content[-5:]
pfp_url = author.avatar.url
embed = discord.Embed(description=contents, color=color, timestamp=time)
embed.set_author(name=f"{author.name}#{author.discriminator}", icon_url=pfp_url)
embed.set_footer(text=f"Deleted in : #{channel_name}")
if proxy_url is not None:
embed.set_image(url=proxy_url)
await ctx.channel.send(embed=embed)
@commands.is_owner()
@bot.command(
name="setsnipenum",
brief="Sets the number of snipes to save",
help="Sets the number of snipes to save",
)
async def setsnipelen(ctx: commands.Context, len: commands.Range[int, 0, 20] = 5):
bot.sniped_len = len
await ctx.send(f"Snipe number set to {len}")
def ordinal(x):
return ["1st", "2nd", "3rd", "4th", "5th"][abs(x) - 1]
@bot.command(
name="snipe",
brief="Snipes last deleted message in channel.",
help="Retrieves and sends the most recently deleted message in the server.",
)
async def snipe(
ctx: commands.Context, snipe_num: commands.Range[int, -bot.sniped_len, -1] = -1
):
if not ctx.guild:
await ctx.channel.send("This command can only be used in a server!")
return
try:
sniped_content = bot.sniped_messages[ctx.guild.id][snipe_num]
await build_sniped_message(ctx, sniped_content)
except IndexError:
await ctx.channel.send(
f"Couldn't find the {ordinal(snipe_num)} deleted message."
)
return
except KeyError:
await ctx.channel.send("Couldn't find a message to snipe!")
return
@snipe.error
async def snipe_on_error(ctx: commands.Context, error):
if isinstance(error, commands.BadArgument):
await ctx.channel.send(
f"Please enter a valid number between -{bot.sniped_len} and -1."
)
return
raise error
def setup_logs():
logger = logging.getLogger("discord")
logger.setLevel(logging.INFO)
class _ColourFormatter(logging.Formatter):
LEVEL_COLOURS = [
(logging.DEBUG, "\x1b[40;1m"),
(logging.INFO, "\x1b[34;1m"),
(logging.WARNING, "\x1b[33;1m"),
(logging.ERROR, "\x1b[31m"),
(logging.CRITICAL, "\x1b[41m"),
]
FORMATS = {
level: logging.Formatter(
f"\x1b[30;1m%(asctime)s\x1b[0m {colour}%(levelname)-8s\x1b[0m \x1b[35m%(name)s\x1b[0m %(message)s",
"%Y-%m-%d %H:%M:%S",
)
for level, colour in LEVEL_COLOURS
}
def format(self, record):
formatter = self.FORMATS.get(record.levelno)
if formatter is None:
formatter = self.FORMATS[logging.DEBUG]
# Override the traceback to always print in red
if record.exc_info:
text = formatter.formatException(record.exc_info)
record.exc_text = f"\x1b[31m{text}\x1b[0m"
output = formatter.format(record)
# Remove the cache layer
record.exc_text = None
return output
formatter = _ColourFormatter()
class _ContextFilter(logging.Filter):
hostname = socket.gethostname()
def filter(self, record):
record.hostname = _ContextFilter.hostname
return True
sysloghandler = SysLogHandler(
address=(keys["PAPERTRAIL"], int(keys["PAPERTRAILPORT"]))
)
sysloghandler.addFilter(_ContextFilter())
sysloghandler.setFormatter(formatter)
logger.addHandler(sysloghandler)
stderrhandler = logging.StreamHandler(sys.stderr)
stderrhandler.setFormatter(formatter)
logger.addHandler(stderrhandler)
async def load_cogs():
"""Loads cogs for bot"""
cog_list = [
"cogs.dev",
"cogs.pics",
"cogs.misc",
"cogs.music",
"cogs.users",
"cogs.globallisteners",
"cogs.trivia",
"cogs.loops",
]
for cog in cog_list:
await bot.load_extension(cog)
setup_logs()
asyncio.run(load_cogs())
bot.run(keys["TOKEN"], log_handler=None)