2016-07-09 13:27:19 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import config
|
2016-07-16 08:13:26 +12:00
|
|
|
from .utils import checks
|
2016-07-09 13:27:19 +12:00
|
|
|
import subprocess
|
|
|
|
import urllib.parse
|
|
|
|
import urllib.request
|
2016-07-17 02:45:44 +12:00
|
|
|
import os
|
|
|
|
import glob
|
2016-07-09 13:27:19 +12:00
|
|
|
import json
|
|
|
|
import random
|
2016-07-10 06:27:14 +12:00
|
|
|
import discord
|
2016-07-11 01:58:46 +12:00
|
|
|
import re
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
|
|
|
|
class Core:
|
2016-07-09 13:59:10 +12:00
|
|
|
"""Core commands, these are the not 'complicated' commands."""
|
2016-07-11 09:18:30 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
|
|
|
|
|
|
|
@commands.command()
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-10 06:28:48 +12:00
|
|
|
async def addbot(self):
|
2016-07-10 06:31:40 +12:00
|
|
|
"""Provides a link that you can use to add me to a server"""
|
2016-07-10 06:27:14 +12:00
|
|
|
perms = discord.Permissions.none()
|
|
|
|
perms.read_messages = True
|
|
|
|
perms.send_messages = True
|
|
|
|
perms.manage_roles = True
|
|
|
|
perms.ban_members = True
|
|
|
|
perms.kick_members = True
|
|
|
|
perms.manage_messages = True
|
|
|
|
perms.embed_links = True
|
|
|
|
perms.read_message_history = True
|
|
|
|
perms.attach_files = True
|
2016-07-11 09:18:30 +12:00
|
|
|
await self.bot.say("Use this URL to add me to a server that you'd like!\n{}"
|
|
|
|
.format(discord.utils.oauth_url('183748889814237186', perms)))
|
2016-07-17 03:24:10 +12:00
|
|
|
|
|
|
|
|
2016-07-17 02:20:17 +12:00
|
|
|
@commands.command(pass_context=True)
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-17 02:46:02 +12:00
|
|
|
async def doggo(self, ctx):
|
2016-07-17 02:20:17 +12:00
|
|
|
"""Use this to print a random doggo image.
|
|
|
|
Doggo is love, doggo is life."""
|
2016-07-17 02:45:44 +12:00
|
|
|
os.chdir('/home/phxntx5/public_html/Bonfire/images')
|
|
|
|
f = glob.glob('doggo*')[random.randint(0,len(glob.glob('doggo*'))-1)]
|
|
|
|
f = open(f,'rb')
|
|
|
|
await self.bot.send_file(ctx.message.channel,f)
|
|
|
|
f.close()
|
2016-07-17 02:20:17 +12:00
|
|
|
|
|
|
|
|
2016-07-10 06:27:14 +12:00
|
|
|
@commands.command()
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-09 13:27:19 +12:00
|
|
|
async def joke(self):
|
|
|
|
"""Prints a random riddle"""
|
2016-07-10 00:57:25 +12:00
|
|
|
fortuneCommand = "/usr/bin/fortune riddles"
|
|
|
|
fortune = subprocess.check_output(fortuneCommand.split()).decode("utf-8")
|
|
|
|
await self.bot.say(fortune)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command()
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-09 13:27:19 +12:00
|
|
|
async def urban(self, *msg: str):
|
|
|
|
"""Pulls the top urbandictionary.com definition for a term"""
|
|
|
|
try:
|
|
|
|
term = '+'.join(msg)
|
|
|
|
url = "http://api.urbandictionary.com/v0/define?term={}".format(term)
|
|
|
|
response = urllib.request.urlopen(url)
|
|
|
|
data = json.loads(response.read().decode('utf-8'))
|
|
|
|
if len(data['list']) == 0:
|
|
|
|
await self.bot.say("No result with that term!")
|
|
|
|
else:
|
|
|
|
await self.bot.say(data['list'][0]['definition'])
|
|
|
|
except discord.HTTPException:
|
|
|
|
await self.bot.say('```Error: Definition is too long for me to send```')
|
|
|
|
|
|
|
|
@commands.command(pass_context=True)
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-09 13:27:19 +12:00
|
|
|
async def derpi(self, ctx, *search: str):
|
|
|
|
"""Provides a random image from the first page of derpibooru.org for the following term"""
|
2016-07-10 00:57:25 +12:00
|
|
|
if len(search) > 0:
|
2016-07-17 02:20:17 +12:00
|
|
|
# This sets the url as url?q=search+terms
|
2016-07-10 00:57:25 +12:00
|
|
|
url = 'https://derpibooru.org/search.json?q='
|
|
|
|
query = '+'.join(search)
|
|
|
|
url += query
|
2016-07-17 02:20:17 +12:00
|
|
|
|
|
|
|
# This will check if the channel is a 'nsfw' channel, if so add 'explicit' to the search term
|
2016-07-17 23:49:05 +12:00
|
|
|
#cursor = config.getCursor()
|
|
|
|
#cursor.execute('use {}'.format(config.db_default))
|
|
|
|
#cursor.execute('select * from nsfw_channels')
|
|
|
|
#result = cursor.fetchall()
|
|
|
|
nsfw_channels = config.getContent("nsfw_channels")
|
2016-07-10 04:25:28 +12:00
|
|
|
if {'channel_id': '{}'.format(ctx.message.channel.id)} in result:
|
2016-07-10 00:57:25 +12:00
|
|
|
url += ",+explicit&filter_id=95938"
|
2016-07-10 04:33:48 +12:00
|
|
|
config.closeConnection()
|
2016-07-17 02:20:17 +12:00
|
|
|
|
|
|
|
# Get the response from derpibooru and parse the 'searc' result from it
|
2016-07-10 00:57:25 +12:00
|
|
|
response = urllib.request.urlopen(url)
|
|
|
|
data = json.loads(response.read().decode('utf-8'))
|
|
|
|
results = data['search']
|
2016-07-17 02:20:17 +12:00
|
|
|
|
|
|
|
# Get the link if it exists, if not return saying no results found
|
2016-07-10 00:57:25 +12:00
|
|
|
if len(results) > 0:
|
|
|
|
index = random.randint(0, len(results) - 1)
|
2016-07-11 01:58:03 +12:00
|
|
|
imageLink = 'http://' + results[index].get('representations').get('full')[2:].strip()
|
2016-07-09 13:27:19 +12:00
|
|
|
else:
|
2016-07-10 00:57:25 +12:00
|
|
|
await self.bot.say("No results with that search term, {0}!".format(ctx.message.author.mention))
|
|
|
|
return
|
|
|
|
else:
|
2016-07-17 02:20:17 +12:00
|
|
|
# If no search term was provided, search for a random image
|
2016-07-10 00:57:25 +12:00
|
|
|
with urllib.request.urlopen('https://derpibooru.org/images/random') as response:
|
|
|
|
imageLink = response.geturl()
|
2016-07-17 02:20:17 +12:00
|
|
|
|
|
|
|
# Post link to my link shortening site
|
|
|
|
# discord still shows image previews through redirects so this is not an issue.
|
2016-07-10 00:57:25 +12:00
|
|
|
url = 'https://shpro.link/redirect.php/'
|
|
|
|
data = urllib.parse.urlencode({'link': imageLink}).encode('ascii')
|
|
|
|
response = urllib.request.urlopen(url, data).read().decode('utf-8')
|
|
|
|
await self.bot.say(response)
|
2016-07-09 13:27:19 +12:00
|
|
|
|
|
|
|
@commands.command(pass_context=True)
|
2016-07-17 04:48:10 +12:00
|
|
|
@checks.customPermsOrRole("none")
|
2016-07-16 09:39:26 +12:00
|
|
|
async def roll(self, ctx, notation: str="d6"):
|
2016-07-11 01:58:03 +12:00
|
|
|
"""Rolls a die based on the notation given
|
|
|
|
Format should be #d#"""
|
|
|
|
try:
|
2016-07-12 06:16:18 +12:00
|
|
|
dice = int(re.search("(\d*)d(\d*)", notation).group(1))
|
|
|
|
num = int(re.search("(\d*)d(\d*)", notation).group(2))
|
2016-07-17 02:20:17 +12:00
|
|
|
# This error will be hit if the notation is completely different than #d#
|
2016-07-12 06:18:57 +12:00
|
|
|
except AttributeError:
|
|
|
|
await self.bot.say("Please provide the die notation in #d#!")
|
|
|
|
return
|
2016-07-17 02:20:17 +12:00
|
|
|
# This error will be hit if there was an issue converting to an int
|
|
|
|
# This means the notation was still given wrong
|
2016-07-12 06:18:57 +12:00
|
|
|
except ValueError:
|
2016-07-11 01:58:03 +12:00
|
|
|
await self.bot.say("Please provide the die notation in #d#!")
|
|
|
|
return
|
2016-07-17 02:20:17 +12:00
|
|
|
# Dice will be None if d# was provided, assume this means 1d#
|
2016-07-15 03:15:13 +12:00
|
|
|
dice = dice or '1'
|
2016-07-12 06:16:18 +12:00
|
|
|
if dice > 10:
|
2016-07-11 01:58:03 +12:00
|
|
|
await self.bot.say("I'm not rolling more than 10 dice, I have tiny hands")
|
|
|
|
return
|
2016-07-12 06:16:18 +12:00
|
|
|
if num > 100:
|
2016-07-12 05:56:43 +12:00
|
|
|
await self.bot.say("What die has more than 100 sides? Please, calm down")
|
2016-07-11 01:58:03 +12:00
|
|
|
return
|
2016-07-16 09:39:26 +12:00
|
|
|
|
2016-07-15 03:16:03 +12:00
|
|
|
valueStr = ", ".join("{}".format(random.randint(1, num)) for i in range(0, int(dice)))
|
2016-07-11 09:18:30 +12:00
|
|
|
|
2016-07-11 01:58:03 +12:00
|
|
|
if int(dice) == 1:
|
2016-07-12 06:13:25 +12:00
|
|
|
fmt = '{0.message.author.name} has rolled a {2} sided die and got the number {3}!'
|
2016-07-11 01:58:03 +12:00
|
|
|
else:
|
2016-07-12 05:58:53 +12:00
|
|
|
fmt = '{0.message.author.name} has rolled {1}, {2} sided dice and got the numbers {3}!'
|
2016-07-12 06:13:25 +12:00
|
|
|
await self.bot.say(fmt.format(ctx, dice, num, valueStr))
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@commands.group(pass_context=True, invoke_without_command=True, no_pm=True)
|
2016-07-13 04:28:35 +12:00
|
|
|
async def tag(self, ctx, *tag: str):
|
2016-07-13 04:03:35 +12:00
|
|
|
"""This can be used for custom tags
|
|
|
|
The format to call a custom tag is !tag <tag>"""
|
2016-07-13 04:40:34 +12:00
|
|
|
tag = ' '.join(tag).strip()
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor = config.getCursor()
|
2016-07-13 03:58:25 +12:00
|
|
|
cursor.execute('use {}'.format(config.db_default))
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor.execute('select * from tags where server_id=%s and tag=%s', (ctx.message.server.id, tag))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result is None:
|
|
|
|
await self.bot.say('That tag does not exist!')
|
|
|
|
config.closeConnection()
|
|
|
|
return
|
|
|
|
await self.bot.say("{}".format(result['result']))
|
|
|
|
config.closeConnection()
|
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@tag.command(name='add', aliases=['create', 'start'], pass_context=True, no_pm=True)
|
2016-07-16 08:15:20 +12:00
|
|
|
@checks.customPermsOrRole("kick_members")
|
2016-07-13 03:47:41 +12:00
|
|
|
async def add_tag(self, ctx, *result: str):
|
2016-07-13 04:03:35 +12:00
|
|
|
"""Use this to add a new tag that can be used in this server
|
|
|
|
Format to add a tag is !tag add <tag> - <result>"""
|
2016-07-13 04:30:47 +12:00
|
|
|
result = ' '.join(result).strip()
|
2016-07-17 06:09:44 +12:00
|
|
|
tag = result[0:result.find('-')].strip()
|
|
|
|
result = result[result.find('-') + 2:].strip()
|
|
|
|
if len(tag) == 0 or len(result) == 0:
|
|
|
|
await self.bot.say("Please provide the format for the tag in: !tag add <tag> - <result>")
|
|
|
|
return
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor = config.getCursor()
|
2016-07-13 03:56:55 +12:00
|
|
|
cursor.execute('use {}'.format(config.db_default))
|
|
|
|
cursor.execute('select * from tags where server_id=%s and tag=%s', (ctx.message.server.id, tag))
|
2016-07-13 03:48:34 +12:00
|
|
|
response = cursor.fetchone()
|
|
|
|
if response is not None:
|
2016-07-15 06:23:11 +12:00
|
|
|
await self.bot.say('That tag already exists! Please remove it and re-add it if you want to change it')
|
2016-07-13 03:47:41 +12:00
|
|
|
config.closeConnection()
|
|
|
|
return
|
2016-07-13 03:56:55 +12:00
|
|
|
sql = 'insert into tags (server_id, tag, result) values (%s, %s, %s)'
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor.execute(sql, (ctx.message.server.id, tag, result))
|
2016-07-13 16:30:22 +12:00
|
|
|
await self.bot.say("I have just added the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
|
2016-07-13 03:47:41 +12:00
|
|
|
config.closeConnection()
|
|
|
|
|
2016-07-16 23:42:44 +12:00
|
|
|
@tag.command(name='delete', aliases=['remove', 'stop'], pass_context=True, no_pm=True)
|
2016-07-16 10:46:10 +12:00
|
|
|
@checks.customPermsOrRole("kick_members")
|
2016-07-13 04:48:47 +12:00
|
|
|
async def del_tag(self, ctx, *tag: str):
|
2016-07-13 04:03:35 +12:00
|
|
|
"""Use this to remove a tag that from use for this server
|
|
|
|
Format to delete a tag is !tag delete <tag>"""
|
2016-07-13 04:48:47 +12:00
|
|
|
tag = ' '.join(tag).strip()
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor = config.getCursor()
|
2016-07-13 03:56:55 +12:00
|
|
|
cursor.execute('use {}'.format(config.db_default))
|
2016-07-13 03:47:41 +12:00
|
|
|
cursor.execute('select * from tags where server_id=%s and tag=%s', (ctx.message.server.id, tag))
|
|
|
|
result = cursor.fetchone()
|
|
|
|
if result is None:
|
2016-07-17 04:57:17 +12:00
|
|
|
await self.bot.say("The tag {} does not exist! You can't remove something if it doesn't exist...".format(tag))
|
2016-07-13 03:47:41 +12:00
|
|
|
config.closeConnection()
|
|
|
|
return
|
|
|
|
cursor.execute('delete from tags where server_id=%s and tag=%s', (ctx.message.server.id, tag))
|
2016-07-13 16:30:22 +12:00
|
|
|
await self.bot.say('I have just removed the tag `{}`'.format(tag))
|
2016-07-13 03:47:41 +12:00
|
|
|
config.closeConnection()
|
2016-07-09 13:27:19 +12:00
|
|
|
|
2016-07-13 03:58:57 +12:00
|
|
|
|
2016-07-09 13:27:19 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Core(bot))
|