-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutils.py
73 lines (63 loc) · 2.83 KB
/
utils.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
import discord
import time
import sys
import traceback
class DeleteButton(discord.ui.View):
def __init__(self, user_id):
super().__init__(timeout=None)
self.user_id = user_id
@discord.ui.button(label="", style=discord.ButtonStyle.grey, emoji="❌")
async def delete_button(self, interaction: discord.Interaction, button: discord.ui.Button):
if interaction.user.id in [self.user_id, interaction.client.owner_id]:
await interaction.message.delete()
else:
await interaction.response.defer()
last_used = {}
async def send(interaction: discord.Interaction, content: str = None, message: str = "this embed", embed: discord.Embed = None,
ephemeral: bool = False, view: discord.ui.View = None, ping: discord.User = None, deferred: bool = False):
""" Replies to an interaction (app command)
:param interaction: The interation that is being replied to
:param content: Set the content of the message (default message is used if this is not set)
:param message: Set the message placeholder in the default message
:param embed: The embed that is sent with the message
:param ephemeral: Set the ephemeral flag
:param view: Add a custom view to the response (defaults to delete button view)
:param ping: Specify a user to ping
:param deferred: Specify if this response has already been responded to
"""
view = view or DeleteButton(interaction.user.id)
ping = "" if ping is None else ping.mention
if content is None:
content = "{user} suggests that you read {message} {ping}".format(
ping=ping,
user=interaction.user.display_name,
message=message
)
else:
content = content + " " + ping
if not ephemeral:
if (str(interaction.channel.id) + interaction.command.name in last_used) and (last_used[str(interaction.channel.id) + interaction.command.name] + 10 > time.time()):
ephemeral = True
view = discord.ui.View()
content = "**Your message was hidden because someone requested it recently**\n\n" + content
else:
last_used[str(interaction.channel.id) + interaction.command.name] = round(time.time())
if deferred:
await interaction.followup.send(
content=content,
embed=embed,
ephemeral=ephemeral,
view=view
)
else:
await interaction.response.send_message(
content=content,
embed=embed,
ephemeral=ephemeral,
view=view
)
if len(last_used) >= 50:
last_used.clear()
def print_error(ctx, exception: Exception):
print('Ignoring exception in command {}:'.format(ctx.command), file=sys.stderr)
traceback.print_exception(type(exception), exception, exception.__traceback__, file=sys.stderr)