-
Notifications
You must be signed in to change notification settings - Fork 18
/
ROBOT.py
executable file
·188 lines (162 loc) · 6.17 KB
/
ROBOT.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
from __future__ import annotations
import asyncio
import glob
import sys
import traceback
import warnings
from datetime import datetime
from pathlib import Path
from typing import Coroutine
import discord
from PIL import Image
from discord import app_commands
from discord.ext import commands
import auth
import config
import webhooks
from src.types import Macro
from src.db import Database
from numpy import set_printoptions as numpy_set_printoptions
class Context(commands.Context):
silent: bool = False
ephemeral: bool = False
async def error(self, msg: str, embed: discord.Embed | None = None, **kwargs) -> Coroutine[discord.Message]:
try:
await self.message.add_reaction("\u26a0\ufe0f")
except discord.errors.NotFound:
pass
if embed is not None:
return await self.reply(msg, embed=embed, **kwargs)
else:
return await self.reply(msg, **kwargs)
async def send(self, content: str = "", embed: discord.Embed | None = None, **kwargs):
content = str(content)
kwargs['ephemeral'] = self.ephemeral
kwargs['silent'] = self.silent
if len(content) > 2000:
msg = " [...] \n\n (Character limit reached!)"
content = content[:2000 - len(msg)] + msg
if embed is not None:
if content:
return await super().send(content, embed=embed, **kwargs)
return await super().send(embed=embed, **kwargs)
elif content:
return await super().send(content, embed=embed, **kwargs)
return await super().send(**kwargs)
async def reply(self, *args, mention_author: bool = False, **kwargs):
kwargs['mention_author'] = mention_author
kwargs['reference'] = self.message
kwargs['ephemeral'] = self.ephemeral
return await self.send(*args, **kwargs)
class Bot(commands.Bot):
"""Custom bot class :)"""
db: Database
def __init__(
self,
*args,
cogs: list[str],
embed_color: discord.Color,
webhook_id: int,
prefixes: list[str],
db_path: str,
**kwargs
):
self.started = datetime.utcnow()
self.loading = True
self.exit_code = 0
self.embed_color = embed_color
self.webhook_id = webhook_id
self.prefixes = prefixes
self.db = Database(self)
self.db_path = db_path
self.config = config.__dict__
self.renderer = None
self.flags = None
self.variants = None
self.palette_cache = {}
self.macros = {}
self.baba_loaded = True
for path in glob.glob("data/palettes/*.png"):
with Image.open(path) as im:
self.palette_cache[Path(path).stem] = im.convert("RGBA").copy()
numpy_set_printoptions(
threshold=sys.maxsize,
linewidth=sys.maxsize
)
super().__init__(*args, **kwargs)
self.remove_command('help')
# has to be after __init__
async def gather_cogs():
await asyncio.gather(*(self.load_extension(cog, package='ROBOT') for cog in cogs))
asyncio.run(gather_cogs())
async def get_context(self, message: discord.Message, **kwargs) -> Context:
return await super().get_context(message, cls=Context)
async def close(self) -> None:
await self.db.close()
await super().close()
async def on_ready(self) -> None:
await self.db.connect(self.db_path)
print("Loading macros...")
async with self.db.conn.cursor() as cur:
await cur.execute("SELECT * from macros")
for (name, value, description, author) in await cur.fetchall():
self.macros[name] = Macro(value, description, author)
print(f"Logged in as {self.user}!")
if bot.baba_loaded:
await self.change_presence(activity=discord.Activity(type=discord.ActivityType.listening, name="commands..."))
else:
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.playing,
name="Still being set up..."
),
status=discord.Status.do_not_disturb
)
async def is_owner(self, user: discord.User):
if user.id == 280756504674566144: # Implement your own conditions here
return True
# Else fall back to the original
return await super().is_owner(user)
discord.utils.setup_logging()
# Establishes the bot
bot = Bot(
# Prefixes
commands.when_mentioned_or(*config.prefixes) if config.trigger_on_mention else config.prefixes,
# Other behavior parameters
case_insensitive=True,
activity=discord.Game(name=config.activity),
description=config.description,
# Never mention roles, @everyone or @here
allowed_mentions=discord.AllowedMentions(everyone=False, roles=False, users=False),
# Only receive message and reaction events
intents=discord.Intents(messages=True, reactions=True, guilds=True, message_content=True),
# Disable the member cache
member_cache_flags=discord.MemberCacheFlags.none(),
# Disable the message cache
max_messages=None,
# Don't chunk guilds
chunk_guilds_at_startup=False,
# custom fields
cogs=config.cogs,
embed_color=config.embed_color,
webhook_id=webhooks.logging_id,
prefixes=config.prefixes,
db_path=config.db_path
)
@bot.event
async def on_command(ctx):
try:
webhook = await bot.fetch_webhook(webhooks.logging_id)
ctx: Context
embed = discord.Embed(
description=ctx.message.content,
color=config.logging_color)
embed.set_author(name=f'{ctx.author.name}'[:32],
icon_url=ctx.author.avatar.url if ctx.author.avatar else None)
if not isinstance(ctx.channel, discord.DMChannel):
embed.set_footer(text=f"{ctx.message.guild.name} ({ctx.message.guild.id})", icon_url=ctx.message.guild.icon.url)
await webhook.send(embed=embed)
except Exception as e:
warnings.warn("\n".join(traceback.format_exception(e)))
bot.run(auth.token, log_handler=None)
sys.exit(bot.exit_code)