Discord bot like .fmbot but for Rocksky.app
1
2import discord
3import os
4from pathlib import Path
5import shutil
6import http.client
7import json
8class MyClient(discord.Client):
9 async def on_ready(self):
10 print(f'Logged on as {self.user}!')
11
12 async def on_message(self, message):
13 if message.content.startswith('!link'):
14 msgcontent = message.content
15 print("message content: " + msgcontent)
16 did = msgcontent[6:]
17 print("truncated did: " + did)
18 print("creating file!")
19 txt = Path("db/" + str(message.author.id) + ".txt")
20 if txt.is_file():
21 await message.reply("I've already linked you! Run !unlink to unlink.")
22 else:
23 with open("db/" + str(message.author.id) + ".txt", 'x+') as file:
24 print("file made! writing...")
25 file.write(did)
26 print("wrote!")
27 await message.reply("Created file in db/" + str(message.author.id) + ".txt")
28 if message.content.startswith('!erasedb'):
29 if message.author.id == 1409704302238502935:
30 await message.reply("The database was deleted.")
31 shutil.rmtree("db")
32 os.makedirs("db")
33 else:
34 return
35 if message.content.startswith('!fm'):
36 with open('db/' + str(message.author.id) + ".txt") as f: didfile = f.read()
37 conn = http.client.HTTPSConnection("api.rocksky.app")
38 payload = ''
39 headers = {}
40 conn.request("GET", "/xrpc/app.rocksky.actor.getActorScrobbles?limit=1&offset=0&did=" + didfile, payload, headers)
41 res = conn.getresponse()
42 data = res.read()
43 print(data.decode("utf-8"))
44 parsed = json.loads(data.decode("utf-8"))
45 scrobbles = parsed.get("scrobbles", [])
46 if scrobbles:
47 latest = scrobbles[0]
48 embedVar = discord.Embed(title=latest['title'], color=0xff0177)
49 embedVar.add_field(name="", value=latest['artist'], inline=False)
50 embedVar.set_thumbnail(url=latest['albumArt'])
51 user = await self.fetch_user(message.author.id)
52 embedVar.set_footer(text="Requested by " + user.global_name + " (" + user.name + ") | Via rocksky.app")
53 await message.reply(embed=embedVar)
54 if message.content.startswith('!whatsmydid'):
55 with open('db/' + str(message.author.id) + ".txt") as f: didfile = f.read()
56 await message.reply("The DID linked to your account is: " + didfile)
57 if message.content.startswith("!unlink"):
58 if os.path.exists("db/" + str(message.author.id) + ".txt"):
59 os.remove("db/" + str(message.author.id) + ".txt")
60 await message.reply("Your DID file has been deleted! To relink, run !link with your DID.")
61 else:
62 await message.reply("DID file not found, and was not deleted.")
63 if message.content.startswith("!skyhelp"):
64 await message.reply("SkyEars Help\n !link [did]: Link your ATProto DID to use the bot! \n !unlink: Unlink your DID! \n !art: Shows the latest song's cover art. \n !fm: See your latest Rocksky song! \n !whatsmydid: See your DID you have linked!\n Licensed under GPL-3.0 by Freakybob Team. Not associated with Rocksky or associates.")
65 if message.content.startswith("!art"):
66 with open('db/' + str(message.author.id) + ".txt") as f: didfile = f.read()
67 conn = http.client.HTTPSConnection("api.rocksky.app")
68 payload = ''
69 headers = {}
70 conn.request("GET", "/xrpc/app.rocksky.actor.getActorScrobbles?limit=1&offset=0&did=" + didfile, payload, headers)
71 res = conn.getresponse()
72 data = res.read()
73 print(data.decode("utf-8"))
74 parsed = json.loads(data.decode("utf-8"))
75 scrobbles = parsed.get("scrobbles", [])
76 if scrobbles:
77 latest = scrobbles[0]
78 await message.reply(latest['albumArt'])
79 if message.content.startswith("!countday"):
80 with open('db/' + str(message.author.id) + ".txt") as f: didfile = f.read()
81 conn = http.client.HTTPSConnection("api.rocksky.app")
82 payload = ''
83 headers = {}
84 conn.request("GET", "/xrpc/app.rocksky.charts.getScrobblesChart?did=" + didfile, payload, headers)
85 res = conn.getresponse()
86 data = res.read()
87 print(data.decode("utf-8"))
88 parsed = json.loads(data.decode("utf-8"))
89 scrobbles = parsed.get("scrobbles", [])
90 if scrobbles:
91 latest = scrobbles[-1]
92 await message.reply("Today's scrobble count for you: " + str(latest['count']) + " (uses UTC time)")
93 if message.content.startswith("!cat"):
94 with urllib.request.urlopen("https://www.randomnumberapi.com/api/v1.0/random?min=100&max=1000&count=1") as response:
95 rndmnumsource = response.read().decode()
96 rndmnum = rndmnumsource.replace("[","").replace("]","")
97 await message.reply("https://cataas.com/cat?" + rndmnum)
98intents = discord.Intents.default()
99intents.message_content = True
100
101client = MyClient(intents=intents)
102client.run('your discord bot token here')