-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathutils.py
160 lines (129 loc) · 4.8 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
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
import discord
from discord.ext import commands
from asyncio import TimeoutError
from rcon.instances import Instance
GAME_IMAGES = {
"squad": "https://media.discordapp.net/attachments/729998051288285256/765669813338374144/unknown.png",
"ps": "https://media.discordapp.net/attachments/779050069252243477/790620643958718504/PostScriptum.png",
"btw": "https://media.discordapp.net/attachments/768095907102720051/772495252850212884/2Q.png"
}
SIMPLIFIED_CLASS_NAMES = {
int: "number",
str: "text",
discord.TextChannel: "text channel"
}
def get_name(user):
return user.nick if user.nick else user.name
class Config:
def __init__(self, path: str = "config.txt"):
self.path = path
self.update()
def update(self):
self.config = {}
with open(self.path, "r", encoding="utf-8") as f:
for line in f.readlines():
line = line.strip("\n").split("=", 1)
if len(line) == 2:
self.config[line[0]] = line[1]
def get(self, key: str, alternative_value=None, update_config=False):
if update_config:
self.update()
try:
res = self.config[key]
except KeyError:
res = alternative_value
finally:
return res
def get_player_input_type(player: str):
try: # Can the value be an ID?
int(player)
except: # Value can't be converted to an int, must be a name
return "name"
else: # Value can be converted to an int, can be an ID
if len(str(player)) == 17: # Value is same length as steam64id
return "steam64id"
elif len(str(player)) <= 3: # Value can be a squadid
return "squadid"
else: # Can't be steam64id or squadid, must be a name
return "name"
def add_empty_fields(embed):
try: fields = len(embed._fields)
except AttributeError: fields = 0
if fields > 3:
empty_fields_to_add = 3 - (fields % 3)
if empty_fields_to_add in [1, 2]:
for i in range(empty_fields_to_add):
embed.add_field(name=" ", value=" ") # These are special characters that can not be seen
return embed
def base_embed(instance, title: str = None, description: str = None, color=None):
if isinstance(instance, int):
instance = Instance(instance)
embed = EmbedMenu(title=title, description=description, color=color)
embed.set_author(name=instance.name, icon_url=GAME_IMAGES[instance.game])
return embed
class EmbedMenu(discord.Embed):
def add_option(self, emoji, title, description):
option = {
'emoji': str(emoji),
'name': str(title),
'value': str(description)
}
try:
self._options.append(option)
except AttributeError:
self._options = [option]
def remove_option(self, index):
try:
del self._options[index]
except (AttributeError, IndexError):
pass
def set_option_at(self, index, *, emoji, title, description):
try:
option = self._option[index]
except (TypeError, IndexError, AttributeError):
raise IndexError('option index out of range')
option['emoji'] = str(emoji)
option['name'] = str(title)
option['value'] = str(description)
return self
def insert_option_at(self, index, *, emoji, title, description):
option = {
'emoji': str(emoji),
'name': str(title),
'value': str(description)
}
try:
self._options.insert(index, option)
except AttributeError:
self._options = [option]
return self
def clear_options(self):
try:
self._options.clear()
except AttributeError:
self._options = []
async def run(self, ctx, timeout=60):
emojis = []
self._fields = []
for i, option in enumerate(self._options):
field = {
'inline': True,
'name': option["emoji"] + " " + option["name"],
'value': option["value"]
}
self._fields.append(field)
self._fields = add_empty_fields(self)._fields
emojis = [option['emoji'] for option in self._options]
msg = await ctx.send(embed=self)
for emoji in emojis:
await msg.add_reaction(emoji)
def check(reaction, user):
return str(reaction.emoji) in emojis and user == ctx.author
try:
reaction, user = await ctx.bot.wait_for('reaction_add', timeout=timeout, check=check)
except TimeoutError:
await msg.clear_reactions()
return None
else:
await msg.clear_reactions()
return reaction