Files
Gwendolyn/cogs/MiscCog.py
2021-03-29 00:55:31 +02:00

157 lines
5.6 KiB
Python

import discord, codecs, string, json
from discord.ext import commands
from discord_slash import cog_ext
from funcs import logThis, helloFunc, roll_dice, imageFunc, movieFunc, cap, findWikiPage
from utils import Options
with open("resources/slashParameters.json", "r") as f:
params = json.load(f)
options = Options()
if options.testing:
for p in params:
params[p]["guild_ids"] = options.guildIds
class MiscCog(commands.Cog):
def __init__(self,client):
"""Runs misc commands."""
self.client = client
self.client.remove_command("help")
self.generator = client.generator
self.bedreNetflix = client.bedreNetflix
self.nerdShit = client.nerdShit
# Sends the bot's latency
@cog_ext.cog_slash(**params["ping"])
async def ping(self, ctx):
await ctx.send(f"Pong!\nLatency is {round(self.client.latency * 1000)}ms")
# Restarts the bot
@cog_ext.cog_slash(**params["stop"])
async def stop(self, ctx):
if "#"+str(ctx.author.id) in self.client.options.admins:
await ctx.send("Pulling git repo and restarting...")
self.client.funcs.stopServer()
logThis("Logging out.")
await self.client.logout()
else:
logThis(f"{ctx.author.display_name} tried to stop me! (error code 201)",str(ctx.channel_id))
await ctx.send(f"I don't think I will, {ctx.author.display_name} (error code 201)")
# Gets help for specific command
@cog_ext.cog_slash(**params["help"])
async def helpCommand(self, ctx, command = ""):
if command == "":
with codecs.open("resources/help/help.txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = "Help", description = text,colour = 0x59f442)
await ctx.send(embed = em)
else:
logThis(f"Looking for help-{command}.txt",str(ctx.channel_id))
with codecs.open(f"resources/help/help-{command}.txt",encoding="utf-8") as f:
text = f.read()
em = discord.Embed(title = command.capitalize(), description = text,colour = 0x59f442)
await ctx.send(embed = em)
# Let's you thank the bot
@cog_ext.cog_slash(**params["thank"])
async def thank(self, ctx):
await ctx.send("You're welcome :blush:")
# Sends a friendly message
@cog_ext.cog_slash(**params["hello"])
async def hello(self, ctx):
await ctx.send(helloFunc(ctx.author.display_name))
# Rolls dice
@cog_ext.cog_slash(**params["roll"])
async def roll(self, ctx, dice = "1d20"):
await ctx.send(roll_dice(ctx.author.display_name,dice))
# Sends a random image
@cog_ext.cog_slash(**params["image"])
async def image(self, ctx):
randomImage = imageFunc()
await ctx.send(randomImage)
# Finds a random movie
@cog_ext.cog_slash(**params["movie"])
async def movie(self,ctx):
await ctx.defer()
title, plot, cover, cast = movieFunc()
if title == "error":
await ctx.send("An error occurred. Try again (error code "+plot+")")
else:
try:
embed = discord.Embed(title=title, description=plot, color=0x24ec19)
embed.set_thumbnail(url=cover)
embed.add_field(name="Cast", value=cast,inline = True)
await ctx.send(embed = embed)
except:
logThis("Error embedding (error code 805)")
# Generates a random name
@cog_ext.cog_slash(**params["name"])
async def name(self, ctx):
await ctx.send(self.generator.nameGen())
# Generates a random tavern name
@cog_ext.cog_slash(**params["tavern"])
async def tavern(self, ctx):
await ctx.send(self.generator.tavernGen())
# Sets the game Gwendolyn's playing
@cog_ext.cog_slash(**params["game"])
async def game(self, ctx, gameText):
gamePlaying = cap(gameText)
game = discord.Game(gamePlaying)
await self.client.change_presence(activity=game)
await ctx.send(f"Setting game to \"{gamePlaying}\"")
# Finds a page on the Senkulpa wiki
@cog_ext.cog_slash(**params["wiki"])
async def wiki(self, ctx, wikiPage):
await ctx.defer()
command = string.capwords(wikiPage)
title, content, thumbnail = findWikiPage(command)
if title != "":
logThis("Sending the embedded message",str(ctx.channel_id))
content += "\n[Læs mere](https://senkulpa.fandom.com/da/wiki/"+title.replace(" ","_")+")"
embed = discord.Embed(title = title, description = content, colour=0xDEADBF)
if thumbnail != "":
embed.set_thumbnail(url=thumbnail)
await ctx.send(embed = embed)
else:
await ctx.send(content)
#Searches for movie and adds it to Bedre Netflix
@cog_ext.cog_slash(**params["addMovie"])
async def addMovie(self, ctx, movie):
await ctx.defer()
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):
await ctx.defer()
await self.bedreNetflix.requestShow(ctx, show)
#Returns currently downloading torrents
@cog_ext.cog_slash(**params["downloading"])
async def downloading(self, ctx, parameters = "-d"):
await ctx.defer()
await self.bedreNetflix.downloading(ctx, parameters)
#Looks up on Wolfram Alpha
@cog_ext.cog_slash(**params["wolf"])
async def wolf(self, ctx, query):
await self.nerdShit.wolfSearch(ctx, query)
def setup(client):
client.add_cog(MiscCog(client))