-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
main.py
316 lines (263 loc) · 10.9 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
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
"""
The MIT License (MIT)
Copyright (c) 2021-present Pycord Development
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
"""
import inspect
import os
import time
import discord
from discord import SlashCommand, Option, SlashCommandGroup, option, OptionChoice, AllowedMentions
from discord.ext import commands
from tools import Bot, send_code, get_prefix
bot = Bot(command_prefix=get_prefix,
case_insensitive=True,
strip_after_prefix=True,
intents=discord.Intents.all(),
activity=discord.Activity(type=discord.ActivityType.watching, name="Pycord"),
description="The official pycord bot")
brainfuck = bot.create_group("bf", "Commands related to brainfuck.")
github = bot.create_group("github", "Commands related to github.")
repo = 'https://github.com/Pycord-Development/pycord'
for cog in bot.config.get('cogs', []):
try:
bot.load_extension(cog)
print(cog)
except discord.DiscordException:
if __name__ == "__main__":
print(f'!!! {cog} !!!')
else:
raise
@brainfuck.command()
async def encode(ctx, text: Option(str, "Text to encode in brainfuck")):
"""Encode text into brainfuck."""
encoded = bot.brainfuck.encode(text)
await send_code(ctx, encoded.code, lang="bf")
@brainfuck.command(name="compile")
async def _compile(ctx, code: Option(str, "Brainfuck code to compile into python")):
"""Compile brainfuck into python."""
compiled = bot.brainfuck.compile(code)
await send_code(ctx, compiled.code, lang="py")
@brainfuck.command()
async def decode(ctx, code: Option(str, "Brainfuck code to decode into text")):
"""Decode brainfuck into text."""
decoded = bot.brainfuck.decode(code)
await send_code(ctx, decoded.text, lang="txt", filename="text.txt")
@bot.slash_command()
async def invite(ctx):
"""Invite me to your server."""
permissions = 2134207679
url = discord.utils.oauth_url(client_id=bot.user.id, permissions=discord.Permissions(permissions=permissions),
scopes=("bot", "applications.commands"))
view = discord.ui.View()
view.add_item(discord.ui.Button(label="Invite", url=url))
await ctx.respond("I'm glad you want to add me to your server, here's a link!", view=view)
@bot.slash_command()
async def ping(ctx):
"""Get the latency of the bot."""
latencies = {
"websocket": bot.latency,
}
def comp_message():
msgs = []
for title in latencies:
msgs.append(f"{title.title()}: {(latencies[title] * 1000):.0f}ms")
return '\n'.join(msgs)
start = time.perf_counter()
await ctx.respond(comp_message())
end = time.perf_counter()
latencies["round trip"] = end - start
await ctx.edit(content=comp_message())
@github.command()
async def issue(ctx, number: Option(int, "Issue number")):
"""View an issue from the pycord github repo."""
url = f"{repo}/issues/{number}"
view = discord.ui.View()
view.add_item(discord.ui.Button(label="View Issue", url=url))
await ctx.respond(f"Here's a link", view=view)
@github.command()
async def pr(ctx, number: Option(int, "Pull request number")):
"""View a pull request from the pycord github repo."""
url = f"{repo}/issues/{number}"
view = discord.ui.View()
view.add_item(discord.ui.Button(label="View Pull Request", url=url))
await ctx.respond(f"Here's a link", view=view)
@bot.slash_command()
async def source(ctx, command: Option(str, "The command to view the source code for", required=False)):
"""View the source for a particular command or the whole bot."""
source_url = 'https://github.com/Pycord-Development/robocord'
branch = 'main'
view = discord.ui.View()
if command is None:
url = source_url
label = "Source code for entire bot"
else:
command_split = command.split()
index = 0
obj = discord.utils.get(bot.application_commands.values(), name=command_split[index])
while isinstance(obj, SlashCommandGroup):
if index + 1 > len(command_split):
return await ctx.respond("Error: Command is a group. You must choose a subcommand from it.")
obj = discord.utils.get(obj.subcommands, name=command_split[index])
if not isinstance(obj, SlashCommand):
return await ctx.respond("Error: Command could not be found")
# noinspection PyUnresolvedReferences
src = obj.callback.__code__
filename = src.co_filename
lines, firstlineno = inspect.getsourcelines(src)
location = os.path.relpath(filename).replace('\\', '/')
url = f'{source_url}/blob/{branch}/{location}#L{firstlineno}-L{firstlineno + len(lines) - 1}'
content = await discord.ext.commands.clean_content(escape_markdown=True).convert(ctx, command)
label = f'Source code for command "{content}"'
view.add_item(discord.ui.Button(label="View Code", url=url))
await ctx.respond(label, view=view)
@bot.user_command(name="Join Position")
async def _joinpos(ctx, member):
all_members = list(ctx.guild.members)
all_members.sort(key=lambda m: m.joined_at)
def _ord(n):
return str(n) + (
"th"
if 4 <= n % 100 <= 20
else {1: "st", 2: "nd", 3: "rd"}.get(n % 10, "th")
)
await ctx.respond(f"{member.mention} was the {_ord(all_members.index(member) + 1)} person to join {ctx.guild.name}", allowed_mentions=AllowedMentions(users=False))
role_option = Option(
int,
description="The role you want added",
choices = [
OptionChoice("Events", 915701572003049482),
OptionChoice("Tester", 881968560635805706),
])
@bot.slash_command(name="role", guild_ids=[881207955029110855])
async def _role(ctx, name: role_option):
"""Claim roles in the server"""
role_id = name
assert role_id in (915701572003049482, 881968560635805706)
role = guild.get_role(role_id)
if not role:
await ctx.respond("Error: Couldn't find that role")
elif not role in ctx.author.roles:
await ctx.author.add_roles(role)
await ctx.respond(f"Added {role.mention} role")
else:
await ctx.author.remove_roles(role)
await ctx.respond(f"Removed {role.mention} role")
class Developer(commands.Cog):
def __init__(self, bot_):
self.bot = bot_
@commands.command(name='load', aliases=['l'])
@commands.is_owner()
async def _load(self, ctx, cog_, save: bool = False):
if save:
val = self.bot.config.get('cogs', [])
val.append(cog_)
self.bot.config['cogs'] = val
self.bot.load_extension(cog_)
await ctx.send(f'Loaded cog "{cog_}"{" (saved)" if save else ""}')
@commands.command(name='unload', aliases=['u'])
@commands.is_owner()
async def _unload(self, ctx, cog_, save: bool = False):
if save:
val = self.bot.config.get('cogs', [])
val.remove(cog_)
self.bot.config['cogs'] = val
self.bot.unload_extension(cog_)
await ctx.send(f'Unloaded cog "{cog_}"{" (saved)" if save else ""}')
@commands.command(name='reload', aliases=['r'])
@commands.is_owner()
async def _reload(self, ctx, cog_):
self.bot.reload_extension(cog_)
await ctx.send(f'Reloaded cog "{cog_}"')
@commands.command(name='loadall', aliases=['la'])
@commands.is_owner()
async def _loadall(self, ctx):
data = self.bot.config.setdefault('cogs', [])
cogs = {
'loaded': [],
'not': []
}
for cog_ in data:
if cog_ in bot.extensions:
continue
try:
self.bot.load_extension(cog_)
cogs['loaded'].append(cog_)
except discord.DiscordException:
cogs['not'].append(cog_)
await ctx.send('\n'.join([
('\U00002705' if cog_ in cogs['loaded'] else '\U0000274c')
+ cog_ for cog_ in data]) or "No cogs to load")
@commands.command(name='unloadall', aliases=['ua'])
@commands.is_owner()
async def _unloadall(self, ctx):
cogs = {
'unloaded': [],
'not': []
}
processing = bot.extensions.copy()
for cog_ in processing:
try:
self.bot.unload_extension(cog_)
cogs['unloaded'].append(cog_)
except discord.DiscordException:
cogs['not'].append(cog_)
await ctx.send('\n'.join([
('\U00002705' if cog_ in cogs['unloaded'] else '\U0000274c')
+ cog_ for cog_ in processing]) or "No cogs to unload")
@commands.command(name='reloadall', aliases=['ra'])
@commands.is_owner()
async def _reloadall(self, ctx):
cogs = {
'reloaded': [],
'not': []
}
processing = bot.extensions.copy()
for cog_ in processing:
try:
self.bot.reload_extension(cog_)
cogs['reloaded'].append(cog_)
except discord.DiscordException:
cogs['not'].append(cog_)
await ctx.send('\n'.join([
('\U00002705' if cog_ in cogs['reloaded'] else '\U0000274c')
+ cog_ for cog_ in processing]) or "No cogs to reload")
bot.add_cog(Developer(bot))
@bot.event
async def on_ready():
print("ready")
restart_channel = bot.cache.get("restart_channel")
if restart_channel:
print("Restarted")
del bot.cache["restart_channel"]
channel = bot.get_channel(restart_channel)
await channel.send("I'm back online")
else:
print(f"Logged in as {bot.user}")
await bot.storage.setup_db()
@bot.event
async def on_message_edit(before, after):
if before.content != after.content: # invoke the command again on edit
if not after.author.bot:
ctx = await bot.get_context(after)
await bot.invoke(ctx)
if __name__ == "__main__":
bot.run()
if bot.hang:
# We want to prevent this from finishing, but the bot is logged out
while True:
time.sleep(60)