-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
94 lines (75 loc) · 3.76 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
import discord
import responses
import pylast
from responses import get_most_recent_track
from color_pick import get_dominant_color, rgb_to_hex
from key import API_KEY, API_SECRET, TOKEN
network = pylast.LastFMNetwork(api_key=API_KEY, api_secret=API_SECRET)
async def send_message(message, user_message, is_private):
try:
response = responses.handle_response(user_message)
await message.author.send(response) if is_private else await message.channel.send(response)
except Exception as e:
print(e)
def run_bot():
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents = intents)
@client.event
async def on_ready():
print(f'{client.user} is now running!')
@client.event
async def on_message(message):
if message.author == client.user:
return
username = str(message.author)
user_message = str(message.content).strip()
channel = str(message.channel)
print(f'{username} said in #{channel}: {user_message}')
if user_message.startswith('?'):
user_message = user_message[1:]
await send_message(message, user_message, is_private=True)
elif user_message:
await send_message(message, user_message, is_private=False)
else:
await message.channel.send("Please provide a valid message.")
if message.content.startswith('recent'):
lastfm_username = 'ptrn23'
recent_track = get_most_recent_track(lastfm_username)
cover_image_url = recent_track.track.get_cover_image()
embed = discord.Embed(
title=f'{recent_track.track}',
color=0xff0000)
embed.add_field(name='Artist', value=recent_track.track.artist.name, inline=True)
embed.add_field(name='Album', value=recent_track.album, inline=True)
embed.add_field(name='Playback Date', value=recent_track.playback_date, inline=False)
embed.set_thumbnail(url=cover_image_url)
if cover_image_url:
embed.set_thumbnail(url=cover_image_url)
dominant_color = get_dominant_color(cover_image_url)
if dominant_color:
embed.color = discord.Color(int(rgb_to_hex(dominant_color).strip('#'), 16))
else:
embed.set_footer(text="Cover image not available.")
await message.channel.send(embed=embed)
elif message.content.startswith('surpass'):
lastfm_username = 'ptrn23'
recent_track = get_most_recent_track(lastfm_username)
recent_track.track.username = lastfm_username
cover_image_url = recent_track.track.get_cover_image()
count = recent_track.track.get_userplaycount()
embed = discord.Embed(
title=f'{recent_track.track} has now surpassed {count} scrobbles!',
description=f"— It is the first song by the artist to surpass this milestone in {lastfm_username}'s charts!",
color=0xff0000)
embed.add_field(name='Milestone Date', value=recent_track.playback_date, inline=False)
embed.set_thumbnail(url=cover_image_url)
if cover_image_url:
embed.set_thumbnail(url=cover_image_url)
dominant_color = get_dominant_color(cover_image_url)
if dominant_color:
embed.color = discord.Color(int(rgb_to_hex(dominant_color).strip('#'), 16))
else:
embed.set_footer(text="Cover image not available.")
await message.channel.send(embed=embed)
client.run(TOKEN)