Skip to content
Permalink
Browse files
fixed bugs
  • Loading branch information
elbezral committed Nov 28, 2020
1 parent 7e827ce commit 7b54a28da7a2d1947612ab6cb1ee4aac56fa49d9
Showing 1 changed file with 76 additions and 66 deletions.
@@ -1,136 +1,146 @@
import discord
from discord.ext import commands
import requests
#Code by: Mazikeen El Bezra, ID: 10832578, documentation used: https://requests.readthedocs.io/en/master/, https://discordpy.readthedocs.io/en/latest/


import discord # import discord.py to use discord's api
from discord.ext import commands #extension library for higher level interactions with the API
import requests #import request package which is used to import HTTP requests
import json

key= "&apikey=352a18cc5baa9cd34bd6fc0aa20c3401"
url="https://api.musixmatch.com/ws/1.1/"
key= "&apikey=352a18cc5baa9cd34bd6fc0aa20c3401" #API key used to access the music API
url="https://api.musixmatch.com/ws/1.1/" #music API URL

#connection to discord
#The command prefix is what the message content must contain initially to have a command invoked
client = commands.Bot(command_prefix='-')

#A decorator that registers an event to listen to.
@client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
channel = client.get_channel(779071742596874262)
await channel.send('Hello! Welcome to musikbot! send "-helpme" to see what i can do!')

#event for bot
async def on_ready(): #Called when login is successful
print('We have logged in as {0.user}'.format(client))
channel = client.get_channel(781865007003467806) #Returns a channel in which the bot is in with the given ID.
await channel.send('Hello! Welcome to musikbot! send "-helpme" to see what i can do!') #Sends a message to the previosly called channel

'''command to print the instructions message when it's called'''
@client.command(name='helpme')
async def helpme(context):
guide = discord.Embed(title='What do i do?', description="I'm a music bot!enter the following commands to use me!", color= 0xFE2E2E)
guide = discord.Embed(title='What do i do?', description="I'm a music bot! Enter the following commands to use me!", color= 0xFE2E2E)
guide.add_field(name='-lyrics', value='get first 30% of lyrics for a song!', inline='False')
guide.add_field(name='-top_songs', value='get the top chart songs!', inline=False)
guide.add_field(name='-top_artists', value='get the top chart artists!', inline=False)
guide.add_field(name='-artists_recc', value='reccomend you artists similar to the one you enter!', inline=False)
await context.message.channel.send(embed=guide)


'''lyrics command'''
@client.command(name='lyrics')
async def lyrics(context):
await context.message.channel.send("Welcome to the lyrics search! What's the name of the song?")

@client.event
async def on_message(text):
if text.author.id != 781194607157313537:
song= text.content
async def on_message(text): #Called when a Message is created and sent.
if text.author.id != 781194607157313537: #checks user's id, uses message input only if it's sent from someone other than the bot
song= text.content #saves message for it to be used as part of the music api method call
await context.message.channel.send("What's the name of the artist? ")
await client.process_commands(text)
await client.process_commands(text) #this function processes the command that has been registered to the bot


@client.event
async def on_message(message):
if message.author.id != 781194607157313537 and message.content.startswith('-')==False:
artist= message.content
api= url + "matcher.lyrics.get" + "?format=json&callback=callback" + "&q_artist=" + artist + "&q_track=" + song + key
request = requests.get(api)
data = request.json()
lyricsss= data['message']['body']['lyrics']['lyrics_body']
return await context.message.channel.send(lyricsss)
@client.event
async def on_message(message): #Called when a Message is created and sent.
if message.author.id != 781194607157313537 and message.content.startswith('-')==False: #checks that message input isnt taken from bot and that user isn't calling another command
artist= message.content #saves message for it to be used as part of the music api method cal
api= url + "matcher.lyrics.get" + "?format=json&callback=callback" + "&q_artist=" + artist + "&q_track=" + song + key #calls music api method
request = requests.get(api) #sends a GET request to the specified url to get data from server
data = request.json() #parse it as a JSON object
lyricsss= data['message']['body']['lyrics']['lyrics_body'] #access object lyrics and return the required object value
return await context.message.channel.send(lyricsss) #this function processes the command that has been registered to the bot

await client.process_commands(message)
await client.process_commands(message)#this function processes the command that has been registered to the bot

'''command to return top songs '''
@client.command(name='top_songs')
async def top_songs(context):
await context.message.channel.send("Hello! Welcome to top songs chart! how many songs would you like to view?")

@client.event
async def on_message(message):
async def on_message(message): #Called when a Message is created and sent.
if message.author.id != 781194607157313537:
amount= message.content
await context.message.channel.send("Please enter what country's charts would you like to view (i.e: us for america, uk for united kingdom, eg for egypt etc.")
amount= message.content #saves message for it to be used as part of the music api method cal
await context.message.channel.send("Please enter what country's charts would you like to view (i.e: us for america, uk for united kingdom, eg for egypt etc.)")

@client.event
async def on_message(text):
if text.author.id != 781194607157313537 and text.content.startswith('-')==False:
country= text.content
charturl= url+ "chart.tracks.get"+ "?format=json&callback=callback" + "page=1&page_size=" + amount + "&country=" + country + key
request = requests.get(charturl)
data = request.json()
async def on_message(text): #Called when a Message is created and sent.
if text.author.id != 781194607157313537 and text.content.startswith('-')==False: #checks that message input isnt taken from bot and that user isn't calling another command
country= text.content #saves message for it to be used as part of the music api method cal
charturl= url+ "chart.tracks.get"+ "?format=json&callback=callback" + "page=1&page_size=" + amount + "&country=" + country + key #calls music api method
request = requests.get(charturl) #sends a GET request to the specified url to get data from server
data = request.json() #parse it as a JSON object
##for loop used to access the 'track list' object and return two object values, the song name and artist name
for i in data['message']['body']['track_list']:
await context.message.channel.send("Song title: "+i['track']['track_name']+" -> Artist: "+i['track']['artist_name'])
await client.process_commands(text)
await client.process_commands(text) #this function processes the command that has been registered to the bot


''' command to return top artists'''
@client.command(name='top_artists')
async def top_artists(context):
await context.message.channel.send("Hello! Welcome to top artists chart! how many artists would you like to view?")

@client.event
async def on_message(message):
async def on_message(message): #Called when a Message is created and sent.
if message.author.id != 781194607157313537:
amount= message.content
amount= message.content #saves message for it to be used as part of the music api method cal
await context.message.channel.send("Please enter what country's charts would you like to view (i.e: us for america, uk for united kingdom, eg for egypt etc.")

@client.event
async def on_message(text):
if text.author.id != 781194607157313537 and text.content.startswith('-')==False:
country= text.content
charturl= url+ "chart.artists.get"+ "?format=json&callback=callback" + "page=1&page_size=" + amount + "&country=" + country + key
request = requests.get(charturl)
data = request.json()
for i in data['message']['body']['artist_list']:
async def on_message(text): #Called when a Message is created and sent.
if text.author.id != 781194607157313537 and text.content.startswith('-')==False: #checks that message input isnt taken from bot and that user isn't calling another command
country= text.content #saves message for it to be used as part of the music api method cal
charturl= url+ "chart.artists.get"+ "?format=json&callback=callback" + "page=1&page_size=" + amount + "&country=" + country + key #calls music api method
request = requests.get(charturl) #sends a GET request to the specified url to get data from server
data = request.json() #parse it as a JSON object
# #for loop used to access the 'artist list' object and return artist names
for i in data['message']['body']['artist_list']:
await context.message.channel.send(i['artist']['artist_name'])
await client.process_commands(text)
await client.process_commands(text) #this function processes the command that has been registered to the bot

'''command to return reccomended artists based on chosen artist'''
@client.command(name='artists_recc')
async def artist_recc(context):
await context.message.channel.send("Hello! Welcome to artist reccomendation! Tell me who's your inspiration!")

@client.event
async def on_message(message):
if message.author.id != 781194607157313537:
artist= message.content
artistsearch= url + 'artist.search' + '?format=json&callback=callback' + '&q_artist=' + artist + key
request = requests.get(artistsearch)
data = request.json()
async def on_message(message): #Called when a Message is created and sent.
if message.author.id != 781194607157313537:
artist= message.content #saves message for it to be used as part of the music api method cal
artistsearch= url + 'artist.search' + '?format=json&callback=callback' + '&q_artist=' + artist + key #calls music api method
request = requests.get(artistsearch) #sends a GET request to the specified url to get data from server
data = request.json() #parse it as a JSON object
# #retrieves the artist id from json and saves it as a variable
artist_id=str(data['message']['body']['artist_list'][0]['artist']['artist_id'])
await context.message.channel.send('How many reccomendations would you like?')

@client.event
async def on_message(text):
if text.author.id != 781194607157313537 and text.content.startswith('-')==False:
number= text.content
related= url + 'artist.related.get'+ '?format=json&callback=callback' + '&artist_id=' + artist_id + '&page_size=' +number + key
request = requests.get(related)
data = request.json()
async def on_message(text): #Called when a Message is created and sent.
if text.author.id != 781194607157313537 and text.content.startswith('-')==False: #checks that message input isnt taken from bot and that user isn't calling another command
number= text.content #saves message for it to be used as part of the music api method cal
related= url + 'artist.related.get'+ '?format=json&callback=callback' + '&artist_id=' + artist_id + '&page_size=' +number + key #calls music api method
request = requests.get(related) #sends a GET request to the specified url to get data from server
data = request.json() #parse it as a JSON object
##for loop used to access the 'artist list' object and return its object values
for i in data['message']['body']['artist_list']:
await context.message.channel.send(i['artist']['artist_name'])
await context.message.channel.send("would you like their rating? enter yes or no")


@client.event
async def on_message(reply):
if text.author.id != 781194607157313537 and text.content.startswith('-')==False:
twit= reply.content
async def on_message(reply): #Called when a Message is created and sent.
if text.author.id != 781194607157313537 and text.content.startswith('-')==False: #checks that message input isnt taken from bot and that user isn't calling another command
twit= reply.content #saves message for it to be used as part of the music api method cal
if twit=='yes':
#loop to access 'artist list' object and return the rating
for i in data['message']['body']['artist_list']:
await context.message.channel.send(i['artist']['artist_name']+" is ranked " + str(i['artist']['artist_rating']))
else:
pass
await client.process_commands(reply)
await client.process_commands(reply) #this function processes the command that has been registered to the bot

#test

client.run('NzgxMTk0NjA3MTU3MzEzNTM3.X76GLA.39vqlezgZKW9gR8hGCTYOFy8ing')
client.run('NzgxMTk0NjA3MTU3MzEzNTM3.X76GLA.39vqlezgZKW9gR8hGCTYOFy8ing') #run the bot with login token

0 comments on commit 7b54a28

Please sign in to comment.