-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbot.py
More file actions
259 lines (215 loc) · 9.08 KB
/
bot.py
File metadata and controls
259 lines (215 loc) · 9.08 KB
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
# Created by Deltaion Lee (MCMi460) on Github
import discord
import asyncio
from discord.ext import commands
from PIL import Image
import re
import io
import json
import typing
import requests
import love
import love3
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
bot = commands.Bot(command_prefix = '.', intents = intents, case_insensitive = True)
NRP_Guild = 1012066992817193001
# https://www.geeksforgeeks.org/check-if-an-url-is-valid-or-not-using-regular-expression/
regex = ('((http|https)://)(www.)?' +
'[a-zA-Z0-9@:%._\\+~#?&//=]' +
'{2,256}\\.[a-z]' +
'{2,6}\\b([-a-zA-Z0-9@:%' +
'._\\+~#?&//=]*)'
)
pattern = re.compile(regex)
@bot.event
async def on_ready():
print('Logged in as %s (%s)' % (bot.user,bot.user.id))
print('Present in %s servers.' % len(bot.guilds))
print('------')
await bot.change_presence(
status = discord.Status.online,
activity = discord.Activity(
type = discord.ActivityType.listening,
name = 'your game suggestions'
)
)
if not discord.utils.get(bot.guilds, id = NRP_Guild):
raise Exception('expected to be in Nintendo Rich Presence guild')
@bot.event
async def on_member_join(member):
guild = member.guild
if guild.id == NRP_Guild and guild.system_channel is not None:
await guild.system_channel.send(
'Welcome to %s, %s! If you want a quick tutorial on using NSO-RPC or 3DS-RPC, go to <#1126310378981294211>.\nOr, if you\'d like a special role (like 3DS Updates or NSO Updates), see <id:customize>!' % (
member.guild.name, member.mention
)
)
@bot.event
async def on_message(message):
await bot.process_commands(message)
# From @AbstractUmbra
@bot.command()
@commands.guild_only()
@commands.is_owner()
async def sync(ctx) -> None:
synced = await ctx.bot.tree.sync()
await ctx.send(f'Synced {len(synced)} commands globally')
synced = await ctx.bot.tree.sync(guild = discord.Object(id = NRP_Guild))
await ctx.send(f'Synced {len(synced)} commands to NRP_Guild')
@bot.tree.command(guild = discord.Object(id = NRP_Guild))
@discord.app_commands.checks.cooldown(1, 60)
async def create(interaction:discord.Interaction, title_id:str, short:str, long:str, publisher:str, icon_url:str):
'''
Create a new game addition application.
'''
channel = bot.get_channel(1123524711742193694)
try:
if channel.id != interaction.channel.id:
raise Exception('please send request commands in %s' % channel.mention)
title_id = title_id.upper()
if not isHex(title_id):
raise Exception('not real title ID!')
short, long, publisher, icon_url = tuple(map(str, (short, long, publisher, icon_url)))
if not re.search(pattern, icon_url):
raise Exception('not a real URL!')
startswith = (
'https://media.discordapp.net/attachments/1123524711742193694/',
'https://cdn.discordapp.com/attachments/1123524711742193694/',
'https://github.com/',
'https://raw.githubusercontent.com/',
)
fail = True
for link in startswith:
if icon_url.startswith(link):
fail = False
if fail:
raise Exception('please use an image URL uploaded to %s. After uploading, right click (or press share on mobile) to copy the attachment link!' % channel.mention)
meta = 'Request created by %s.\n# `%s`.\n**Title ID**: %s\n**Short**: %s\n**Long**: %s\n**Publisher**: %s\n**Icon URL**: <%s>' % (interaction.user.mention, short, title_id, short, long, publisher, icon_url)
await interaction.response.send_message(
meta,
ephemeral = False
)
state = ''
state += '\n1. Passed all checks'
await interaction.edit_original_response(content = meta + state)
ret = {
'short': short,
'long': long,
'publisher': publisher,
'imageID': title_id,
}
ret = io.BytesIO(bytes(json.dumps(ret), 'utf-8'))
state += '\n2. Formatted JSON file'
await interaction.edit_original_response(content = meta + state)
image_data = requests.get(icon_url).content
image = Image.open(io.BytesIO(image_data))
image = image.resize((48, 48))
image_data = io.BytesIO()
image.save(image_data, format = 'PNG')
image_data.seek(0)
state += '\n3. Formatted icon PNG'
await interaction.edit_original_response(content = meta + state)
files = (
discord.File(fp = image_data, filename = '%s.png' % title_id),
discord.File(fp = ret, filename = '%s.txt' % title_id),
)
state += '\n4. Created Discord files'
await interaction.edit_original_response(content = meta + state)
message = await (await interaction.original_response()).fetch()
thread = discord.utils.get(channel.threads, name = title_id)
if not thread:
thread = await channel.create_thread(name = title_id, message = message)
state += '\n5. Created Discord thread (%s)' % thread.mention
else:
state += '\n5. Grabbed open Discord thread (%s)' % thread.mention
await thread.send(meta)
await interaction.edit_original_response(content = meta + state)
upload = await thread.send('(%s)' % interaction.user.mention, files = files)
state += '\n6. Uploaded files (%s)' % upload.jump_url
await interaction.edit_original_response(content = meta + state)
await asyncio.sleep(10)
await interaction.edit_original_response(content = meta)
except Exception as e:
try:
await interaction.response.send_message(
'Exception encountered!\n%s' % e,
ephemeral = True
)
except:
await interaction.channel.send('Exception encountered!\n%s' % e)
@create.error
async def create_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError):
if isinstance(error, discord.app_commands.CommandOnCooldown):
await interaction.response.send_message(str(error), ephemeral = True)
@bot.tree.command()
@discord.app_commands.checks.cooldown(1, 60)
async def get_title_id(interaction:discord.Interaction, friendcode:int):
'''
Gets the current title ID that a user is playing
'''
try:
love.convertFriendCodeToPrincipalId(friendcode)
headers = {
'User-Agent': '3DS-RPC/0.31',
}
ret = requests.get('https://3dsrpc.com/api/user/%s' % friendcode, headers = headers).json()
if ret['Exception']:
raise Exception(ret['Exception'])
titleID = ret['User'].get('Presence', {}).get('titleID', None)
updateID = ret['User'].get('Presence', {}).get('updateID', None)
await interaction.response.send_message(
'`%s`: **%s**\nPlaying: `%s`\nUpdate: `%s`' % (ret['User']['friendCode'], ret['User']['username'], hex(int(titleID)).replace('0x', '').zfill(16) if titleID else 'Nothing!', updateID if updateID else 'None!'),
ephemeral = False
)
except Exception as e:
try:
await interaction.response.send_message(
'Exception encountered!\n%s' % e,
ephemeral = False
)
except:
await interaction.channel.send('Exception encountered!\n%s' % e)
@get_title_id.error
async def get_title_id_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError):
if isinstance(error, discord.app_commands.CommandOnCooldown):
await interaction.response.send_message(str(error), ephemeral = True)
@bot.tree.command()
@discord.app_commands.checks.cooldown(1, 30)
async def get_idbe_data(interaction:discord.Interaction, title_id:str):
'''
Gets title information of a title ID from the IDBE database
'''
try:
title_id = title_id.replace('0x', '')
if not isHex(title_id):
raise Exception('not real title ID!')
ret, image = love3.getTitleInfo('0x' + title_id)
files = (
discord.File(fp = image, filename = '%s.png' % title_id),
)
await interaction.response.send_message(
content = ret,
files = files,
)
except Exception as e:
await interaction.response.send_message(
'Exception encountered!\n%s' % e,
ephemeral = True,
)
@get_idbe_data.error
async def get_idbe_data_error(interaction: discord.Interaction, error: discord.app_commands.AppCommandError):
if isinstance(error, discord.app_commands.CommandOnCooldown):
await interaction.response.send_message(str(error), ephemeral = True)
def isHex(string):
for char in string:
if not char.lower() in '0123456789abcdef':
return False
return string and len(string) == 16
async def main():
async with bot:
await bot.load_extension('NSO-IconDB.server.bot_extension')
from private import token
await bot.start(token)
asyncio.run(main())