This repository has been archived on 2025-02-16. You can view files and clone it, but cannot push or open issues or pull requests.
sidBot/main.py

176 lines
6 KiB
Python
Raw Normal View History

2021-12-18 17:55:54 +01:00
import os
import discord
2021-12-18 18:36:06 +01:00
import requests
#from requests import Request, Session
#from requests.exceptions import ConnectionError, Timeout, TooManyRedirects
2021-12-18 18:36:06 +01:00
import json
2021-12-18 17:55:54 +01:00
import random
from keep_alive import keep_alive
2021-12-18 17:55:54 +01:00
client = discord.Client()
my_secret = os.environ['TOKEN']
2021-12-18 18:36:06 +01:00
# arrays containing answers
2021-12-18 17:55:54 +01:00
xmasAnswers = ['Happy Chrismis!', 'Its Chrismin!', 'Merry Chrisis!', 'Merry Chrysler!']
coinflip = ['Heads', 'Not Sonic lol (Tails..)']
2021-12-18 23:11:02 +01:00
dogTitles = ['Who let the dogs out?', 'woof', 'Whos a good boy!', 'meow', 'Mr. GoodBoy']
2021-12-18 17:55:54 +01:00
2021-12-18 18:36:06 +01:00
2021-12-18 23:11:02 +01:00
## APIs
2021-12-18 18:36:06 +01: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 22:30:11 +01:00
# gets a meme from Huge RedditMemesAPI
def get_meme():
2021-12-18 23:11:02 +01:00
response = requests.get('https://memes.blademaker.tv/api?lang=en')
res = response.json()
2021-12-18 22:30:11 +01:00
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 meme
2021-12-18 18:36:06 +01:00
2021-12-18 23:11:02 +01:00
# gets a dog from Dog API
def get_dog():
response = requests.get('https://dog.ceo/api/breeds/image/random')
res = response.json()
dog = discord.Embed(title = random.choice(dogTitles))
dog.set_image(url = res['message'])
return dog
# crypto API
# def get_crypto():
# url = 'https://pro-api.coinmarketcap.com/v1/cryptocurrency/listings/latest'
# parameters = {
# 'start':'1',
# 'limit':'5000',
# 'convert':'USD'
# }
# headers = {
# 'Accepts': 'application/json',
# 'X-CMC_PRO_API_KEY': '5c96c638-c66f-4ddb-9c5d-7a77d5d83258',
# }
# session = Session()
# session.headers.update(headers)
# try:
# response = session.get(url, params=parameters)
# #res = response.json()
# #name = res['name']
# #id = res['id']
# #tags = res['tags']
# #crypto = discord.Embed(title = f'{id}\nMiniable: {tags}')
# json_data = json.loads(response.text)
# return(json_data)
# except (ConnectionError, Timeout, TooManyRedirects) as e:
# return(e)
2021-12-19 11:55:41 +01:00
## BOT READY
2021-12-18 17:55:54 +01:00
@client.event # Register an event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
2021-12-19 11:55:41 +01:00
activity = discord.Game(name = "!help") # sets bot activity
await client.change_presence(status = discord.Status.online, activity = activity)
2021-12-18 17:55:54 +01:00
2021-12-18 18:36:06 +01:00
2021-12-18 23:11:02 +01:00
## MESSAGE RESPONSES
2021-12-18 18:08:55 +01:00
# bot senses a message & responds
2021-12-18 17:55:54 +01: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 18:19:54 +01:00
# user sends "!ping", bot responds w/ "Pong" + bot latency and a gif
2021-12-18 17:55:54 +01: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 18:19:54 +01:00
await message.channel.send(file=discord.File('resources/pingpong.gif'))
2021-12-18 17:55:54 +01:00
2021-12-18 18:36:06 +01:00
# user sends "!coinflip", bot returns result
2021-12-18 17:55:54 +01:00
elif message.content.lower().startswith("!coinflip"):
await message.channel.send(random.choice(coinflip))
2021-12-18 18:36:06 +01:00
# user sends "!github", bot responds w/ my GitHub profile
2021-12-18 17:55:54 +01: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 18:36:06 +01:00
# user sends "!inspire", bot inspires user
elif message.content.lower().startswith("!inspire"):
quote = get_quote()
await message.channel.send(quote)
2021-12-18 22:30:11 +01: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 23:11:02 +01:00
# user sends "!plsdog", bot sends picture of dog from Dog API
elif message.content.lower().startswith("!plsdog"):
dog = get_dog()
await message.channel.send(embed = dog)
# user sends "!invbot", bot responds w/ invite link for bot
elif message.content.lower().startswith("!invbot"):
embedVar = discord.Embed(color=0x7B64FF)
embedVar.add_field(name="Bot Invite Link", value="https://discord.com/oauth2/authorize?client_id=921786935662477412&permissions=274881309760&scope=bot", inline=False)
await message.channel.send(embed=embedVar)
# crypto - *not working*
# elif message.content.lower().startswith("!crypto"):
# crypto = get_crypto()
# await message.channel.send(crypto)
# user sends "!help", bot sends commandsfile
elif message.content.lower().startswith("!help"):
embedVar = discord.Embed(title="List of sidBots features/commands:", description="--------------------------------", color=0x7B64FF)
embedVar.add_field(name="!help", value="List of commands", inline=False)
embedVar.add_field(name="!hello", value="Bot responds with 'Hello!'", inline=False)
embedVar.add_field(name="!ping", value="Bot responds with 'Pong!' and botlatency + a gif from Ping Pong The Animation", inline=False)
embedVar.add_field(name="!github", value="Flexes github link", inline=False)
embedVar.add_field(name="!coinflip", value="Heads or Tails!", inline=False)
embedVar.add_field(name="merry christmas", value="Someone writes 'merry christmas' and bot responds w/ legendary vine quote selected from an array", inline=False)
embedVar.add_field(name="!inspire", value="Bot inspires user with a quote from zenquotes.io", inline=False)
embedVar.add_field(name="!plsmeme", value="Bot supplies with premium memes from subreddits across Reddit from Huge RedditMemesAPI", inline=False)
embedVar.add_field(name="!plsdog", value="Bot supplies with pictures of cute doggos across the whole internet through Dog API", inline=False)
embedVar.add_field(name="!invbot", value="Bot sends invite link for itself", inline=False)
await message.channel.send(embed=embedVar)
# keeps bot alive
keep_alive()
2021-12-18 17:55:54 +01:00
# run bot
2021-12-18 23:11:02 +01:00
client.run(my_secret)