Code Cleanup
This commit is contained in:
parent
d2622e25e8
commit
fa34f8510d
|
@ -1,3 +1,8 @@
|
|||
'''
|
||||
A module that sits between the API and the cog
|
||||
and provides functions for easy communication
|
||||
with the inspirobot API
|
||||
'''
|
||||
import requests
|
||||
from util import logging
|
||||
|
||||
|
|
|
@ -1,9 +1,14 @@
|
|||
'''
|
||||
A module that sits between the API and the cog
|
||||
and provides functions for easy communication
|
||||
with the NASA API
|
||||
'''
|
||||
import requests
|
||||
from util import config, logging
|
||||
|
||||
api_key = config.settings["api_keys"]["nasa"]
|
||||
|
||||
def image_of_the_day():
|
||||
def image_of_the_day() -> str:
|
||||
url = f"https://api.nasa.gov/planetary/apod?api_key={api_key}"
|
||||
response = requests.get(url)
|
||||
|
||||
|
|
16
api/steam.py
16
api/steam.py
|
@ -1,16 +1,16 @@
|
|||
'''
|
||||
A module that sits between the API and the cog
|
||||
and provides functions for easy communication
|
||||
with the Steam API
|
||||
'''
|
||||
import requests
|
||||
import json
|
||||
from util import logging
|
||||
from util import logging, config
|
||||
|
||||
# This module acts as an interface between the bot and the Steam API
|
||||
# The responses of the API are formatted and returned in a standard format
|
||||
|
||||
with open("config.json", "r") as key_file:
|
||||
json = json.load(key_file)
|
||||
key = json["api_keys"]["steam"]
|
||||
key = config.settings["api_keys"]["steam"]
|
||||
|
||||
# Returns a (hopefully) valid Steam API URL
|
||||
def assemble_url(interface_name, method_name, version):
|
||||
def assemble_url(interface_name: str, method_name: str, version: int) -> str:
|
||||
return f"http://api.steampowered.com/{interface_name}/{method_name}/v{version}/?key={key}&format=json"
|
||||
|
||||
def get_json_request_response(url: str):
|
||||
|
|
9
bot.py
9
bot.py
|
@ -6,6 +6,7 @@ from util import logging, config
|
|||
|
||||
# Bot URL https://discordapp.com/api/oauth2/authorize?client_id=657709911337074698&permissions=314432&scope=bot
|
||||
|
||||
# The config must be loaded before everything else
|
||||
total = 0
|
||||
failed = 0
|
||||
|
||||
|
@ -19,13 +20,12 @@ logging.info("Finished loading configs.")
|
|||
logging.info(f"Total {total}, Failed {failed}\n")
|
||||
|
||||
|
||||
|
||||
client = commands.Bot(command_prefix=config.settings["prefix"], case_insensitive=True)
|
||||
|
||||
|
||||
@client.command()
|
||||
@commands.is_owner()
|
||||
async def load(ctx, extension):
|
||||
async def load(ctx : commands.Context, extension : str):
|
||||
try:
|
||||
client.load_extension(f"cogs.{extension}")
|
||||
await ctx.message.add_reaction("👍")
|
||||
|
@ -34,7 +34,7 @@ async def load(ctx, extension):
|
|||
|
||||
@client.command()
|
||||
@commands.is_owner()
|
||||
async def unload(ctx, extension):
|
||||
async def unload(ctx : commands.Context, extension : str):
|
||||
try:
|
||||
client.unload_extension(f"cogs.{extension}")
|
||||
await ctx.message.add_reaction("👍")
|
||||
|
@ -43,7 +43,7 @@ async def unload(ctx, extension):
|
|||
|
||||
@client.command()
|
||||
@commands.is_owner()
|
||||
async def reload(ctx, extension):
|
||||
async def reload(ctx : commands.Context, extension : str):
|
||||
try:
|
||||
client.reload_extension(f"cogs.{extension}")
|
||||
await ctx.message.add_reaction("👍")
|
||||
|
@ -53,6 +53,7 @@ async def reload(ctx, extension):
|
|||
|
||||
@client.event
|
||||
async def on_ready():
|
||||
# Try to load cogs
|
||||
logging.info("--- Loading cogs ---\n")
|
||||
|
||||
total = 0
|
||||
|
|
|
@ -1,3 +1,8 @@
|
|||
'''
|
||||
This is the cog that allows users to
|
||||
fetch an AI-generated inspirational quote
|
||||
made by Inspirobot
|
||||
'''
|
||||
import discord
|
||||
|
||||
from discord.ext import commands
|
||||
|
@ -6,12 +11,12 @@ from util import config
|
|||
|
||||
class Inspirobot(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client : discord.Client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(name="Inspirobot", description="Sends a randomly generated inspirational quote", aliases=["inspiration", "inspiro"])
|
||||
@commands.cooldown(1, 5)
|
||||
async def inspirobot(self, ctx):
|
||||
async def inspirobot(self, ctx : commands.Context):
|
||||
image = inspirobot.get_inspirational_quote()
|
||||
if image is None:
|
||||
await ctx.message.add_reaction("⚠️")
|
||||
|
@ -20,5 +25,5 @@ class Inspirobot(commands.Cog):
|
|||
embed.set_image(url=image)
|
||||
await ctx.send(embed=embed)
|
||||
|
||||
def setup(client):
|
||||
def setup(client : discord.Client):
|
||||
client.add_cog(Inspirobot(client))
|
|
@ -1,3 +1,7 @@
|
|||
'''
|
||||
This cog is capable of communicating with the
|
||||
NASA API
|
||||
'''
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from api import nasa
|
||||
|
@ -5,12 +9,12 @@ from util import config
|
|||
|
||||
class Nasa(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: discord.Client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(name="APOD", description="Posts NASA's picture of the day.")
|
||||
@commands.cooldown(1, 30)
|
||||
async def apod(self, ctx):
|
||||
async def apod(self, ctx: commands.Context):
|
||||
url = nasa.image_of_the_day()
|
||||
embed = discord.Embed(title="Astronomy Picture of the day", color=int(config.settings["color"], 16))
|
||||
embed.set_image(url=url)
|
||||
|
@ -18,5 +22,5 @@ class Nasa(commands.Cog):
|
|||
await ctx.send(embed=embed)
|
||||
|
||||
|
||||
def setup(client):
|
||||
def setup(client: discord.Client):
|
||||
client.add_cog(Nasa(client))
|
|
@ -1,15 +1,19 @@
|
|||
'''
|
||||
This cog can fetch data from the
|
||||
Steam WebAPI
|
||||
'''
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from api import steam
|
||||
|
||||
class Steam(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: discord.Client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(name="SteamLevel", description="Finds the steam level of a user", usage="SteamLevel <Vanity URL>", aliases=["level"])
|
||||
@commands.cooldown(1, 2)
|
||||
async def SteamLevel(self, ctx, vanity_url):
|
||||
async def SteamLevel(self, ctx: commands.Context, vanity_url: str):
|
||||
level = steam.get_steam_level(vanity_url)
|
||||
|
||||
if level is None:
|
||||
|
@ -19,7 +23,5 @@ class Steam(commands.Cog):
|
|||
await ctx.send(f"{vanity_url} is level {level}! This makes him better than {percentile}% of Steam users!")
|
||||
|
||||
|
||||
|
||||
|
||||
def setup(client):
|
||||
def setup(client: discord.Client):
|
||||
client.add_cog(Steam(client))
|
|
@ -1,3 +1,8 @@
|
|||
'''
|
||||
Listens for certain keywords in messages and
|
||||
then provides conversion charts that might be
|
||||
useful.
|
||||
'''
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from util import logging, checking, config
|
||||
|
@ -5,7 +10,7 @@ import re
|
|||
|
||||
class AutoConvert(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: discord.Client):
|
||||
self.client = client
|
||||
self.currencies = ['€', '$', '£']
|
||||
self.currency_conversion = {
|
||||
|
@ -33,7 +38,7 @@ class AutoConvert(commands.Cog):
|
|||
|
||||
return prices
|
||||
|
||||
def convert_currency(self, _from, to, value):
|
||||
def convert_currency(self, _from: str, to: str, value: float) -> float:
|
||||
if _from == '€':
|
||||
conversion_rate = self.currency_conversion[_from + to]
|
||||
elif to == '€':
|
||||
|
@ -44,7 +49,7 @@ class AutoConvert(commands.Cog):
|
|||
return round(value * conversion_rate, 2)
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_message(self, message):
|
||||
async def on_message(self, message: discord.Message):
|
||||
if message.author.bot:
|
||||
return
|
||||
|
||||
|
@ -63,13 +68,9 @@ class AutoConvert(commands.Cog):
|
|||
currency_string += f"{self.convert_currency(element[1], currency, element[0])}{currency}, "
|
||||
embed.add_field(name=str(element[0])+element[1], value=currency_string[:-2])
|
||||
|
||||
|
||||
|
||||
if not empty:
|
||||
await message.channel.send(embed=embed)
|
||||
|
||||
|
||||
|
||||
|
||||
def setup(client):
|
||||
def setup(client: discord.Client):
|
||||
client.add_cog(AutoConvert(client))
|
|
@ -1,18 +1,23 @@
|
|||
'''
|
||||
Flips a coin.
|
||||
Will probably reworked into a "games" cog to
|
||||
hold more interactive commands
|
||||
'''
|
||||
import discord
|
||||
import random
|
||||
from discord.ext import commands
|
||||
|
||||
class Coinflip(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: discord.Client):
|
||||
self.client = client
|
||||
|
||||
@commands.command(name="coinflip", description="Flips a coin and reacts with the result", aliases=["coin", "flip"])
|
||||
async def coinflip(self, ctx):
|
||||
async def coinflip(self, ctx: commands.Context):
|
||||
if random.randint(0, 1) == 0:
|
||||
await ctx.message.add_reaction("🌑")
|
||||
else:
|
||||
await ctx.message.add_reaction("🌕")
|
||||
|
||||
def setup(client):
|
||||
def setup(client: discord.Client):
|
||||
client.add_cog(Coinflip(client))
|
|
@ -1,9 +1,13 @@
|
|||
'''
|
||||
Listens to keywords in messages and
|
||||
reacts accordingly
|
||||
'''
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
class Reactions(commands.Cog):
|
||||
|
||||
def __init__(self, client):
|
||||
def __init__(self, client: discord.Client):
|
||||
self.client = client
|
||||
|
||||
@commands.Cog.listener()
|
||||
|
@ -11,6 +15,7 @@ class Reactions(commands.Cog):
|
|||
if message.author.bot:
|
||||
return
|
||||
|
||||
# Flags
|
||||
if "canada" in message.content.lower():
|
||||
await message.add_reaction("🇨🇦")
|
||||
|
||||
|
@ -28,9 +33,10 @@ class Reactions(commands.Cog):
|
|||
await message.add_reaction("🇦")
|
||||
await message.add_reaction("🇾")
|
||||
|
||||
if "extremejoy" in str(message):
|
||||
message.add_reaction(664251611765407745)
|
||||
|
||||
if "extremejoy" in message.content.lower():
|
||||
await message.add_reaction("<:extremejoy:612424729969819648>")
|
||||
|
||||
|
||||
def setup(client):
|
||||
def setup(client: discord.Client):
|
||||
client.add_cog(Reactions(client))
|
|
@ -1,8 +1,13 @@
|
|||
'''
|
||||
This module will provide permission checks
|
||||
for the command executions.
|
||||
'''
|
||||
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
from util import logging
|
||||
|
||||
def is_author_bot(ctx : commands.Context):
|
||||
def is_author_bot(ctx : commands.Context) -> bool:
|
||||
if ctx.message.author.bot:
|
||||
return False
|
||||
return True
|
|
@ -1,10 +1,15 @@
|
|||
'''
|
||||
This module loads settings. Right now
|
||||
it can only hold one config file at a time
|
||||
'''
|
||||
|
||||
import json
|
||||
from util import logging
|
||||
|
||||
filepath = ""
|
||||
settings = {}
|
||||
|
||||
def load(file):
|
||||
def load(file : str) -> bool:
|
||||
global settings
|
||||
filepath = file
|
||||
try:
|
||||
|
|
|
@ -1,10 +1,14 @@
|
|||
'''
|
||||
Provides some simple logging utility
|
||||
'''
|
||||
import datetime
|
||||
|
||||
path = "logs/log.txt"
|
||||
cleanup = open(path, "w+")
|
||||
cleanup.close()
|
||||
|
||||
def log(msg, log_type, mute):
|
||||
# Format: [DD.MM.YYYY HH:MM:SS] [<TYPE>] MESSAGE
|
||||
def log(msg: str, log_type: str, mute: bool):
|
||||
log = f"[{datetime.datetime.now().strftime('%d.%m.%Y %H:%M:%S')}] [{log_type.upper()}] {msg}"
|
||||
log_file = open(path, "a")
|
||||
log_file.write(log + "\n")
|
||||
|
@ -12,11 +16,11 @@ def log(msg, log_type, mute):
|
|||
if mute == False:
|
||||
print(log)
|
||||
|
||||
def info(msg, mute=False):
|
||||
def info(msg: str, mute: bool=False):
|
||||
log(msg, "info", mute)
|
||||
|
||||
def warning(msg, mute=False):
|
||||
def warning(msg: str, mute: bool=False):
|
||||
log(msg, "warning", mute)
|
||||
|
||||
def error(msg, mute=False):
|
||||
def error(msg: str, mute: bool=False):
|
||||
log(msg, "error", mute)
|
||||
|
|
Loading…
Reference in a new issue