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):
|
|
|
|
"""Prints all the custom tags that this server currently has"""
|
2016-08-31 13:48:30 +12:00
|
|
|
tags = await config.get_content('tags')
|
2016-09-01 11:19:47 +12:00
|
|
|
tags = tags['tags']
|
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-07-31 12:20:55 +12:00
|
|
|
fmt = "\n".join("{}".format(tag['tag']) for tag in tags if tag['server_id'] == ctx.message.server.id)
|
2016-08-01 07:11:05 +12:00
|
|
|
await self.bot.say('```\n{}```'.format(fmt))
|
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
|
|
|
|
The format to call a custom tag is !tag <tag>"""
|
2016-08-31 13:48:30 +12:00
|
|
|
tags = await config.get_content('tags')
|
2016-09-01 11:19:47 +12:00
|
|
|
tags = tags['tags']
|
2016-08-19 08:55:04 +12:00
|
|
|
# Same generator as the method for tags, other than the second check to get the tag that is provided
|
2016-07-23 23:59:02 +12:00
|
|
|
result = [t for t in tags if t['tag'] == tag and t['server_id'] == ctx.message.server.id]
|
|
|
|
if len(result) == 0:
|
|
|
|
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-07-23 23:59:02 +12:00
|
|
|
await self.bot.say("{}".format(result[0]['result']))
|
|
|
|
|
|
|
|
@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
|
|
|
|
Format to add a tag is !tag add <tag> - <result>"""
|
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-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-08-31 13:48:30 +12:00
|
|
|
tags = await config.get_content('tags')
|
2016-09-01 11:19:47 +12:00
|
|
|
tags = tags['tags']
|
2016-07-23 23:59:02 +12:00
|
|
|
for t in tags:
|
2016-08-17 12:45:01 +12:00
|
|
|
# Attempt to find a tag with that name, so that we update it instead of making a duplicate
|
2016-07-23 23:59:02 +12:00
|
|
|
if t['tag'] == tag and t['server_id'] == ctx.message.server.id:
|
|
|
|
t['result'] = tag_result
|
2016-08-14 08:16:52 +12:00
|
|
|
await self.bot.say(
|
|
|
|
"I have just updated the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
|
2016-08-17 12:45:01 +12:00
|
|
|
# If we haven't found one, append a new one to the list
|
2016-07-23 23:59:02 +12:00
|
|
|
tags.append({'server_id': ctx.message.server.id, 'tag': tag, 'result': tag_result})
|
2016-08-14 08:16:52 +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-09-02 17:40:32 +12:00
|
|
|
await config.save_content('tags', {'tags': tags})
|
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):
|
|
|
|
"""Use this to remove a tag that from use for this server
|
|
|
|
Format to delete a tag is !tag delete <tag>"""
|
2016-08-31 13:48:30 +12:00
|
|
|
tags = await config.get_content('tags')
|
2016-09-01 11:19:47 +12:00
|
|
|
tags = tags['tags']
|
2016-08-17 12:45:01 +12:00
|
|
|
# Get a list of the tags that match this server, and the name provided (should only ever be one if any)
|
2016-07-23 23:59:02 +12:00
|
|
|
result = [t for t in tags if t['tag'] == tag and t['server_id'] == ctx.message.server.id]
|
2016-08-17 12:45:01 +12:00
|
|
|
# If we haven't found one, can't delete it
|
2016-07-23 23:59:02 +12:00
|
|
|
if len(result) == 0:
|
|
|
|
await self.bot.say(
|
|
|
|
"The tag {} does not exist! You can't remove something if it doesn't exist...".format(tag))
|
|
|
|
return
|
2016-08-17 12:45:01 +12:00
|
|
|
|
|
|
|
# Since there should never be more than one result due to our checks we've made, just remove the first result
|
|
|
|
tags.remove(result[0])
|
|
|
|
await self.bot.say('I have just removed the tag `{}`'.format(tag))
|
2016-09-02 17:40:32 +12:00
|
|
|
await config.save_content('tags', {'tags': tags})
|
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))
|