sidBot/main.py

87 lines
2.7 KiB
Python
Raw Normal View History

2021-12-18 16:55:54 +00:00
import os
import discord
2021-12-18 17:36:06 +00:00
import requests
import json
2021-12-18 16:55:54 +00:00
import random
client = discord.Client()
my_secret = os.environ['TOKEN']
2021-12-18 17:36:06 +00:00
# arrays containing answers
2021-12-18 16:55:54 +00:00
xmasAnswers = ['Happy Chrismis!', 'Its Chrismin!', 'Merry Chrisis!', 'Merry Chrysler!']
coinflip = ['Heads', 'Not Sonic lol (Tails..)']
2021-12-18 17:36:06 +00:00
# gets a quote from zenquotes.io
def get_quote():
response = requests.get('https://zenquotes.io/api/random')
json_data = json.loads(response.text)
quote = json_data[0]['q'] + ' -' + json_data[0]['a']
return(quote)
2021-12-18 21:30:11 +00:00
# gets a meme from Huge RedditMemesAPI
def get_meme():
r = requests.get('https://memes.blademaker.tv/api?lang=en')
res = r.json()
title = res['title']
ups = res['ups']
downs = res['downs']
sub = res['subreddit']
meme = discord.Embed(title = f'{title}\nSubreddit: {sub}')
meme.set_image(url = res['image'])
meme.set_footer(text=f"👍:{ups}")
return m
2021-12-18 17:36:06 +00:00
2021-12-18 16:55:54 +00:00
# when bot is ready
@client.event # Register an event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
2021-12-18 17:36:06 +00:00
2021-12-18 17:08:55 +00:00
# bot senses a message & responds
2021-12-18 16:55:54 +00:00
@client.event
async def on_message(message):
# if message is from bot, return nothing
if message.author == client.user:
return
# user sends "!hello", bot responds w/ "Hello!"
if message.content.lower().startswith('!hello'):
await message.channel.send('Hello!')
2021-12-18 17:19:54 +00:00
# user sends "!ping", bot responds w/ "Pong" + bot latency and a gif
2021-12-18 16:55:54 +00:00
elif message.content.lower().startswith('!ping'):
await message.channel.send(f'Pong :ping_pong: (Bot latency: **{round(client.latency * 1000)}ms**)')
2021-12-18 17:19:54 +00:00
await message.channel.send(file=discord.File('resources/pingpong.gif'))
2021-12-18 16:55:54 +00:00
2021-12-18 17:36:06 +00:00
# user sends "!help", bot sends commandsfile
2021-12-18 16:55:54 +00:00
elif message.content.lower().startswith("!help"):
await message.channel.send(file=discord.File("commands.md"))
2021-12-18 17:36:06 +00:00
# user sends "!coinflip", bot returns result
2021-12-18 16:55:54 +00:00
elif message.content.lower().startswith("!coinflip"):
await message.channel.send(random.choice(coinflip))
2021-12-18 17:36:06 +00:00
# user sends "!github", bot responds w/ my GitHub profile
2021-12-18 16:55:54 +00:00
elif message.content.lower().startswith("!github"):
await message.channel.send('https://github.com/SindreKjelsrud')
# someone writes "merry christmas", bot responds w/ legendary vine quote
elif "merry christmas" in message.content.lower():
await message.channel.send(random.choice(xmasAnswers) + ':santa:')
2021-12-18 17:36:06 +00:00
# user sends "!inspire", bot inspires user
elif message.content.lower().startswith("!inspire"):
quote = get_quote()
await message.channel.send(quote)
2021-12-18 21:30:11 +00:00
# user sends "!plsmeme", bot sends meme from random subreddit
elif message.content.lower().startswith("!plsmeme"):
meme = get_meme()
await message.channel.send(embed = meme)
2021-12-18 16:55:54 +00:00
# run bot
2021-12-18 17:19:54 +00:00
client.run(my_secret)