🧹 Cleaning up cogs
This commit is contained in:
@ -1,4 +1,4 @@
|
||||
import discord, traceback
|
||||
import discord
|
||||
from discord.ext import commands
|
||||
|
||||
class EventCog(commands.Cog):
|
||||
@ -8,10 +8,7 @@ class EventCog(commands.Cog):
|
||||
# Syncs commands, sets the game, and logs when the bot logs in
|
||||
@commands.Cog.listener()
|
||||
async def on_ready(self):
|
||||
await self.bot.databaseFuncs.syncCommands()
|
||||
self.bot.log("Logged in as "+self.bot.user.name+", "+str(self.bot.user.id), level = 25)
|
||||
game = discord.Game("Use /help for commands")
|
||||
await self.bot.change_presence(activity=game, status = discord.Status.online)
|
||||
await self.bot.eventHandler.on_ready()
|
||||
|
||||
@commands.Cog.listener()
|
||||
async def on_disconnect(self):
|
||||
@ -20,38 +17,18 @@ class EventCog(commands.Cog):
|
||||
# Logs when user sends a command
|
||||
@commands.Cog.listener()
|
||||
async def on_slash_command(self, ctx):
|
||||
self.bot.log(f"{ctx.author.display_name} ran /{ctx.name}", str(ctx.channel_id), level = 25)
|
||||
logMessage = f"{ctx.author.display_name} ran /{ctx.name}"
|
||||
self.bot.log(logMessage, str(ctx.channel_id), level = 25)
|
||||
|
||||
# Logs if a command experiences an error
|
||||
@commands.Cog.listener()
|
||||
async def on_slash_command_error(self, ctx, error):
|
||||
if isinstance(error, commands.CommandNotFound):
|
||||
await ctx.send("That's not a command (error code 001)")
|
||||
elif isinstance(error,commands.errors.MissingRequiredArgument):
|
||||
self.bot.log(f"{error}",str(ctx.channel_id))
|
||||
await ctx.send("Missing command parameters (error code 002). Try using `!help [command]` to find out how to use the command.")
|
||||
else:
|
||||
exception = traceback.format_exception(type(error), error, error.__traceback__)
|
||||
stopAt = "\nThe above exception was the direct cause of the following exception:\n\n"
|
||||
if stopAt in exception:
|
||||
index = exception.index(stopAt)
|
||||
exception = exception[:index]
|
||||
|
||||
exceptionString = "".join(exception)
|
||||
self.bot.log([f"exception in /{ctx.name}", f"{exceptionString}"],str(ctx.channel_id), 40)
|
||||
await ctx.send("Something went wrong (error code 000)")
|
||||
await self.bot.errorHandler.on_slash_command_error(ctx, error)
|
||||
|
||||
# Logs if an error occurs
|
||||
@commands.Cog.listener()
|
||||
async def on_error(self, method):
|
||||
exception = traceback.format_exc()
|
||||
stopAt = "\nThe above exception was the direct cause of the following exception:\n\n"
|
||||
if stopAt in exception:
|
||||
index = exception.index(stopAt)
|
||||
exception = exception[:index]
|
||||
|
||||
exceptionString = "".join(exception)
|
||||
self.bot.log([f"exception in /{method}", f"{exceptionString}"], level = 40)
|
||||
await self.bot.errorHandler.on_error(method)
|
||||
|
||||
def setup(bot):
|
||||
bot.add_cog(EventCog(bot))
|
||||
|
@ -1,8 +1,6 @@
|
||||
import discord, asyncio, json
|
||||
from discord.ext import commands
|
||||
from discord_slash import cog_ext
|
||||
from discord_slash import SlashCommandOptionType as scot
|
||||
|
||||
from utils import getParams
|
||||
|
||||
params = getParams()
|
||||
@ -15,73 +13,22 @@ class GamesCog(commands.Cog):
|
||||
# Checks user balance
|
||||
@cog_ext.cog_slash(**params["balance"])
|
||||
async def balance(self, ctx):
|
||||
await ctx.defer()
|
||||
response = self.bot.money.checkBalance("#"+str(ctx.author.id))
|
||||
if response == 1:
|
||||
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBuck"
|
||||
else:
|
||||
new_message = ctx.author.display_name + " has " + str(response) + " GwendoBucks"
|
||||
await ctx.send(new_message)
|
||||
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):
|
||||
await ctx.defer()
|
||||
username = user.display_name
|
||||
if self.bot.databaseFuncs.getID(username) == None:
|
||||
async for member in ctx.guild.fetch_members(limit=None):
|
||||
if member.display_name.lower() == username.lower():
|
||||
username = member.display_name
|
||||
userID = "#" + str(member.id)
|
||||
self.bot.database["users"].insert_one({"_id":userID,"user name":username,"money":0})
|
||||
response = self.bot.money.giveMoney("#"+str(ctx.author.id),username,amount)
|
||||
await ctx.send(response)
|
||||
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"):
|
||||
await ctx.defer()
|
||||
response = self.bot.games.invest.parseInvest(parameters,"#"+str(ctx.author.id))
|
||||
if response.startswith("**"):
|
||||
responses = response.split("\n")
|
||||
em = discord.Embed(title=responses[0],description="\n".join(responses[1:]),colour=0x00FF00)
|
||||
await ctx.send(embed=em)
|
||||
else:
|
||||
await ctx.send(response)
|
||||
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 = ""):
|
||||
await ctx.defer()
|
||||
if answer == "":
|
||||
question, options, correctAnswer = self.bot.games.trivia.triviaStart(str(ctx.channel_id))
|
||||
if options != "":
|
||||
results = "**"+question+"**\n"
|
||||
for x, option in enumerate(options):
|
||||
results += chr(x+97) + ") "+option+"\n"
|
||||
|
||||
await ctx.send(results)
|
||||
|
||||
await asyncio.sleep(60)
|
||||
|
||||
self.bot.games.trivia.triviaCountPoints(str(ctx.channel_id))
|
||||
|
||||
self.bot.databaseFuncs.deleteGame("trivia questions",str(ctx.channel_id))
|
||||
|
||||
self.bot.log("Time's up for the trivia question",str(ctx.channel_id))
|
||||
await ctx.send("Time's up The answer was \""+chr(correctAnswer)+") "+options[correctAnswer-97]+"\". Anyone who answered that has gotten 1 GwendoBuck")
|
||||
else:
|
||||
await ctx.send(question, hidden=True)
|
||||
|
||||
elif answer in ["a","b","c","d"]:
|
||||
response = self.bot.games.trivia.triviaAnswer("#"+str(ctx.author.id),str(ctx.channel_id),answer)
|
||||
if response.startswith("Locked in "):
|
||||
await ctx.send(f"{ctx.author.display_name} answered {answer}")
|
||||
else:
|
||||
await ctx.send(response)
|
||||
else:
|
||||
self.bot.log("I didn't understand that (error code 1101)",str(ctx.channel_id))
|
||||
await ctx.send("I didn't understand that (error code 1101)")
|
||||
await self.bot.games.trivia.triviaParse(ctx, answer)
|
||||
|
||||
|
||||
class BlackjackCog(commands.Cog):
|
||||
|
Reference in New Issue
Block a user