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?
Python-Chatbot/connector.py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
134 lines (104 sloc)
4.17 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
""" | |
Library 'discord.py' by Rapptz used to connect to Discord. | |
Source: https://github.com/Rapptz/discord.py/ | |
""" | |
import discord | |
import asyncio | |
import wikipedia | |
import stackoverflow | |
import conversate | |
import pickle | |
import os | |
import json | |
import translatesimple | |
import hangman | |
#import weather_realtime | |
client = discord.Client() | |
################################################## | |
# Benjamin Grant | |
# Returns help string | |
def commands(): | |
commandList = "What can I do?\n" | |
commandList += "!q\n" | |
commandList += "!wiki\n" | |
commandList += "!translate\n" | |
commandList += "!flow\n" | |
commandList += "!name\n" | |
commandList += "!hangman\n" | |
return commandList | |
#Creates files critical to bot functioning | |
def initialise_files(): | |
print("Initialising critical files...") | |
names={"1":"test"} | |
brain={"GREETING": | |
["Hello!", | |
"How can I help you today %name%?", | |
"Hey %name%!", | |
"What a lovely surprise! Hello %name%."], | |
"STATE": | |
["I'm okay thanks.", | |
"Good, thank you for asking :)", | |
"All the better now I've spoken to you, %name%."], | |
"POSITIVE": | |
["Good", | |
"Great", | |
"Fantastic", | |
"Spiffing", | |
"Spectacular" | |
"Amazing"] | |
} | |
if not os.path.isfile("names.pck"): | |
print("names.pck not found. Please restart the bot, this will be automatically fixed!") | |
pickle.dump(names, open("names.pck", "wb+")) | |
if not os.path.isfile("brain.pck"): | |
print("brain.pck not found. Please restart the bot, this will be automatically fixed!") | |
pickle.dump(brain, open("brain.pck", "wb+")) | |
if not os.path.isfile("names.json"): | |
print("names.pck not found. Please restart the bot, this will be automatically fixed!") | |
json.dump(names, open("names.json", "w")) | |
print("Initialisation complete.") | |
################################################## | |
@client.event | |
# Called on login | |
async def on_ready(): | |
print('[*] ' + client.user.name + ' ('+client.user.id+') logged in') | |
initialise_files() | |
await client.change_presence(game=discord.Game(name="Chat to me!")) | |
@client.event | |
# Called whenever it receives a message | |
async def on_message(message): | |
################################################## | |
# Benjamin Grant | |
if message.author.id == client.user.id: | |
return | |
await conversate.processMessage(client, message) # Commands and functionality by Benjamin Grant may be found here | |
if message.content.lower().startswith('!help'): | |
await client.send_message(message.channel, commands()) | |
################################################## | |
################################################## | |
# Konrad Czarniecki | |
# command is !wiki search_word | |
elif message.content.lower().startswith('!wiki'): | |
await wikipedia.wikipedia(client, message) | |
# command is !flow search_phrase_here. Then '!f na' for next answer, '!f nq' for next question | |
elif message.content.lower().startswith('!flow'): | |
await stackoverflow.stackoverflow(client, message) | |
# command is !translate from_language to target_language your_query e.g !translate english to french i hate selenium | |
elif message.content.lower().startswith('!translate'): | |
await translatesimple.translate(client, message) | |
elif message.content.lower().startswith('!hangman'): | |
await hangman.hangman(client, message) | |
################################################## | |
################################################## | |
# Kelvin Mock | |
elif message.content.lower().startswith('weather'): | |
#await weather_realtime.weather(client,message) Temporarily disabled due to errors | |
pass | |
elif message.content.lower().startswith('web'): | |
await web.YouTubeBrowser(client,message) | |
elif message.content.lower().startswith('social'): | |
#await (see chat2.py for Kelvin's unfinished implementation) | |
pass | |
################################################## | |
#This token is linked to Ben's discord account/applications - sets bot account | |
client.run('NTA2NDAxMjcwMzAzNTU1NTg0.Drhmxg.w1m3iG58BbYZiRM_o17gd-kyBUE') |