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?
Chatbot-Project-Showcase/discord_test1.9 (my code).py
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
94 lines (83 sloc)
3.65 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 random, requests | |
from discord.ext.commands import Bot | |
from discord.ext import commands | |
import wikipedia | |
BOT_PREFIX = ("?", "!") | |
# The prefixes used to call the bot commands | |
TOKEN = "NTA1ODE4MjY3MjA2NTQ5NTA0.DreJrQ.PtoiMm48GlvkLfgWOvNhEgS9xHU" | |
weather = "http://api.openweathermap.org/data/2.5/weather?appid=e7d4ecdec3fe9c8a0a471cf7091371d8&q=" | |
# Discord server token and weather API URL | |
client = Bot(command_prefix = BOT_PREFIX) | |
#the @ is a function decorator | |
@client.command(name="8ball", | |
description="Answers with 'yes/no", | |
brief="Super duper uber mega ultra psychic bot", | |
aliases=["eight_ball","eightball","8-ball"], | |
pass_context = True) | |
async def eight_ball(context): | |
# async makes the functions run asynchronously | |
possible_responses = [ | |
"That is a resounding no", | |
"It is not looking likely", | |
"Too hard to tell", | |
"It is quite possible", | |
"Definitely" | |
] | |
await client.say(random.choice(possible_responses) + ", " + context.message.author.mention) | |
@client.command() | |
async def square(number): | |
squaredValue = int(number) * int(number) | |
await client.say(str(number) + " squared is " + str(squaredValue)) | |
# .say is the command version of send_message | |
@client.command(pass_context = True) | |
async def weather(context,city): | |
weather = "http://api.openweathermap.org/data/2.5/weather?appid=e7d4ecdec3fe9c8a0a471cf7091371d8&q=" | |
# My openweathermap API key ^^^ | |
url = (weather)+(city) | |
data = requests.get(url).json() | |
# Concatenates the city/town input and url strings together and requests the data about that city | |
weatherDesc = data["weather"][0]["description"] | |
temperature = round(float(data["main"]["temp"] - (273.15)),1) | |
# Pulls the weather description from the database | |
# The pulls and converts the temperature from Kelvin to Celsius | |
weatherWriting = "The weather is " | |
temperatureWriting = "The temperature is " | |
await client.say(str(weatherWriting) + (weatherDesc) + " in " + str(city) + ", " + context.message.author.mention) | |
await client.say(str(temperatureWriting) + str(temperature)+"°C") | |
# The Discord bot sends a message with a description of the weather (mentioning the user aswell) | |
# It also sends another message with the temperature | |
@client.command() | |
async def wiki(text): | |
definition = wikipedia.summary(text,sentences=2) | |
wikiURL = wikipedia.page(text) | |
await client.say(definition) | |
await client.say(wikiURL.url) | |
# Provides the first two sentences of the desired Wikipedia page and then the URL to that page | |
@client.command() | |
async def echo(*args): | |
# *args allows for infite arguments in the input (**kwargs can also be used) | |
output = "" | |
for word in args: | |
output += word | |
output += " " | |
await client.say(output) | |
@client.event | |
async def on_message(message): | |
if message.author == client.user: | |
return | |
# stops the bot from replying to itself | |
if message.content.startswith('!hello'): | |
msg = 'Hello {0.author.mention}'.format(message) | |
await client.send_message(message.channel, msg) | |
# send_message can only be used with events and not commands | |
await client.process_commands(message) | |
# this line fixes an error where on_message takes priority over all commands | |
@client.event | |
async def on_ready(): | |
print("User Logged In:) | |
print(client.user.name) | |
print(client.user.id) | |
print("//////") | |
# prints user details to signify the bot is online | |
client.run(TOKEN) | |
# runs the bot with my discord server token |