Permalink
Cannot retrieve contributors at this time
Name already in use
A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
DiscordBot/DiscordAPI.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
55 lines (47 sloc)
2.41 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import discord | |
import Location as location | |
TOKEN = 'NTAyMjI2MDYzNzYyMzkxMDUw.Dqk38g.KFDtyaHwMfRNK6RRUKYfpDj9L9Y' # The Discord Bot's token. | |
client = discord.Client() | |
'''Function: Takes the "message" object from the Discord channel and interprets it. | |
Input: Object(message), Output: String(msg,Advice,content[0]),Embed(embed) ''' | |
@client.event | |
async def Server(message): | |
if message.author == client.user: # To prevent the bot from replying to itself. | |
return | |
msg = "" | |
if message.content.startswith('!GoogleMaps'): | |
Run = "Maps" | |
msg = msg + str(location.Google(message,Run)) # Calls function "Google" from GoogleMaps.py | |
await client.send_message(message.channel,msg) | |
elif message.content.startswith('!Timezone'): | |
Run = "Time" | |
msg = msg + str(location.Google(message,Run)) | |
await client.send_message(message.channel, msg) # Suspends until the task is complete. If await is not used it will not return anything. | |
elif message.content.startswith('!Weather'): | |
Run = "Weather" | |
Content = location.Google(message,Run) | |
Advice = str(Content[2]) +"\n" + str(Content[3]) # Concatenates the Temp and Condition variables that are returned. | |
embed = discord.Embed() # This allows for the icon image to be inputted into discord. | |
embed.set_image(url=Content[1]) # Sets the embed image to the URL of the Icon_Url variable returned. | |
while True: | |
try: | |
await client.send_message(message.channel,Content[0],embed=embed) # Sends the msg variable that is returned (current weather) and the icon. | |
await client.send_message(message.channel,Advice) # Sends the Advice string to the channel. | |
break | |
except: # If the place doesn't exist. | |
msg = "That location doesn't exist or it is not specific enough!" | |
await client.send_message(message.channel,msg) | |
break | |
elif not msg: # To prevent an empty message being sent | |
return | |
'''Function: Detects an input message in the discord channel and then sends the argument to the "Server" function. | |
Input: Object(message), Output: Object(message)''' | |
@client.event | |
async def on_message(message): # Must be called "on_message", detects a message in Discord. | |
print(message.content) | |
await Server(message) # "await" is used to allow print("message.content") to be executed in time. | |
'''Function prints to console to show program is running''' | |
@client.event | |
async def on_ready(): # Must be called "on_ready" | |
print('Logged in as ' + client.user.name) | |
client.run(TOKEN) # To run the Discord Bot | |