1
0
Fork 0
mirror of synced 2024-05-05 13:12:34 +12:00

Merge branch 'rewrite' of https://github.com/Phxntxm/Bonfire into rewrite

This commit is contained in:
phxntxm 2018-04-24 17:58:53 -05:00
commit c933457c72
3 changed files with 172 additions and 6 deletions

View file

@ -21,7 +21,7 @@ class Images:
EXAMPLE: !cat
RESULT: A beautiful picture of a cat o3o"""
result = await utils.request('http://random.cat/meow')
result = await utils.request('http://aws.random.cat/meow')
if result is None:
await ctx.send("I couldn't connect! Sorry no cats right now ;w;")
return
@ -55,7 +55,7 @@ class Images:
f = discord.File(image, filename=filename)
await ctx.send(file=f)
@commands.command()
@commands.command(aliases=['snake'])
@utils.custom_perms(send_messages=True)
@utils.check_restricted()
async def snek(self, ctx):
@ -63,10 +63,41 @@ class Images:
EXAMPLE: !snek
RESULT: A beautiful picture of a snek o3o"""
# Find a random image based on how many we currently have
f = random.SystemRandom().choice(glob.glob('images/snek*'))
with open(f, 'rb') as f:
await ctx.send(file=discord.File(f))
result = await utils.request("http://hrsendl.com/snake")
if result is None:
await ctx.send("I couldn't connect! Sorry no snakes right now ;w;")
return
filename = result.get('image', None)
if filename is None:
await ctx.send("I couldn't connect! Sorry no snakes right now ;w;")
return
image = await utils.download_image(filename)
filename = re.search('.*/snakes/(.*)', filename).group(1)
f = discord.File(image, filename=filename)
await ctx.send(file=f)
@commands.command()
@utils.custom_perms(send_messages=True)
@utils.check_restricted()
async def horse(self, ctx):
"""Use this to print a random horse image.
EXAMPLE: !horse
RESULT: A beautiful picture of a horse o3o"""
result = await utils.request("http://hrsendl.com/horse")
if result is None:
await ctx.send("I couldn't connect! Sorry no horses right now ;w;")
return
filename = result.get('image', None)
if filename is None:
await ctx.send("I couldn't connect! Sorry no horses right now ;w;")
return
image = await utils.download_image(filename)
filename = re.search('.*/horses/(.*)', filename).group(1)
f = discord.File(image, filename=filename)
await ctx.send(file=f)
@commands.command()
@commands.guild_only()

View file

@ -11,6 +11,7 @@ import inspect
import pendulum
import textwrap
import traceback
import subprocess
from contextlib import redirect_stdout
import io
@ -248,6 +249,16 @@ class Owner:
except discord.HTTPException:
await ctx.send("Content too large for me to print!")
@commands.command()
@commands.check(utils.is_owner)
async def bash(self, ctx, *, cmd: str):
"""Runs a bash command"""
output = subprocess.check_output("{}; exit 0".format(cmd), stderr=subprocess.STDOUT, shell=True)
if output:
await ctx.send("```\n{}\n```".format(output.decode("utf-8", "ignore").strip()))
else:
await ctx.send("No output for `{}`".format(cmd))
@commands.command()
@commands.check(utils.is_owner)
async def shutdown(self, ctx):

124
cogs/tutorial.py Normal file
View file

@ -0,0 +1,124 @@
from discord.ext import commands
from . import utils
import discord
class Tutorial:
def __init__(self, bot):
self.bot = bot
@commands.command()
@commands.check(utils.is_owner)
# @utils.custom_perms(send_messages=True)
async def tutorial(self, ctx, *, cmd_or_cog = None):
# The message we'll use to send
output = ""
# The list of commands we need to run through
commands = []
if cmd_or_cog:
cmd = self.bot.get_command(cmd_or_cog.lower())
# This should be a cog
if cmd is None:
cog = self.bot.get_cog(cmd_or_cog.title())
if cog is None:
await ctx.send("Could not find a command or a cog for {}".format(cmd_or_cog))
return
commands = [c for c in utils.get_all_commands(self.bot) if c.cog_name == cmd_or_cog.title()]
# Specific command
else:
commands = [cmd]
# Use all commands
else:
commands = list(utils.get_all_commands(self.bot))
# Loop through all the commands that we want to use
for command in commands:
embed = self.generate_embed(command)
# await ctx.author.send(embed=embed)
await ctx.send(embed=embed)
return
def generate_embed(self, command):
# Create the embed object
opts = {
"title": "`{}` command tutorial:\n\n".format(command.qualified_name),
"colour": discord.Colour.green()
}
embed = discord.Embed(**opts)
if command.help is not None:
# Split into examples, results, and the description itself based on the string
description, _, rest = command.help.partition('EXAMPLE:')
example, _, rest = rest.partition('RESULT:')
result, _, gif = rest.partition("GIF:")
else:
example = None
result = None
gif = None
# Add a field for the aliases
if command.aliases:
embed.add_field(
name="Aliases",
value="\n".join(["\t{}".format(alias) for alias in command.aliases]),
inline=False
)
# Add any paramaters needed
if command.clean_params:
params = []
for key, value in command.clean_params.items():
# Get the parameter type, as well as the default value if it exists
param_type, has_default, default_value = str(value).partition("=")
try:
# We want everything after the :
param_type = param_type.split(":")[1]
# Now we want to split based on . (for possible deep level types IE discord.member.Member) then get the last value
param_type = param_type.split(".")
param_type = param_type[len(param_type) - 1]
# This could mean something like *param was provided as the parameter
except IndexError:
param_type = "str"
# Start the string that we'll use as the param's info
string = "{} (Type: {}".format(key, param_type)
if default_value:
string += ", Default: {}".format(default_value)
# This is the = from the partition, if it exists, then there's a default...hence the name
if has_default:
string += ", optional)"
else:
string += ", required)"
# Now push our string to the list of params
params.append(string)
name = "Paramaters"
embed.add_field(name=name, value="\n".join(params), inline=False)
# Set the description of the embed to the description
if description:
embed.description = description
# Add these two in one embed
if example and result:
embed.add_field(
name="Example",
value="{}\n{}".format(example.strip(), result.strip()),
inline=False
)
try:
custom_perms = [func for func in command.checks if "custom_perms" in func.__qualname__][0]
perms = ",".join(attribute for attribute, setting in custom_perms.perms.items() if setting)
embed.set_footer(text="Permissions required: {}".format(perms))
except IndexError:
pass
return embed
def setup(bot):
bot.add_cog(Tutorial(bot))