1
0
Fork 0
mirror of synced 2024-05-29 00:39:39 +12:00
Bonfire/cogs/tags.py

234 lines
8 KiB
Python
Raw Normal View History

2017-03-08 11:35:30 +13:00
from discord.ext import commands
2017-03-26 16:39:18 +13:00
import discord
2017-03-08 11:35:30 +13:00
import utils
2017-03-08 11:35:30 +13:00
2017-03-26 16:39:18 +13:00
import asyncio
2017-06-07 20:30:19 +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
@commands.command()
@commands.guild_only()
@utils.can_run(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
EXAMPLE: !tags
RESULT: All tags setup on this server"""
2019-01-28 15:58:39 +13:00
tags = await self.bot.db.fetch("SELECT trigger FROM tags WHERE guild=$1", ctx.guild.id)
if len(tags) > 0:
2017-06-07 20:30:19 +12:00
entries = [t['trigger'] for t in tags]
2018-10-05 09:36:58 +13:00
pages = utils.Pages(ctx, entries=entries)
2017-03-26 16:39:18 +13:00
await pages.paginate()
else:
await ctx.send("There are no tags setup on this server!")
@commands.command()
@commands.guild_only()
@utils.can_run(send_messages=True)
async def mytags(self, ctx):
"""Prints all the custom tags that this server that you own
EXAMPLE: !mytags
RESULT: All your tags setup on this server"""
2019-01-28 15:58:39 +13:00
tags = await self.bot.db.fetch(
"SELECT trigger FROM tags WHERE guild=$1 AND creator=$2",
ctx.guild.id,
ctx.author.id
)
if len(tags) > 0:
entries = [t['trigger'] for t in tags]
pages = utils.Pages(ctx, entries=entries)
await pages.paginate()
2017-06-07 20:30:19 +12:00
else:
2019-01-28 15:58:39 +13:00
await ctx.send("You have no tags on this server!")
2016-07-23 23:59:02 +12:00
@commands.group(invoke_without_command=True)
@commands.guild_only()
@utils.can_run(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
2017-03-26 16:39:18 +13:00
The format to call a custom tag is !tag <tag>
EXAMPLE: !tag butts
RESULT: Whatever you setup for the butts tag!!"""
2019-01-28 15:58:39 +13:00
tag = await self.bot.db.fetchrow(
"SELECT id, result FROM tags WHERE guild=$1 AND trigger=$2",
ctx.guild.id,
tag.lower().strip()
)
if tag:
await ctx.send("\u200B{}".format(tag['result']))
await self.bot.db.execute("UPDATE tags SET uses = uses + 1 WHERE id = $1", tag['id'])
2017-03-26 17:00:07 +13:00
else:
2019-01-28 15:58:39 +13:00
await ctx.send("There is no tag called {}".format(tag))
2016-07-23 23:59:02 +12:00
@tag.command(name='add', aliases=['create', 'setup'])
@commands.guild_only()
@utils.can_run(send_messages=True)
2017-03-26 16:39:18 +13:00
async def add_tag(self, ctx):
2016-07-23 23:59:02 +12:00
"""Use this to add a new tag that can be used in this server
2017-03-26 16:39:18 +13:00
EXAMPLE: !tag add
RESULT: A follow-along in order to create a new tag"""
2017-06-07 20:30:19 +12:00
2017-03-26 16:39:18 +13:00
def check(m):
return m.channel == ctx.message.channel and m.author == ctx.message.author and len(m.content) > 0
2017-06-07 20:30:19 +12:00
2017-03-26 16:39:18 +13:00
my_msg = await ctx.send("Ready to setup a new tag! What do you want the trigger for the tag to be?")
try:
2017-03-26 16:39:18 +13:00
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.lower().strip()
2019-01-28 15:58:39 +13:00
forbidden_tags = ['add', 'create', 'setup', 'edit', 'info', 'delete', 'remove', 'stop']
if len(trigger) > 100:
await ctx.send("Please keep tag triggers under 100 characters")
return
2019-01-28 15:58:39 +13:00
elif trigger.lower() in forbidden_tags:
2017-06-07 20:30:19 +12:00
await ctx.send(
"Sorry, but your tag trigger was detected to be forbidden. "
"Current forbidden tag triggers are: \n{}".format("\n".join(forbidden_tags)))
return
2019-01-28 15:58:39 +13:00
tag = await self.bot.db.fetchrow(
"SELECT result FROM tags WHERE guild=$1 AND trigger=$2",
ctx.guild.id,
trigger.lower().strip()
)
if tag:
await ctx.send("There is already a tag setup called {}!".format(trigger))
return
2017-03-26 16:39:18 +13:00
try:
await my_msg.delete()
await msg.delete()
except (discord.Forbidden, discord.HTTPException):
2017-03-26 16:39:18 +13:00
pass
2017-06-07 20:30:19 +12:00
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))
2017-03-26 16:39:18 +13:00
try:
msg = await self.bot.wait_for("message", check=check, timeout=60)
except asyncio.TimeoutError:
await ctx.send("You took too long!")
return
2017-03-26 16:39:18 +13:00
result = msg.content
try:
await my_msg.delete()
await msg.delete()
except (discord.Forbidden, discord.HTTPException):
2017-03-26 16:39:18 +13:00
pass
await ctx.send("I have just setup a new tag for this server! You can call your tag with {}".format(trigger))
2019-01-28 15:58:39 +13:00
await self.bot.db.execute(
"INSERT INTO tags(guild, creator, trigger, result) VALUES ($1, $2, $3, $4)",
ctx.guild.id,
ctx.author.id,
trigger,
result
)
2017-03-26 16:39:18 +13:00
@tag.command(name='edit')
@commands.guild_only()
@utils.can_run(send_messages=True)
2019-01-28 15:58:39 +13:00
async def edit_tag(self, ctx, *, trigger: str):
2017-03-26 16:59:08 +13:00
"""This will allow you to edit a tag that you have created
EXAMPLE: !tag edit this tag
RESULT: I'll ask what you want the new result to be"""
def check(m):
return m.channel == ctx.message.channel and m.author == ctx.message.author and len(m.content) > 0
2019-01-28 15:58:39 +13:00
tag = await self.bot.db.fetchrow(
"SELECT id, trigger FROM tags WHERE guild=$1 AND creator=$2 AND trigger=$3",
ctx.guild.id,
ctx.author.id,
trigger
)
if tag:
my_msg = await ctx.send(f"Alright, what do you want the new result for the tag {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
new_result = msg.content
try:
await my_msg.delete()
await msg.delete()
except (discord.Forbidden, discord.HTTPException):
pass
await ctx.send(f"Alright, the tag {trigger} has been updated")
await self.bot.db.execute("UPDATE tags SET result=$1 WHERE id=$2", new_result, tag['id'])
2017-03-26 16:59:08 +13:00
else:
2019-01-28 15:58:39 +13:00
await ctx.send(f"You do not have a tag called {trigger} on this server!")
2017-03-26 16:59:08 +13:00
@tag.command(name='delete', aliases=['remove', 'stop'])
@commands.guild_only()
@utils.can_run(send_messages=True)
2019-01-28 15:58:39 +13:00
async def del_tag(self, ctx, *, trigger: 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"""
2019-01-28 15:58:39 +13:00
tag = await self.bot.db.fetchrow(
"SELECT id FROM tags WHERE guild=$1 AND creator=$2 AND trigger=$3",
ctx.guild.id,
ctx.author.id,
trigger
)
if tag:
await ctx.send(f"I have just deleted the tag {trigger}")
await self.bot.db.execute("DELETE FROM tags WHERE id=$1", tag['id'])
else:
2019-01-28 15:58:39 +13:00
await ctx.send(f"You do not own a tag called {trigger} on this server!")
@tag.command(name="info")
@commands.guild_only()
@utils.can_run(send_messages=True)
async def info_tag(self, ctx, *, trigger: str):
"""Shows some information a bout the tag given"""
tag = await self.bot.db.fetchrow(
2019-02-18 08:47:34 +13:00
"SELECT creator, uses, trigger FROM tags WHERE guild=$1 AND trigger=$2",
2019-01-28 15:58:39 +13:00
ctx.guild.id,
trigger
)
2019-02-18 08:56:45 +13:00
if tag is not None:
embed = discord.Embed(title=tag['trigger'])
creator = ctx.guild.get_member(tag['creator'])
if creator:
embed.set_author(name=creator.display_name, url=creator.avatar_url)
embed.add_field(name="Uses", value=tag['uses'])
embed.add_field(name="Owner", value=creator.mention)
await ctx.send(embed=embed)
else:
await ctx.send(f"I cannot find a tag named '{trigger}'")
2019-01-28 15:58:39 +13:00
2016-07-24 00:01:16 +12:00
def setup(bot):
bot.add_cog(Tags(bot))