2016-07-23 23:59:02 +12:00
|
|
|
from discord.ext import commands
|
|
|
|
from .utils import config
|
|
|
|
from .utils import checks
|
2016-08-06 10:00:16 +12:00
|
|
|
import re
|
2016-07-23 23:59:02 +12:00
|
|
|
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-23 23:59:02 +12:00
|
|
|
class Tags:
|
|
|
|
"""This class contains all the commands for custom tags"""
|
|
|
|
|
|
|
|
def __init__(self, bot):
|
|
|
|
self.bot = bot
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-08-05 00:10:17 +12:00
|
|
|
@commands.command(pass_context=True, no_pm=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(send_messages=True)
|
2016-07-23 23:59:02 +12:00
|
|
|
async def tags(self, ctx):
|
2016-11-29 17:55:55 +13:00
|
|
|
"""Prints all the custom tags that this server currently has
|
|
|
|
|
|
|
|
EXAMPLE: !tags
|
|
|
|
RESULT: All tags setup on this server"""
|
2016-09-29 12:39:34 +13:00
|
|
|
tags = await config.get_content('tags', {'server_id': ctx.message.server.id})
|
2016-08-17 12:45:01 +12:00
|
|
|
# Simple generator that adds a tag to the list to print, if the tag is for this server
|
2016-10-02 07:11:48 +13:00
|
|
|
try:
|
|
|
|
fmt = "\n".join("{}".format(tag['tag']) for tag in tags)
|
|
|
|
await self.bot.say('```\n{}```'.format(fmt))
|
|
|
|
except TypeError:
|
|
|
|
await self.bot.say("There are not tags setup on this server!")
|
2016-07-23 23:59:02 +12:00
|
|
|
|
|
|
|
@commands.group(pass_context=True, invoke_without_command=True, no_pm=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(send_messages=True)
|
2016-07-23 23:59:02 +12:00
|
|
|
async def tag(self, ctx, *, tag: str):
|
|
|
|
"""This can be used to call custom tags
|
2016-11-29 17:55:55 +13:00
|
|
|
The format to call a custom tag is !tag <tag>
|
|
|
|
|
|
|
|
EXAMPLE: !tag butts
|
|
|
|
RESULT: Whatever you setup for the butts tag!!"""
|
2016-09-29 12:39:34 +13:00
|
|
|
r_filter = lambda row: (row['server_id'] == ctx.message.server.id) & (row['tag'] == tag)
|
|
|
|
tags = await config.get_content('tags', r_filter)
|
2016-09-29 13:17:07 +13:00
|
|
|
if tags is None:
|
2016-07-23 23:59:02 +12:00
|
|
|
await self.bot.say('That tag does not exist!')
|
|
|
|
return
|
2016-08-17 12:45:01 +12:00
|
|
|
# We shouldn't ever have two tags of the same name, so just get the first result
|
2016-09-29 12:39:34 +13:00
|
|
|
await self.bot.say("\u200B{}".format(tags[0]['result']))
|
2016-07-23 23:59:02 +12:00
|
|
|
|
|
|
|
@tag.command(name='add', aliases=['create', 'start'], pass_context=True, no_pm=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(kick_members=True)
|
2016-07-23 23:59:02 +12:00
|
|
|
async def add_tag(self, ctx, *, result: str):
|
|
|
|
"""Use this to add a new tag that can be used in this server
|
2016-11-29 17:55:55 +13:00
|
|
|
Format to add a tag is !tag add <tag> - <result>
|
|
|
|
|
|
|
|
EXAMPLE: !tag add this is my new tag - This is what it will be
|
|
|
|
RESULT: A tag that can be called by '!tag this is my new tag' and will output 'This is what it will be'"""
|
2016-08-05 03:47:34 +12:00
|
|
|
try:
|
2016-08-17 12:45:01 +12:00
|
|
|
# Use regex to get the matche for everything before and after a -
|
|
|
|
match = re.search("(.*) - (.*)", result)
|
|
|
|
tag = match.group(1).strip()
|
|
|
|
tag_result = match.group(2).strip()
|
|
|
|
# Next two checks are just to ensure there was a valid match found
|
2016-08-05 03:47:34 +12:00
|
|
|
except AttributeError:
|
2016-08-17 12:45:01 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
|
2016-07-23 23:59:02 +12:00
|
|
|
return
|
2016-12-13 04:52:04 +13:00
|
|
|
# If our regex failed to find the content (aka they provided the wrong format)
|
2016-08-05 03:47:34 +12:00
|
|
|
if len(tag) == 0 or len(tag_result) == 0:
|
2016-08-17 12:45:01 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
|
2016-08-05 03:47:34 +12:00
|
|
|
return
|
2016-08-17 12:45:01 +12:00
|
|
|
|
2016-12-13 04:52:04 +13:00
|
|
|
# Make sure the tag created does not mention everyone/here
|
|
|
|
if '@everyone' in tag_result or '@here' in tag_result:
|
|
|
|
await self.bot.say("You cannot create a tag that mentions everyone!")
|
|
|
|
return
|
2016-09-29 12:39:34 +13:00
|
|
|
entry = {'server_id': ctx.message.server.id, 'tag': tag, 'result': tag_result}
|
|
|
|
r_filter = lambda row: (row['server_id'] == ctx.message.server.id) & (row['tag'] == tag)
|
|
|
|
# Try to create new entry first, if that fails (it already exists) then we update it
|
|
|
|
if await config.add_content('tags', entry, r_filter):
|
|
|
|
await self.bot.say(
|
|
|
|
"I have just added the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
|
|
|
|
else:
|
|
|
|
await config.update_content('tags', entry, r_filter)
|
|
|
|
await self.bot.say(
|
|
|
|
"I have just updated the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
|
2016-07-23 23:59:02 +12:00
|
|
|
|
|
|
|
@tag.command(name='delete', aliases=['remove', 'stop'], pass_context=True, no_pm=True)
|
2016-08-15 14:10:12 +12:00
|
|
|
@checks.custom_perms(kick_members=True)
|
2016-07-23 23:59:02 +12:00
|
|
|
async def del_tag(self, ctx, *, tag: str):
|
2016-09-29 12:39:34 +13:00
|
|
|
"""Use this to remove a tag from use for this server
|
2016-11-29 17:55:55 +13:00
|
|
|
Format to delete a tag is !tag delete <tag>
|
|
|
|
|
|
|
|
EXAMPLE: !tag delete stupid_tag
|
|
|
|
RESULT: Deletes that stupid tag"""
|
2016-09-29 12:39:34 +13:00
|
|
|
r_filter = lambda row: (row['server_id'] == ctx.message.server.id) & (row['tag'] == tag)
|
|
|
|
if await config.remove_content('tags', r_filter):
|
|
|
|
await self.bot.say('I have just removed the tag `{}`'.format(tag))
|
|
|
|
else:
|
2016-07-23 23:59:02 +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-24 00:01:16 +12:00
|
|
|
|
2016-07-31 12:20:55 +12:00
|
|
|
|
2016-07-24 00:01:16 +12:00
|
|
|
def setup(bot):
|
|
|
|
bot.add_cog(Tags(bot))
|