Implement the new tag setup
This commit is contained in:
parent
ea8535a647
commit
0968284ede
152
cogs/tags.py
152
cogs/tags.py
|
@ -1,9 +1,10 @@
|
|||
import re
|
||||
|
||||
from discord.ext import commands
|
||||
import discord
|
||||
|
||||
from . import utils
|
||||
|
||||
import asyncio
|
||||
import rethinkdb as r
|
||||
|
||||
class Tags:
|
||||
"""This class contains all the commands for custom tags"""
|
||||
|
@ -18,83 +19,108 @@ class Tags:
|
|||
|
||||
EXAMPLE: !tags
|
||||
RESULT: All tags setup on this server"""
|
||||
tags = await utils.get_content('tags', ctx.message.guild.id)
|
||||
# Simple generator that adds a tag to the list to print, if the tag is for this server
|
||||
try:
|
||||
fmt = "\n".join("{}".format(tag['tag']) for tag in tags)
|
||||
await ctx.send('```\n{}```'.format(fmt))
|
||||
except TypeError:
|
||||
await ctx.send("There are not tags setup on this server!")
|
||||
tags = await utils.get_content('tags', str(ctx.message.guild.id))
|
||||
if tags:
|
||||
entries = [t['trigger'] for t in tags['tags']]
|
||||
pages = utils.Pages(self.bot, message=ctx.message, entries=entries)
|
||||
await pages.paginate()
|
||||
else:
|
||||
await ctx.send("There are no tags setup on this server!")
|
||||
|
||||
@commands.group(invoke_without_command=True, no_pm=True)
|
||||
@utils.custom_perms(send_messages=True)
|
||||
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>
|
||||
The format to call a custom tag is !tag <tag>
|
||||
|
||||
EXAMPLE: !tag butts
|
||||
RESULT: Whatever you setup for the butts tag!!"""
|
||||
r_filter = lambda row: (row['server_id'] == ctx.message.guild.id) & (row['tag'] == tag)
|
||||
tags = await utils.filter_content('tags', r_filter)
|
||||
if tags is None:
|
||||
await ctx.send('That tag does not exist!')
|
||||
return
|
||||
# We shouldn't ever have two tags of the same name, so just get the first result
|
||||
await ctx.send("\u200B{}".format(tags[0]['result']))
|
||||
|
||||
@tag.command(name='add', aliases=['create', 'start'], no_pm=True)
|
||||
@utils.custom_perms(kick_members=True)
|
||||
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>
|
||||
|
||||
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'"""
|
||||
try:
|
||||
# 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
|
||||
except AttributeError:
|
||||
await ctx.send(
|
||||
"Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
|
||||
return
|
||||
# If our regex failed to find the content (aka they provided the wrong format)
|
||||
if len(tag) == 0 or len(tag_result) == 0:
|
||||
await ctx.send(
|
||||
"Please provide the format for the tag in: {}tag add <tag> - <result>".format(ctx.prefix))
|
||||
return
|
||||
|
||||
# Make sure the tag created does not mention everyone/here
|
||||
if '@everyone' in tag_result or '@here' in tag_result:
|
||||
await ctx.send("You cannot create a tag that mentions everyone!")
|
||||
return
|
||||
entry = {'server_id': ctx.message.guild.id, 'tag': tag, 'result': tag_result}
|
||||
r_filter = lambda row: (row['server_id'] == ctx.message.guild.id) & (row['tag'] == tag)
|
||||
# Try to create new entry first, if that fails (it already exists) then we update it
|
||||
if await utils.filter_content('tags', entry, r_filter):
|
||||
await ctx.send(
|
||||
"I have just added the tag `{0}`! You can call this tag by entering !tag {0}".format(tag))
|
||||
EXAMPLE: !tag butts
|
||||
RESULT: Whatever you setup for the butts tag!!"""
|
||||
tags = await utils.get_content('tags', str(ctx.message.guild.id))
|
||||
if tags:
|
||||
for t in tags['tags']:
|
||||
if t['trigger'] == tag:
|
||||
await ctx.send(t['result'])
|
||||
return
|
||||
await ctx.send("There is no tag called {}".format(tag))
|
||||
else:
|
||||
await ctx.send("That tag already exists!")
|
||||
await ctx.send("There are no tags setup on this server!")
|
||||
|
||||
|
||||
@tag.command(name='add', aliases=['create', 'setup'], no_pm=True)
|
||||
@utils.custom_perms(send_messages=True)
|
||||
async def add_tag(self, ctx):
|
||||
"""Use this to add a new tag that can be used in this server
|
||||
|
||||
EXAMPLE: !tag add
|
||||
RESULT: A follow-along in order to create a new tag"""
|
||||
def check(m):
|
||||
return m.channel == ctx.message.channel and m.author == ctx.message.author and len(m.content) > 0
|
||||
my_msg = await ctx.send("Ready to setup a new tag! What do you want the trigger for the tag to be?")
|
||||
|
||||
try:
|
||||
msg = await self.bot.wait_for("message", check=check, timeout=60)
|
||||
except asyncio.TimeoutError:
|
||||
await ctx.send("You took too long!")
|
||||
return
|
||||
|
||||
trigger = msg.content
|
||||
try:
|
||||
await my_msg.delete()
|
||||
await msg.delete()
|
||||
except discord.Forbidden:
|
||||
pass
|
||||
|
||||
my_msg = await ctx.send("Alright, your new tag can be called with {}!\n\nWhat do you want to be displayed with this tag?".format(trigger))
|
||||
|
||||
try:
|
||||
msg = await self.bot.wait_for("message", check=check, timeout=60)
|
||||
except asyncio.TimeoutError:
|
||||
await ctx.send("You took too long!")
|
||||
return
|
||||
|
||||
result = msg.content
|
||||
try:
|
||||
await my_msg.delete()
|
||||
await msg.delete()
|
||||
except discord.Forbidden:
|
||||
pass
|
||||
|
||||
# The different DB settings
|
||||
tag = {
|
||||
'author': str(ctx.message.author.id),
|
||||
'trigger': trigger,
|
||||
'result': result
|
||||
}
|
||||
entry = {
|
||||
'server_id': str(ctx.message.guild.id),
|
||||
'tags': [tag]
|
||||
}
|
||||
key = str(ctx.message.guild.id)
|
||||
if not await utils.add_content('tags', entry):
|
||||
await utils.update_content('tags', {'tags': r.row['tags'].append(tag)}, key)
|
||||
await ctx.send("I have just setup a new tag for this server! You can call your tag with {}".format(trigger))
|
||||
|
||||
|
||||
@tag.command(name='delete', aliases=['remove', 'stop'], no_pm=True)
|
||||
@utils.custom_perms(kick_members=True)
|
||||
@utils.custom_perms(send_messages=True)
|
||||
async def del_tag(self, ctx, *, tag: str):
|
||||
"""Use this to remove a tag from use for this server
|
||||
Format to delete a tag is !tag delete <tag>
|
||||
|
||||
EXAMPLE: !tag delete stupid_tag
|
||||
RESULT: Deletes that stupid tag"""
|
||||
await ctx.send("Temporarily disabled")
|
||||
# TODO: Fix tags, this will inherently fix this method
|
||||
"""r_filter = lambda row: (row['server_id'] == ctx.message.guild.id) & (row['tag'] == tag)
|
||||
if await utils.remove_content('tags', r_filter):
|
||||
await ctx.send('I have just removed the tag `{}`'.format(tag))
|
||||
tags = await utils.get_content('tags', str(ctx.message.guild.id))
|
||||
if tags:
|
||||
for t in tags['tags']:
|
||||
if t['trigger'] == tag:
|
||||
if ctx.message.author.permissions_in(ctx.message.channel).manage_server or str(ctx.message.author.id) == t['author']:
|
||||
tags['tags'].remove(t)
|
||||
await ctx.send("I have just removed the tag {}".format(trigger))
|
||||
else:
|
||||
await ctx.send("You don't own that tag! You can't remove it!")
|
||||
return
|
||||
else:
|
||||
await ctx.send(
|
||||
"The tag {} does not exist! You can't remove something if it doesn't exist...".format(tag))"""
|
||||
await ctx.send("There are no tags setup on this server!")
|
||||
|
||||
|
||||
def setup(bot):
|
||||
|
|
Loading…
Reference in a new issue