-
Notifications
You must be signed in to change notification settings - Fork 1
/
bot.py
278 lines (237 loc) Β· 9.77 KB
/
bot.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
import os
import lightbulb
import hikari
import miru
import json as jsonlib
import datetime
import re
from dotenv import load_dotenv
from actionparser import supportedActions, is_supported, parse_actions
from UserMenu import UserButton
load_dotenv()
TOKEN = os.getenv("TOKEN")
bot = lightbulb.BotApp(token=TOKEN)
err_color = hikari.Color.from_hex_code("#cf1d1d")
success_color = hikari.Color.from_hex_code('#76ff57')
date = datetime.datetime
request_date_format = "%m/%d/%Y, %H:%M %p"
miru.load(hikari.GatewayBot(TOKEN)) # Initialize miru library
@bot.command
@lightbulb.option("name", "The name of the action")
@lightbulb.command('issupported', 'Tells you if an action is supported or not.')
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
if (len(context.options.name) >= 256):
return await context.respond(hikari.Embed(title='π£ Invalid Input', description=f'There is no way that action exists anyway...\nLength: **{len(context.options.name)}/256**', color=hikari.Color.from_hex_code('#ff4747')))
support_result = is_supported(str.lower(context.options.name))
if(support_result != "NOT SUPPORTED"):
embed = hikari.Embed(title=context.options.name, description=f'β
Found **{support_result}**', color=success_color)
else:
embed = hikari.Embed(title=context.options.name, description='β Could not find the action.', color=hikari.Color.from_hex_code('#ff4747'))
await context.respond(embed)
@bot.command
@lightbulb.command('supportdump', 'Outputs a formatted version of all supported actions for use in supportdump.md')
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
attachment = hikari.File(r"./action/supportdump.md");
await context.respond(
hikari.Embed(
title="β
Success!",
description= "#οΈβ£ {amount} actions are currently supported.".format(amount=supportedActions),
color=success_color
), attachment=attachment
)
@bot.command
@lightbulb.command('about', 'About me')
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
await context.respond(
hikari.Embed(
title="π€ About this bot",
description="This bot was created by Wonk#8781.\n\nThe point of this bot is to help you check if an action is supported or not.\nYou can use the `/issupported` command to check if an action is supported or not.\nYou can use the `/supportdump` command to output a file of all supported actions.\n\nSource code: https://github.com/Wonkers0/SpigotifierBot",
color=success_color
),
)
@bot.command
@lightbulb.option("name", "The name of the action you want to be added.")
@lightbulb.command('request', 'Request a new action to be supported.')
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
action_name = str.lower(context.options.name)
support_result = is_supported(action_name)
userID = str(context.author.id)
if(support_result != "NOT SUPPORTED"):
await context.respond(
hikari.Embed(
title=f'β
**{support_result}** is already supported.',
description="You can check if an action is supported with `/issupported [action name]`",
color=err_color
)
)
else:
with open('./action/actionrequests.json', "r", encoding='utf-8') as f:
json_data = f.read()
user_requests = jsonlib.loads(json_data)
if(userID not in user_requests):
user_requests[userID] = init_request_profile(context.author.username)
requests = user_requests[userID]["requests"]
json = open('./action/actionrequests.json', 'w', encoding='utf-8')
if(action_name in requests and getDateDiff(requests[action_name], request_date_format) <= 7):
await context.respond(
hikari.Embed(
title="π΅ You've already requested this action in the past week.",
description="I'll try to get to it as soon as possible π",
color=err_color
)
)
json.write(json_data)
else:
requests[action_name] = date.now().strftime(request_date_format)
json.write(jsonlib.dumps(user_requests))
await context.respond(
hikari.Embed(
title="π Submitted your request!",
description="I'll try to get to it as soon as possible π",
color=success_color
)
)
json.close()
def init_request_profile(profileName):
print("Initializing user profile...")
return {
"name": profileName,
"requests": {}
}
@bot.command
@lightbulb.option("reparse", "Update what actions are supported locally", required=False)
# @lightbulb.decorators.add_cooldown(60, 1,lightbulb.buckets.Bucket(60, 1))
@lightbulb.command("updaterequests", "Remove any supported actions from the json file")
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
if context.options.reparse:
parse_actions()
update_reqs()
await context.respond(
hikari.Embed(
title="π Done!",
description="Updated all requests throughout all servers",
color=success_color
)
)
@bot.command
@lightbulb.option("name", "Name of the user whose requests you wish to view", required=False)
@lightbulb.command("viewrequests", "View your latest requests")
@lightbulb.implements(lightbulb.SlashCommand)
async def command(context):
with open('./action/actionrequests.json', "r", encoding='utf-8') as f:
json_data = jsonlib.loads(f.read())
foundUser = context.author
if context.options.name:
foundUser = await getUserFromName(context, context.options.name)
if foundUser == None:
return
userID = str(context.author.id) if not context.options.name else str(foundUser.id)
userName = context.author.username if not context.options.name else foundUser.username
if userID in json_data.keys():
requests = json_data[userID]["requests"]
msg = ""
if(userID not in json_data.keys() or len(requests.keys()) == 0):
await context.respond(
hikari.Embed(
title=f"π {userName} has no requests.",
description="*You can make a request using `/request [action name]`*",
color=err_color
)
)
return
print(requests)
keys = list(requests.keys())
start = len(requests) - 6 if len(requests) >= 6 else 0
for i in range(start, len(requests)):
diff = getDateDiff(requests[keys[i]], request_date_format)
msg += f"**{keys[i]}**\nRequested at {requests[keys[i]]} *({diff + ' days' if diff > 0 else 'less than a day'} ago)*\n\n\n"
await context.respond(
hikari.Embed(
title=f"{userName}'s Requests",
description=msg,
color=success_color
).set_thumbnail(foundUser.avatar_url)
)
def getDateDiff(oldDate, date_format): # 'oldDate' should be a string formatted as a date
return date.now().day - date.strptime(oldDate, date_format).day
async def getUserFromName(context, name):
foundUsers = []
exactName = re.match("#\d{4}$", name) != None
members = await context.bot.rest.fetch_members(context.guild_id)
for user in members:
if(not exactName and user.username == name):
foundUsers.append(user)
elif exactName:
if(user.username + user.discriminator == name):
return user
if len(foundUsers) == 0:
return None
if len(foundUsers) > 5:
await context.respond(
hikari.Embed(
title="π’ Too many users with this name!",
description= '\n'.join(foundUsers) + "\n\nTry adding a discriminator at the end, e.g. `foo#1234`",
color=err_color
)
)
return None
elif len(foundUsers) > 1:
view = miru.View()
for user in foundUsers:
print("Adding user: " + user.username + "#" + user.discriminator)
view.add_item(UserButton(user.username + "#" + user.discriminator))
message = await context.respond(
hikari.Embed(
title="π¨βπ©βπ¦βπ¦ Found 2 or more users with the same name",
description="Which one were you referring to?",
color=hikari.Color.from_hex_code('#2d2f34')
), components=view.build()
)
view.start(await message.message())
await view.wait()
return None
else:
return foundUsers[0]
@bot.listen(lightbulb.CommandErrorEvent)
async def catch_errors(event):
if(isinstance(event.exception, lightbulb.NotOwner)):
await event.context.respond(
hikari.Embed(
title="π¨ No Permission",
description="*You do not have permission to execute this command.*",
color=err_color
)
)
else:
await event.context.respond(
hikari.Embed(
title="π₯ Waht?!",
description="An internal error occurred :face_holding_back_tears:",
color = err_color
)
)
raise event.exception
@bot.listen(hikari.GuildMessageCreateEvent)
async def messageSent(event):
contributors = [711657398724722708, 916875577015799818, 577082051895754782]
if event.content == None:
return
if(event.author_id == 545012151081893898 and "<@1004187679493214219>" in event.content):
await bot.rest.create_message(event.channel_id, hikari.Embed(title="π€© Hey creator!", description="You are so unbelievably awesome.", color = hikari.Color.from_hex_code("#ff9933")), reply=event.message_id)
if(event.author_id in contributors and "<@1004187679493214219>" in event.content):
await bot.rest.create_message(event.channel_id, hikari.Embed(title="π Hey contributor!", description="Thanks for working on me.", color = hikari.Color.from_hex_code("#ff6699")), reply=event.message_id)
def update_reqs():
with open('./action/actionrequests.json', "r", encoding='utf-8') as f:
json_data = jsonlib.loads(f.read())
for profile in json_data.keys():
requests = json_data[profile]["requests"]
for request in requests.keys():
if is_supported(request):
del requests[request]
bot.run()
update_reqs() # Update requests on start-up