-
Notifications
You must be signed in to change notification settings - Fork 2
/
references.py
243 lines (182 loc) · 6.83 KB
/
references.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
import discord
from discord.ext import commands
import datetime
import asyncio
bot = commands.Bot(command_prefix='!', intents=discord.Intents.all())
# CONVERTERS
class DurationConverter(commands.Converter):
async def convert(self, ctx: commands.Context, argument: str) -> datetime.timedelta:
multipliers = {
's': 1, # seconds
'm': 60, # minutes
'h': 3600, # hours
'd': 86400, # days
'w': 604800 # weeks
}
try:
amount = int(argument[:-1])
unit = argument[-1]
seconds = amount * multipliers[unit]
delta = datetime.timedelta(seconds=seconds)
return delta
except (ValueError, KeyError):
raise commands.BadArgument("Invalid duration provided.")
# PARAMETER METADATA
@bot.command()
async def timeout(ctx: commands.Context, member: discord.Member, duration: datetime.timedelta = commands.parameter(converter=DurationConverter)):
await member.timeout(duration)
await ctx.send(f"Timed out {member.mention} for {duration}")
# GLOBAL CHECKS
@bot.check
def check(ctx: commands.Context):
...
# When an error inside check happens, the error is propagated to the error handlers.
# If you don't raise an exception but return false-like value, then it will get wrapped up into a CheckFailure exception.
class CustomException(commands.CommandError): ...
async def check(ctx: commands.Context):
if "1" in ctx.message.content:
raise CustomException()
if "2" in ctx.message.content:
return False
return True
@commands.check(check)
@bot.command()
async def foo(ctx: commands.Context):
await ctx.send("Success!")
@foo.error
async def handler(ctx: commands.Context, error: commands.CommandError):
if isinstance(error, CustomException):
await ctx.send("CustomException was raised inside check!")
elif isinstance(error, commands.CheckFailure):
await ctx.send("Check has failed!")
else:
await ctx.send(f"Got unexpected error: {error}")
"""
ALL THE PERMISSIONS
add_reactions, administrator, attach_files, ban_members, change_nickname,
connect, create_instant_invite, create_private_threads, create_public_threads,
deafen_members, embed_links, external_emojis, external_stickers, kick_members,
manage_channels, manage_emojis, manage_emojis_and_stickers, manage_events,
manage_guild, manage_messages, manage_nicknames, manage_permissions, manage_roles,
manage_threads, manage_webhooks, mention_everyone, moderate_members, move_members,
mute_members, priority_speaker, read_message_history, read_messages, request_to_speak,
send_messages, send_messages_in_threads, send_tts_messages, speak, stream,
use_application_commands, use_embedded_activities, use_external_emojis,
use_external_stickers, use_voice_activation, view_audit_log, view_channel,
view_guild_insights
"""
# HANDLED BY DISCORD
@bot.tree.command()
@discord.app_commands.guild_only()
async def foo(interaction: discord.Interaction):
await interaction.response.send_message(f"Success!")
# BEFORE INVOKE HOOK
async def func(ctx: commands.Context):
await ctx.send("hook")
@bot.hybrid_command()
@commands.before_invoke(func)
async def foo(ctx: commands.Context):
await ctx.send("command")
# AFTER INVOKE HOOK
async def func(ctx: commands.Context):
await ctx.send("hook")
@bot.hybrid_command()
@commands.after_invoke(func)
async def foo(ctx: commands.Context):
await ctx.send("command")
#COOLDOWN (throws CommandOnCooldown Exception)
@bot.hybrid_command()
@commands.cooldown(1, 10, commands.BucketType.user)
async def foo(ctx: commands.Context):
await ctx.send("Success!")
#- - - - - - - - - - -
def cooldown(ctx: commands.Context):
"""A cooldown for 10 seconds for everyone except listed users"""
if ctx.author.id in (656919778572632094, 703327554936766554):
return
return commands.Cooldown(1, 10)
@bot.hybrid_command()
@commands.dynamic_cooldown(cooldown, commands.BucketType.user)
async def foo(ctx: commands.Context):
await ctx.send("Success!")
# MaxConcurrency
@bot.hybrid_command()
@commands.max_concurrency(1, commands.BucketType.member, wait=False)
async def foo(ctx: commands.Context):
await asyncio.sleep(1)
await ctx.send("Success!")
# A CUSTOM CHECK
banwords = {"rabbit", "horse"}
async def safe_content(ctx):
return not (set(ctx.message.content.lower().split()) & banwords)
@bot.command()
@commands.check(safe_content)
async def check_content(ctx):
await ctx.send("Content is clean!")
#### ANSIIIIIIIIIIIIIIIIIIIIIIII
import enum
class Style(enum.IntEnum):
def __str__(self) -> str:
return f"{self.value}"
class Colors(Style):
GRAY = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
class BackgroundColors(Style):
FIREFLY_DARK_BLUE = 40
ORANGE = 41
MARBLE_BLUE = 42
GREYISH_TURQUOISE = 43
GRAY = 44
INDIGO = 45
LIGHT_GRAY = 46
WHITE = 47
class Styles(Style):
NORMAL = 0
BOLD = 1
UNDERLINE = 4
class AnsiBuilder:
def __init__(self, text: str = "", *styles: Style) -> None:
self.styles = styles
self.cursor = len(text)
self.text = f"\033[{';'.join(map(str, styles))}m{text}\033[0m" if styles and text else text
def __add__(self, other: str) -> "AnsiBuilder":
self.text += other
self.cursor += len(other)
return self
def write(self, cursor: int, text: str) -> "AnsiBuilder":
if cursor > self.cursor or cursor > len(self.text):
raise ValueError("Cursor cannot be greater than the length of the text")
if cursor < 0:
raise ValueError("Cursor cannot be less than 0")
self.text = self.text[:cursor] + text + self.text[cursor:]
self.cursor += len(text)
return self
def __str__(self) -> str:
return self.text
@classmethod
def to_ansi(cls, text: str, *styles: Style) -> str:
return str(cls(text, *styles))
@property
def block(self) -> str:
return f"```ansi\n{self.text}```"
@commands.command()
async def test(self, ctx: commands.Context):
async for log in ctx.guild.audit_logs(limit=100):
await ctx.send(f"{log.user} did {log.action} to {log.target}")
@commands.command()
async def test2(self, ctx: commands.Context, name: str, filter: str):
automod = await ctx.guild.create_automod_rule(
name=name,
event_type=discord.AutoModRuleEventType.message_send,
trigger=discord.AutoModTrigger(
keyword_filter=[filter]
),
actions=[discord.AutoModRuleAction(type=discord.AutoModRuleActionType.block_message)]
)
await ctx.send(f"Created automod rule with ID {automod.id}")