-
Notifications
You must be signed in to change notification settings - Fork 0
/
cog.py
352 lines (301 loc) · 12.7 KB
/
cog.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
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
import typing
import nextcord
from nextcord.ext import commands
import aiohttp
import io
from emoji import EMOJI_ALIAS_UNICODE_ENGLISH as EMOJIS
import constants
from utils import discord_utils, logging_utils, command_predicates
class DiscordCog(commands.Cog, name="Discord"):
"""Discord Utility Commands"""
def __init__(self, bot):
self.bot = bot
####################
# PINNING COMMANDS #
####################
@commands.command(name="pin")
async def pin(self, ctx, to_delete: str = ""):
"""Pin a message (Either reply to the message, or it auto pins the message above)
If the message is already pinned, just unpins and repins (brings it to top of pins).
If you say delete after the command, it deletes original message that called the command.
Usage: `~pin` (as reply to message)
Usage: `~pin` (to just pin the last message)
Usage: `~pin del` (to pin the message and also delete the msg)
"""
logging_utils.log_command("pin", ctx.guild, ctx.channel, ctx.author)
if not ctx.message.reference:
channel_history = await ctx.message.channel.history(limit=2).flatten()
message = channel_history[-1]
else:
message = await ctx.fetch_message(ctx.message.reference.message_id)
embed_or_none = await discord_utils.pin_message(message)
# Error pinning, send error message to user
if embed_or_none is not None:
await ctx.send(embed=embed_or_none)
else:
await message.add_reaction(EMOJIS[":pushpin:"])
await ctx.message.add_reaction(EMOJIS[":white_check_mark:"])
try:
if to_delete.lower()[0:3] == "del":
await ctx.message.delete()
except nextcord.Forbidden:
embed = discord_utils.create_embed()
embed.add_field(
name=f"{constants.FAILED}!",
value=f"Unable to delete original message. Do I have `manage_messages` permissions?",
)
await ctx.send(embed=embed)
return
@commands.command(name="pinme")
async def pinme(self, ctx):
"""Pins the message that called it.
Usage : `~pinme Message`
"""
logging_utils.log_command("pinme", ctx.guild, ctx.channel, ctx.author)
embed_or_none = await discord_utils.pin_message(ctx.message)
# Error pinning, send error message to user
if embed_or_none is not None:
await ctx.send(embed=embed_or_none)
else:
await ctx.message.add_reaction(EMOJIS[":pushpin:"])
# await ctx.message.add_reaction(EMOJIS[':white_check_mark:'])
@commands.command(name="unpin")
async def unpin(self, ctx, num_to_unpin: int = 1):
"""Unpins a specific message from a channel, or a given number of pins
Usage: `~unpin 2` (unpins latest 2 pins)
Usage: `~unpin` (as a reply to pinned message)
"""
logging_utils.log_command("unpin", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
if num_to_unpin < 1 or not isinstance(num_to_unpin, int):
embed = discord_utils.create_no_argument_embed(
"number of messages to unpin"
)
await ctx.send(embed=embed)
return
pins = await ctx.message.channel.pins()
messages_to_unpin = []
strmsg = ""
reply = False
# If unpin is in direct reply to another message, unpin only that message
if ctx.message.reference:
reply = True
orig_msg = ctx.message.reference.resolved
if not orig_msg.pinned:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"The linked message [Msg]({orig_msg.jump_url}) has not been pinned, there's nothing to unpin.",
inline=False,
)
await ctx.send(embed=embed)
return
messages_to_unpin.append(orig_msg)
# Else unpin the last X messages
else:
if num_to_unpin < len(pins):
messages_to_unpin = pins[:num_to_unpin]
# If too many messages to unpin, just unpin all
else:
messages_to_unpin = pins
i = 1
for pin in messages_to_unpin:
try:
await pin.unpin()
await pin.remove_reaction(EMOJIS[":pushpin:"], ctx.me)
strmsg = strmsg + f"[Msg{i}]({pin.jump_url}) : "
i = i + 1
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"I do not have permissions to unpin that message. Please check my perms and try again?",
inline=False,
)
await ctx.send(embed=embed)
return
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"Unpinned {'the most recent' if not reply else ''} {i - 1} {'messages' if i - 1 != 1 else 'message'}\n"
+ f"{strmsg[:-3]}",
inline=False,
)
await ctx.send(embed=embed)
await ctx.message.add_reaction(EMOJIS[":white_check_mark:"])
@commands.command(name="lspin", aliases=["lspins", "listpin", "listpins"])
async def listpin(self, ctx):
"""Lists all the pinned posts in the current channel
Usage: `~listpins~`
"""
logging_utils.log_command("listpin", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
pins = await ctx.message.channel.pins()
strmsg = ""
i = 1
for pin in pins:
strmsg = strmsg + f"[Msg{i}]({pin.jump_url}) : "
i += 1
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"There are {len(pins)} pinned posts on this channel."
f"\n{strmsg[:-3]}",
inline=False,
)
embeds = discord_utils.split_embed(embed)
for embed in embeds:
await ctx.send(embed=embed)
#######################
# STATISTICS COMMANDS #
#######################
@command_predicates.is_verified()
@commands.command(name="stats")
async def stats(self, ctx):
"""Get server stats
Permission Category : Verified Roles only.
Usage: `~stats`
"""
logging_utils.log_command("stats", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
guild = ctx.guild
embed.add_field(name="Members", value=f"{guild.member_count}", inline=False)
embed.add_field(name="Roles", value=f"{len(guild.roles)}", inline=False)
embed.add_field(
name="Emoji (limit)",
value=f"{len(guild.emojis)} ({guild.emoji_limit})",
inline=False,
)
embed.add_field(
name="Categories", value=f"{len(guild.categories)}", inline=False
)
embed.add_field(
name="Text Channels", value=f"{len(guild.text_channels)}", inline=False
)
embed.add_field(
name="Voice Channels", value=f"{len(guild.voice_channels)}", inline=False
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(name="catstats")
async def catstats(self, ctx, cat_name: str = ""):
"""Get category stats
Permission Category : Verified Roles only.
Usage: `~catstats` (current category)
Usage: `~catstats "Cat Name"` (Named category)
"""
logging_utils.log_command("catstats", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
if cat_name == "":
cat = ctx.message.channel.category
else:
cat = await discord_utils.find_category(ctx, cat_name)
if cat is None:
embed.add_field(
name=f"{constants.FAILED}",
value=f"I cannot find category `{cat_name}`. Perhaps check your spelling and try again.",
)
await ctx.send(embed=embed)
return
embed.add_field(name="Category Name", value=f"{cat.name}", inline=False)
embed.add_field(
name="Text Channels", value=f"{len(cat.text_channels)}", inline=False
)
embed.add_field(
name="Voice Channels", value=f"{len(cat.voice_channels)}", inline=False
)
await ctx.send(embed=embed)
##################
# EMOJI COMMANDS #
##################
@command_predicates.is_verified()
@commands.command(name="listemoji", aliases=["lsemoji", "listemojis", "lsemojis"])
async def listemoji(self, ctx):
"""List all emojis in a server
Permission Category : Verified Roles only.
Usage: `~listemojis`
"""
logging_utils.log_command("listemoji", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
embed.add_field(
name=f"Emoji in {ctx.guild.name}",
value=f"{chr(10).join([f'{emoji} {emoji.name} {emoji.id}' for emoji in ctx.guild.emojis])}",
)
await ctx.send(embed=embed)
@command_predicates.is_verified()
@commands.command(name="steal")
async def steal(
self, ctx, *emojis: typing.Union[nextcord.Emoji, nextcord.PartialEmoji]
):
"""Steals an emote from another server and uploads it to this server with the same name.
Permission Category : Verified Roles only.
Usage: `~steal :emote1: :emote2:`
"""
logging_utils.log_command("steal", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
for emoji in emojis:
url = str(emoji.url)
name = emoji.name
async with aiohttp.ClientSession() as ses:
async with ses.get(url) as r:
try:
img_or_gif = io.BytesIO(await r.read())
b_value = img_or_gif.getvalue()
try:
emoji = await ctx.guild.create_custom_emoji(
image=b_value, name=name
)
embed.add_field(
name=f"{constants.SUCCESS}",
value=f"Added {emoji} with name {emoji.name}",
)
await ses.close()
except nextcord.Forbidden:
embed.add_field(
name=f"{constants.FAILED}",
value=f"Error adding `:{name}:` to server. Do I have the correct permissions to manage emotes in this server?",
)
await ses.close()
except:
embed.add_field(
name=f"{constants.FAILED}",
value=f"Could not add `:{name}:` to server. Do you have any emote slots left?",
)
await ses.close()
except:
embed.add_field(
name=f"{constants.FAILED}",
value=f"Could not find emote `:{name}:`.",
)
await ctx.send(embed=embed)
@commands.command(name="getsource")
async def getsource(self, ctx):
"""Gives the discord formatted source code for a specific message in the channel.
This command must be a reply
Usage: `~getsource` (as a reply to the message)
"""
logging_utils.log_command("getsource", ctx.guild, ctx.channel, ctx.author)
embed = discord_utils.create_embed()
# If not direct reply to another message
if not ctx.message.reference:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"The command `~getsource` can only be used as a reply to another message.",
inline=False,
)
await ctx.send(embed=embed)
return
orig_msg = ctx.message.reference.resolved
if orig_msg.content is None or len(orig_msg.content) == 0:
embed.add_field(
name=f"{constants.FAILED}!",
value=f"The replied message has no content to `~getsource` from. Is it a bot or system message?",
inline=False,
)
await ctx.send(embed=embed)
return
msg = "```" + orig_msg.content + "```"
embed.add_field(
name=f"{constants.SUCCESS}!",
value=f"{msg}",
inline=False,
)
await ctx.send(embed=embed)
def setup(bot):
bot.add_cog(DiscordCog(bot))