📝 Cogs and utils

Improved code style and added comments and docstrings to cogs and utils.
This commit is contained in:
NikolajDanger
2021-04-15 15:16:56 +02:00
parent 35b2446a10
commit 43f26ec383
11 changed files with 725 additions and 255 deletions

View File

@ -1,34 +1,40 @@
from discord.ext import commands
"""Contains the EventCog, which runs code for specific bot events."""
from discord.ext import commands # Has the cog class
class EventCog(commands.Cog):
"""Handles bot events."""
def __init__(self, bot):
"""Initialize the bot."""
self.bot = bot
self.bot.on_error = self.on_error
# Syncs commands, sets the game, and logs when the bot logs in
@commands.Cog.listener()
async def on_ready(self):
"""Log and set bot status when bot logs in."""
await self.bot.eventHandler.on_ready()
# Logs when user sends a command
@commands.Cog.listener()
async def on_slash_command(self, ctx):
"""Log when a slash command is run."""
await self.bot.eventHandler.on_slash_command(ctx)
# Logs if a command experiences an error
@commands.Cog.listener()
async def on_slash_command_error(self, ctx, error):
"""Log when a slash error occurs."""
await self.bot.errorHandler.on_slash_command_error(ctx, error)
# Logs if on error occurs
async def on_error(self, method, *args, **kwargs):
"""Log when an error occurs."""
await self.bot.errorHandler.on_error(method)
# If someone reacted to a message, checks if it's a reaction it's
# Gwendolyn has been waiting for, and then does something
@commands.Cog.listener()
async def on_reaction_add(self, reaction, user):
"""Handle when someone reacts to a message."""
await self.bot.eventHandler.on_reaction_add(reaction, user)
def setup(bot):
"""Add the eventcog to the bot."""
bot.add_cog(EventCog(bot))

View File

@ -1,155 +1,177 @@
from discord.ext import commands
from discord_slash import cog_ext
"""Contains all the cogs that deal with game commands."""
from discord.ext import commands # Has the cog class
from discord_slash import cog_ext # Used for slash commands
from utils import getParams
from utils import getParams # pylint: disable=import-error
params = getParams()
class GamesCog(commands.Cog):
def __init__(self,bot):
"""Runs game stuff."""
"""Contains miscellaneous game commands."""
def __init__(self, bot):
"""Initialize the cog."""
self.bot = bot
# Checks user balance
@cog_ext.cog_slash(**params["balance"])
async def balance(self, ctx):
"""Check user balance."""
await self.bot.money.sendBalance(ctx)
# Gives another user an amount of GwendoBucks
@cog_ext.cog_slash(**params["give"])
async def give(self, ctx, user, amount):
"""Give another user an amount of GwendoBucks."""
await self.bot.money.giveMoney(ctx, user, amount)
# Invest GwendoBucks in the stock market
@cog_ext.cog_slash(**params["invest"])
async def invest(self, ctx, parameters = "check"):
async def invest(self, ctx, parameters="check"):
"""Invest GwendoBucks in the stock market."""
await self.bot.games.invest.parseInvest(ctx, parameters)
# Runs a game of trivia
@cog_ext.cog_slash(**params["trivia"])
async def trivia(self, ctx, answer = ""):
async def trivia(self, ctx, answer=""):
"""Run a game of trivia."""
await self.bot.games.trivia.triviaParse(ctx, answer)
class BlackjackCog(commands.Cog):
def __init__(self,bot):
"""Runs game stuff."""
"""Contains the blackjack commands."""
def __init__(self, bot):
"""Initialize the cog."""
self.bot = bot
# Starts a game of blackjack
@cog_ext.cog_subcommand(**params["blackjackStart"])
async def blackjackStart(self, ctx):
"""Start a game of blackjack."""
await self.bot.games.blackjack.start(ctx)
@cog_ext.cog_subcommand(**params["blackjackBet"])
async def blackjackBet(self, ctx, bet):
"""Enter the game of blackjack with a bet."""
await self.bot.games.blackjack.playerDrawHand(ctx, bet)
@cog_ext.cog_subcommand(**params["blackjackStand"])
async def blackjackStand(self, ctx, hand = ""):
async def blackjackStand(self, ctx, hand=""):
"""Stand on your hand in blackjack."""
await self.bot.games.blackjack.stand(ctx, hand)
@cog_ext.cog_subcommand(**params["blackjackHit"])
async def blackjackHit(self, ctx, hand = 0):
async def blackjackHit(self, ctx, hand=0):
"""Hit on your hand in blackjack."""
await self.bot.games.blackjack.hit(ctx, hand)
@cog_ext.cog_subcommand(**params["blackjackDouble"])
async def blackjackDouble(self, ctx, hand = 0):
async def blackjackDouble(self, ctx, hand=0):
"""Double in blackjack."""
await self.bot.games.blackjack.double(ctx, hand)
@cog_ext.cog_subcommand(**params["blackjackSplit"])
async def blackjackSplit(self, ctx, hand = 0):
async def blackjackSplit(self, ctx, hand=0):
"""Split your hand in blackjack."""
await self.bot.games.blackjack.split(ctx, hand)
@cog_ext.cog_subcommand(**params["blackjackHilo"])
async def blackjackHilo(self, ctx):
"""Get the hilo value for the deck in blackjack."""
await self.bot.games.blackjack.hilo(ctx)
@cog_ext.cog_subcommand(**params["blackjackShuffle"])
async def blackjackShuffle(self, ctx):
"""Shuffle the blackjack game."""
await self.bot.games.blackjack.shuffle(ctx)
@cog_ext.cog_subcommand(**params["blackjackCards"])
async def blackjackCards(self, ctx):
"""Get the amount of cards left in the blackjack deck."""
await self.bot.games.blackjack.cards(ctx)
class ConnectFourCog(commands.Cog):
def __init__(self,bot):
"""Runs game stuff."""
"""Contains all the connect four commands."""
def __init__(self, bot):
"""Initialize the cog."""
self.bot = bot
# Start a game of connect four against a user
@cog_ext.cog_subcommand(**params["connectFourStartUser"])
async def connectFourStartUser(self, ctx, user):
"""Start a game of connect four against another user."""
await self.bot.games.connectFour.start(ctx, user)
# Start a game of connect four against gwendolyn
@cog_ext.cog_subcommand(**params["connectFourStartGwendolyn"])
async def connectFourStartGwendolyn(self, ctx, difficulty = 3):
async def connectFourStartGwendolyn(self, ctx, difficulty=3):
"""Start a game of connect four against Gwendolyn."""
await self.bot.games.connectFour.start(ctx, difficulty)
# Stop the current game of connect four
@cog_ext.cog_subcommand(**params["connectFourSurrender"])
async def connectFourSurrender(self, ctx):
"""Surrender the game of connect four."""
await self.bot.games.connectFour.surrender(ctx)
class HangmanCog(commands.Cog):
def __init__(self,bot):
"""Runs game stuff."""
"""Contains all the hangman commands."""
def __init__(self, bot):
"""Initialize the cog."""
self.bot = bot
# Starts a game of Hangman
@cog_ext.cog_subcommand(**params["hangmanStart"])
async def hangmanStart(self, ctx):
"""Start a game of hangman."""
await self.bot.games.hangman.start(ctx)
# Stops a game of Hangman
@cog_ext.cog_subcommand(**params["hangmanStop"])
async def hangmanStop(self, ctx):
"""Stop the current game of hangman."""
await self.bot.games.hangman.stop(ctx)
class HexCog(commands.Cog):
def __init__(self,bot):
"""Runs game stuff."""
self.bot = bot
"""Contains all the hex commands."""
def __init__(self, bot):
"""Initialize the cog."""
self.bot = bot
self.hex = self.bot.games.hex
# Start a game of Hex against another user
@cog_ext.cog_subcommand(**params["hexStartUser"])
async def hexStartUser(self, ctx, user):
await self.bot.games.hex.start(ctx, user)
"""Start a game of hex against another player."""
await self.hex.start(ctx, user)
# Start a game of Hex against Gwendolyn
@cog_ext.cog_subcommand(**params["hexStartGwendolyn"])
async def hexStartGwendolyn(self, ctx, difficulty = 2):
await self.bot.games.hex.start(ctx, difficulty)
async def hexStartGwendolyn(self, ctx, difficulty=2):
"""Start a game of hex against Gwendolyn."""
await self.hex.start(ctx, difficulty)
# Place a piece in the hex game
@cog_ext.cog_subcommand(**params["hexPlace"])
async def hexPlace(self, ctx, coordinates):
await self.bot.games.hex.placeHex(ctx, coordinates, f"#{ctx.author.id}")
"""Place a piece in the hex game."""
await self.hex.placeHex(ctx, coordinates, f"#{ctx.author.id}")
# Undo your last hex move
@cog_ext.cog_subcommand(**params["hexUndo"])
async def hexUndo(self, ctx):
await self.bot.games.hex.undo(ctx)
"""Undo your last hex move."""
await self.hex.undo(ctx)
# Perform a hex swap
@cog_ext.cog_subcommand(**params["hexSwap"])
async def hexSwap(self, ctx):
await self.bot.games.hex.swap(ctx)
"""Perform a hex swap."""
await self.hex.swap(ctx)
# Surrender the hex game
@cog_ext.cog_subcommand(**params["hexSurrender"])
async def hexSurrender(self, ctx):
await self.bot.games.hex.surrender(ctx)
"""Surrender the hex game."""
await self.hex.surrender(ctx)
def setup(bot):
"""Add all the cogs to the bot."""
bot.add_cog(GamesCog(bot))
bot.add_cog(BlackjackCog(bot))
bot.add_cog(ConnectFourCog(bot))
bot.add_cog(HangmanCog(bot))
bot.add_cog(HexCog(bot))
bot.add_cog(HexCog(bot))

View File

@ -1,24 +1,32 @@
from discord.ext import commands
from discord_slash import cog_ext
"""Contains the LookupCog, which deals with the lookup commands."""
from discord.ext import commands # Has the cog class
from discord_slash import cog_ext # Used for slash commands
from utils import getParams
from utils import getParams # pylint: disable=import-error
params = getParams()
class LookupCog(commands.Cog):
"""Contains the lookup commands."""
def __init__(self, bot):
"""Runs lookup commands."""
"""Initialize the cog."""
self.bot = bot
# Looks up a spell
@cog_ext.cog_slash(**params["spell"])
async def spell(self, ctx, query):
"""Look up a spell."""
await self.bot.lookupFuncs.spellFunc(ctx, query)
# Looks up a monster
@cog_ext.cog_slash(**params["monster"])
async def monster(self, ctx, query):
"""Look up a monster."""
await self.bot.lookupFuncs.monsterFunc(ctx, query)
def setup(bot):
bot.add_cog(LookupCog(bot))
"""Add the cog to the bot."""
bot.add_cog(LookupCog(bot))

View File

@ -1,94 +1,99 @@
import discord, codecs, string, json
from discord.ext import commands
from discord_slash import cog_ext
"""Contains the MiscCog, which deals with miscellaneous commands."""
from discord.ext import commands # Has the cog class
from discord_slash import cog_ext # Used for slash commands
from utils import getParams # pylint: disable=import-error
from utils import getParams # pylint: disable=import-error
params = getParams()
class MiscCog(commands.Cog):
"""Contains the miscellaneous commands."""
def __init__(self, bot):
"""Runs misc commands."""
"""Initialize the cog."""
self.bot = bot
self.bot.remove_command("help")
self.generators = bot.other.generators
self.bedreNetflix = bot.other.bedreNetflix
self.nerdShit = bot.other.nerdShit
# Sends the bot's latency
@cog_ext.cog_slash(**params["ping"])
async def ping(self, ctx):
"""Send the bot's latency."""
await ctx.send(f"Pong!\nLatency is {round(self.bot.latency * 1000)}ms")
# Restarts the bot
@cog_ext.cog_slash(**params["stop"])
async def stop(self, ctx):
"""Stop the bot."""
await self.bot.stop(ctx)
# Gets help for specific command
@cog_ext.cog_slash(**params["help"])
async def helpCommand(self, ctx, command = ""):
async def helpCommand(self, ctx, command=""):
"""Get help for commands."""
await self.bot.other.helpFunc(ctx, command)
# Lets you thank the bot
@cog_ext.cog_slash(**params["thank"])
async def thank(self, ctx):
"""Thank the bot."""
await ctx.send("You're welcome :blush:")
# Sends a friendly message
@cog_ext.cog_slash(**params["hello"])
async def hello(self, ctx):
"""Greet the bot."""
await self.bot.other.helloFunc(ctx)
# Rolls dice
@cog_ext.cog_slash(**params["roll"])
async def roll(self, ctx, dice = "1d20"):
async def roll(self, ctx, dice="1d20"):
"""Roll dice."""
await self.bot.other.rollDice(ctx, dice)
# Sends a random image
@cog_ext.cog_slash(**params["image"])
async def image(self, ctx):
"""Get a random image from Bing."""
await self.bot.other.imageFunc(ctx)
# Finds a random movie
@cog_ext.cog_slash(**params["movie"])
async def movie(self, ctx):
"""Get a random movie from the Plex server."""
await self.bot.other.movieFunc(ctx)
# Generates a random name
@cog_ext.cog_slash(**params["name"])
async def name(self, ctx):
"""Generate a random name."""
await self.generators.nameGen(ctx)
# Generates a random tavern name
@cog_ext.cog_slash(**params["tavern"])
async def tavern(self, ctx):
"""Generate a random tavern name."""
await self.generators.tavernGen(ctx)
# Finds a page on the Senkulpa wiki
@cog_ext.cog_slash(**params["wiki"])
async def wiki(self, ctx, wikiPage = ""):
async def wiki(self, ctx, wikiPage=""):
"""Get a page on a fandom wiki."""
await self.bot.other.findWikiPage(ctx, wikiPage)
#Searches for movie and adds it to Bedre Netflix
@cog_ext.cog_slash(**params["addMovie"])
async def addMovie(self, ctx, movie):
"""Search for a movie and add it to the Plex server."""
await self.bedreNetflix.requestMovie(ctx, movie)
#Searches for show and adds it to Bedre Netflix
@cog_ext.cog_slash(**params["addShow"])
async def addShow(self, ctx, show):
"""Search for a show and add it to the Plex server."""
await self.bedreNetflix.requestShow(ctx, show)
#Returns currently downloading torrents
@cog_ext.cog_slash(**params["downloading"])
async def downloading(self, ctx, parameters = "-d"):
async def downloading(self, ctx, parameters="-d"):
"""Get the current downloading torrents."""
await self.bedreNetflix.downloading(ctx, parameters)
#Looks up on Wolfram Alpha
@cog_ext.cog_slash(**params["wolf"])
async def wolf(self, ctx, query):
"""Perform a search on Wolfram Alpha."""
await self.nerdShit.wolfSearch(ctx, query)
def setup(bot):
"""Add the cog to the bot."""
bot.add_cog(MiscCog(bot))

View File

@ -1,37 +1,40 @@
import discord, string, json
"""Contains the StarWarsCog, which deals with Star Wars commands."""
from discord.ext import commands
from discord_slash import cog_ext
from utils import getParams
from utils import getParams # pylint: disable=import-error
params = getParams()
class starWarsCog(commands.Cog):
class StarWarsCog(commands.Cog):
"""Contains the Star Wars commands."""
def __init__(self, bot):
"""Runs star wars commands."""
"""Initialize the cog."""
self.bot = bot
# Rolls star wars dice
@cog_ext.cog_slash(**params["starWarsRoll"])
async def starWarsRoll(self, ctx, dice = ""):
async def starWarsRoll(self, ctx, dice=""):
"""Roll Star Wars dice."""
await self.bot.starWars.roll.parseRoll(ctx, dice)
# Controls destiny points
@cog_ext.cog_slash(**params["starWarsDestiny"])
async def starWarsDestiny(self, ctx, parameters = ""):
async def starWarsDestiny(self, ctx, parameters=""):
"""Control Star Wars destiny points."""
await self.bot.starWars.destiny.parseDestiny(ctx, parameters)
# Rolls for critical injuries
@cog_ext.cog_slash(**params["starWarsCrit"])
async def starWarsCrit(self, ctx, severity : int = 0):
async def starWarsCrit(self, ctx, severity: int = 0):
"""Roll for critical injuries."""
await self.bot.starWars.roll.critRoll(ctx, severity)
# Accesses and changes character sheet data with the parseChar function
# from funcs/starWarsFuncs/starWarsCharacter.py
@cog_ext.cog_slash(**params["starWarsCharacter"])
async def starWarsCharacter(self, ctx, parameters = ""):
async def starWarsCharacter(self, ctx, parameters=""):
"""Access and change Star Wars character sheet data."""
await self.bot.starWars.character.parseChar(ctx, parameters)
def setup(bot):
bot.add_cog(starWarsCog(bot))
"""Add the cog to the bot."""
bot.add_cog(StarWarsCog(bot))