2017-09-24 18:09:02 +00:00
import asyncio
2017-10-13 02:34:48 +00:00
import json
2017-10-11 07:11:00 +00:00
import traceback as tb
2017-10-15 16:57:31 +00:00
from contextlib import suppress
2017-10-13 02:34:48 +00:00
2017-10-11 07:11:00 +00:00
import discord as d
2017-10-13 02:34:48 +00:00
from discord import errors as err
2017-09-24 18:09:02 +00:00
from discord import reaction
from discord . ext import commands
2017-10-02 23:37:58 +00:00
from discord . ext . commands import errors as errext
2017-10-13 02:34:48 +00:00
2017-10-02 19:20:04 +00:00
from cogs import tools
2017-09-24 18:09:02 +00:00
from misc import exceptions as exc
2017-10-13 02:34:48 +00:00
from misc import checks
2017-10-11 07:11:00 +00:00
from utils import utils as u
2017-10-13 02:34:48 +00:00
from utils import formatter , scraper
2017-10-11 07:11:00 +00:00
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
class MsG :
def __init__ ( self , bot ) :
self . bot = bot
2017-10-13 02:34:48 +00:00
self . LIMIT = 100
2017-10-15 18:20:11 +00:00
self . RATE_LIMIT = 2.1
2017-09-24 15:05:28 +00:00
2017-10-15 21:21:08 +00:00
self . favorites = u . setdefault ( ' cogs/favorites.pkl ' , { ' tags ' : set ( ) , ' posts ' : set ( ) } )
2017-10-13 02:34:48 +00:00
self . blacklists = u . setdefault (
2017-10-14 19:29:53 +00:00
' cogs/blacklists.pkl ' , { ' global_blacklist ' : set ( ) , ' guild_blacklist ' : { } , ' user_blacklist ' : { } } )
self . aliases = u . setdefault ( ' cogs/aliases.pkl ' , { } )
2017-09-24 15:05:28 +00:00
2017-10-13 02:34:48 +00:00
# Tag search
2017-10-15 21:21:08 +00:00
@commands.command ( aliases = [ ' rel ' ] , brief = ' e621 Related tag search ' , description = ' e621 | NSFW \n Return a link search for given tags ' )
2017-10-13 02:34:48 +00:00
@checks.del_ctx ( )
2017-10-15 20:18:09 +00:00
async def related ( self , ctx , tag = None ) :
related = [ ]
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
try :
if tag is None :
raise exc . MissingArgument
await ctx . trigger_typing ( )
tag_request = await u . fetch ( ' https://e621.net/tag/related.json ' , params = { ' tags ' : tag , ' type ' : ' general ' } , json = True )
2017-10-15 20:18:09 +00:00
for rel in tag_request . get ( tag , [ ] ) :
related . append ( rel [ 0 ] )
2017-10-14 03:40:38 +00:00
2017-10-15 20:18:09 +00:00
await ctx . send ( ' ✅ ` {} ` **related tags:** \n ``` \n {} ``` ' . format ( tag , formatter . tostring ( related ) ) )
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
except exc . MissingArgument :
await ctx . send ( ' ❌ **No tags given.** ' , delete_after = 10 )
2017-09-24 15:05:28 +00:00
2017-09-30 07:27:57 +00:00
# Tag aliases
2017-10-15 20:18:09 +00:00
@commands.command ( name = ' aliases ' , aliases = [ ' alias ' ] , brief = ' e621 Tag aliases ' , description = ' e621 | NSFW \n Search aliases for given tag ' )
2017-09-30 07:27:57 +00:00
@checks.del_ctx ( )
2017-10-15 18:20:11 +00:00
async def tag_aliases ( self , ctx , tag = None ) :
2017-09-30 07:27:57 +00:00
aliases = [ ]
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
try :
if tag is None :
raise exc . MissingArgument
await ctx . trigger_typing ( )
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
alias_request = await u . fetch ( ' https://e621.net/tag_alias/index.json ' , params = { ' aliased_to ' : tag , ' approved ' : ' true ' } , json = True )
for dic in alias_request :
aliases . append ( dic [ ' name ' ] )
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
await ctx . send ( ' ✅ ` {} ` **aliases:** \n ``` \n {} ``` ' . format ( tag , formatter . tostring ( aliases ) ) )
except exc . MissingArgument :
await ctx . send ( ' ❌ **No tags given.** ' , delete_after = 10 )
2017-09-30 07:27:57 +00:00
2017-09-24 15:05:28 +00:00
# Reverse image searches a linked image using the public iqdb
@commands.command ( name = ' reverse ' , aliases = [ ' rev ' , ' ris ' ] , brief = ' e621 Reverse image search ' , description = ' e621 | NSFW \n Reverse-search an image with given URL ' )
@checks.del_ctx ( )
2017-10-15 18:20:11 +00:00
async def reverse_image_search ( self , ctx , * urls ) :
2017-09-24 15:05:28 +00:00
try :
2017-10-15 18:20:11 +00:00
if not urls and not ctx . message . attachments :
raise exc . MissingArgument
2017-09-24 15:05:28 +00:00
await ctx . trigger_typing ( )
2017-10-15 17:30:40 +00:00
2017-10-15 18:20:11 +00:00
for url in urls :
try :
await ctx . trigger_typing ( )
await ctx . send ( ' ✅ **Probable match:** \n {} ' . format ( await scraper . check_match ( url ) ) )
except exc . MatchError as e :
await ctx . send ( ' ❌ **No probable match for** ` {} `**.** Make sure URLs direct to an image file. ' . format ( e ) , delete_after = 10 )
finally :
await asyncio . sleep ( self . RATE_LIMIT )
for attachment in ctx . message . attachments :
try :
await ctx . trigger_typing ( )
await ctx . send ( ' ✅ **Probable match:** \n {} ' . format ( await scraper . check_match ( attachment . url ) ) )
2017-10-15 17:30:40 +00:00
2017-10-15 18:20:11 +00:00
except exc . MatchError as e :
await ctx . send ( ' ❌ **No probable match for** ` {} `**.** Make sure URLs direct to an image file. ' . format ( e ) , delete_after = 10 )
2017-10-14 03:40:38 +00:00
2017-10-15 18:20:11 +00:00
finally :
await asyncio . sleep ( self . RATE_LIMIT )
2017-10-14 03:40:38 +00:00
2017-10-15 17:30:40 +00:00
except exc . MissingArgument :
await ctx . send ( ' ❌ **Invalid url or file.** ' , delete_after = 10 )
2017-10-13 02:34:48 +00:00
async def return_pool ( self , * , ctx , booru = ' e621 ' , query = [ ] ) :
def on_message ( msg ) :
2017-10-15 16:57:31 +00:00
if msg . content . lower ( ) == ' cancel ' and msg . author is ctx . author and msg . channel is ctx . channel :
2017-10-13 02:34:48 +00:00
raise exc . Abort
2017-10-15 16:57:31 +00:00
with suppress ( ValueError ) :
if int ( msg . content ) < = len ( pools ) and int ( msg . content ) > 0 and msg . author is ctx . author and msg . channel is ctx . channel :
2017-10-13 02:34:48 +00:00
return True
2017-10-15 16:57:31 +00:00
return False
2017-10-13 02:34:48 +00:00
posts = { }
2017-10-13 08:19:49 +00:00
pool = { }
2017-10-13 02:34:48 +00:00
pools = [ ]
pool_request = await u . fetch ( ' https:// {} .net/pool/index.json ' . format ( booru ) , params = { ' query ' : ' ' . join ( query ) } , json = True )
if len ( pool_request ) > 1 :
for pool in pool_request :
pools . append ( pool [ ' name ' ] )
match = await ctx . send ( ' ✅ **Multiple pools found.** Type in the correct match. \n ``` \n {} ``` \n or `cancel` to cancel. ' . format ( ' \n ' . join ( [ ' {} {} ' . format ( c , elem ) for c , elem in enumerate ( pools , 1 ) ] ) ) )
try :
selection = await self . bot . wait_for ( ' message ' , check = on_message , timeout = 10 * 60 )
except exc . Abort :
raise exc . Abort
finally :
await match . delete ( )
2017-10-13 08:19:49 +00:00
tempool = [ pool for pool in pool_request if pool [ ' name ' ]
== pools [ int ( selection . content ) - 1 ] ] [ 0 ]
2017-10-13 02:34:48 +00:00
await selection . delete ( )
2017-10-13 08:19:49 +00:00
pool = { ' name ' : tempool [ ' name ' ] , ' id ' : tempool [ ' id ' ] }
2017-10-13 02:34:48 +00:00
elif request :
2017-10-13 08:19:49 +00:00
temppool = pool_request [ 0 ]
pool = { ' name ' : pool_request [ 0 ] [ ' name ' ] , ' id ' : pool_request [ 0 ] [ ' id ' ] }
2017-10-13 02:34:48 +00:00
else :
raise exc . NotFound
page = 1
2017-10-13 08:19:49 +00:00
while len ( posts ) < tempool [ ' post_count ' ] :
posts_request = await u . fetch ( ' https:// {} .net/pool/show.json ' . format ( booru ) , params = { ' id ' : tempool [ ' id ' ] , ' page ' : page } , json = True )
2017-10-13 02:34:48 +00:00
for post in posts_request [ ' posts ' ] :
2017-10-13 08:19:49 +00:00
posts [ post [ ' id ' ] ] = { ' author ' : post [ ' author ' ] , ' url ' : post [ ' file_url ' ] }
2017-10-13 02:34:48 +00:00
page + = 1
2017-10-13 08:19:49 +00:00
return pool , posts
2017-10-13 02:34:48 +00:00
# Creates reaction-based paginator for linked pools
@commands.command ( name = ' pool ' , aliases = [ ' e6pp ' ] , brief = ' e621 pool paginator ' , description = ' e621 | NSFW \n Show pools in a page format ' , hidden = True )
@checks.del_ctx ( )
async def pool_paginator ( self , ctx , * kwords ) :
def on_react ( reaction , user ) :
2017-10-15 16:57:31 +00:00
if reaction . emoji == ' 🚫 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Abort
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' 📁 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Save
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' ⬅ ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Left
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' 🔢 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . GoTo
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' ➡ ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Right
else :
return False
def on_message ( msg ) :
2017-10-15 16:57:31 +00:00
with suppress ( ValueError ) :
if int ( msg . content ) < = len ( posts ) and msg . author is ctx . author and msg . channel is ctx . channel :
2017-10-13 02:34:48 +00:00
return True
2017-10-15 16:57:31 +00:00
return False
2017-10-13 02:34:48 +00:00
starred = [ ]
c = 1
try :
await ctx . trigger_typing ( )
2017-10-13 08:19:49 +00:00
pool , posts = await self . return_pool ( ctx = ctx , booru = ' e621 ' , query = kwords )
2017-10-13 02:34:48 +00:00
keys = list ( posts . keys ( ) )
values = list ( posts . values ( ) )
embed = d . Embed (
2017-10-13 08:19:49 +00:00
title = values [ c - 1 ] [ ' author ' ] , url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] ) , color = ctx . me . color ) . set_image ( url = values [ c - 1 ] [ ' url ' ] )
embed . set_author ( name = pool [ ' name ' ] ,
2017-10-15 16:57:31 +00:00
url = ' https://e621.net/pool/show?id= {} ' . format ( pool [ ' id ' ] ) , icon_url = ctx . author . avatar_url )
2017-10-13 02:34:48 +00:00
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 02:34:48 +00:00
paginator = await ctx . send ( embed = embed )
await paginator . add_reaction ( ' 🚫 ' )
await paginator . add_reaction ( ' 📁 ' )
await paginator . add_reaction ( ' ⬅ ' )
await paginator . add_reaction ( ' 🔢 ' )
await paginator . add_reaction ( ' ➡ ' )
await asyncio . sleep ( 1 )
2017-10-13 05:24:57 +00:00
while not self . bot . is_closed ( ) :
2017-10-13 02:34:48 +00:00
try :
await self . bot . wait_for ( ' reaction_add ' , check = on_react , timeout = 10 * 60 )
except exc . Left :
if c > 1 :
c - = 1
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
else :
await paginator . edit ( content = ' ❌ **First image.** ' )
except exc . GoTo :
await paginator . edit ( content = ' **Enter image number...** ' )
number = await self . bot . wait_for ( ' message ' , check = on_message , timeout = 10 * 60 )
c = int ( number . content )
await number . delete ( )
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
except exc . Save :
2017-10-13 08:19:49 +00:00
if values [ c - 1 ] [ ' url ' ] not in starred :
starred . append ( values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = ' **Image** ` {} ` **saved.** ' . format ( len ( starred ) ) )
except exc . Right :
if c < len ( keys ) :
c + = 1
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
except exc . Abort :
try :
await paginator . edit ( content = ' 🚫 **Exited paginator.** ' )
2017-10-14 19:29:53 +00:00
2017-10-13 02:34:48 +00:00
except UnboundLocalError :
await ctx . send ( ' 🚫 **Exited paginator.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except asyncio . TimeoutError :
try :
await ctx . send ( content = ' ❌ **Paginator timed out.** ' )
2017-10-14 19:29:53 +00:00
2017-10-13 02:34:48 +00:00
except UnboundLocalError :
await ctx . send ( ' ❌ **Paginator timed out.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . NotFound :
await ctx . send ( ' ❌ **Pool not found.** ' , delete_after = 10 )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . Timeout :
await ctx . send ( ' ❌ **Request timed out.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
finally :
for url in starred :
2017-10-15 16:57:31 +00:00
await ctx . author . send ( url )
2017-10-13 02:34:48 +00:00
if len ( starred ) > 5 :
await asyncio . sleep ( 2.1 )
# Messy code that checks image limit and tags in blacklists
2017-10-13 08:19:49 +00:00
async def check_return_posts ( self , * , ctx , booru = ' e621 ' , tags = [ ] , limit = 1 , previous = { } ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-13 02:34:48 +00:00
blacklist = set ( )
# Creates temp blacklist based on context
for tag in self . blacklists [ ' global_blacklist ' ] :
blacklist . update ( list ( self . aliases [ tag ] ) + [ tag ] )
2017-10-15 16:57:31 +00:00
for tag in self . blacklists [ ' guild_blacklist ' ] . get ( guild . id , { } ) . get ( ctx . channel . id , set ( ) ) :
2017-10-13 02:34:48 +00:00
blacklist . update ( list ( self . aliases [ tag ] ) + [ tag ] )
2017-10-15 16:57:31 +00:00
for tag in self . blacklists [ ' user_blacklist ' ] . get ( ctx . author . id , set ( ) ) :
2017-10-13 02:34:48 +00:00
blacklist . update ( list ( self . aliases [ tag ] ) + [ tag ] )
# Checks if tags are in local blacklists
if tags :
if len ( tags ) > 5 :
raise exc . TagBoundsError ( formatter . tostring ( tags [ 5 : ] ) )
for tag in tags :
if tag == ' swf ' or tag == ' webm ' or tag in blacklist :
raise exc . TagBlacklisted ( tag )
# Checks for blacklisted tags in endpoint blacklists - try/except is for continuing the parent loop
posts = { }
c = 0
while len ( posts ) < limit :
if c == 50 + limit * 3 :
raise exc . Timeout
request = await u . fetch ( ' https:// {} .net/post/index.json ' . format ( booru ) , params = { ' tags ' : ' , ' . join ( [ ' order:random ' ] + tags ) , ' limit ' : self . LIMIT } , json = True )
if len ( request ) == 0 :
raise exc . NotFound ( formatter . tostring ( tags ) )
if len ( request ) < limit :
limit = len ( request )
2017-10-14 19:29:53 +00:00
2017-10-13 02:34:48 +00:00
for post in request :
if ' swf ' in post [ ' file_ext ' ] or ' webm ' in post [ ' file_ext ' ] :
continue
try :
for tag in blacklist :
if tag in post [ ' tags ' ] :
raise exc . Continue
except exc . Continue :
continue
2017-10-13 08:19:49 +00:00
if post [ ' id ' ] not in posts . keys ( ) and post [ ' id ' ] not in previous . keys ( ) :
posts [ post [ ' id ' ] ] = { ' author ' : post [ ' author ' ] , ' url ' : post [ ' file_url ' ] }
2017-10-13 02:34:48 +00:00
if len ( posts ) == limit :
break
c + = 1
2017-10-14 19:29:53 +00:00
2017-10-13 02:34:48 +00:00
return posts
2017-10-11 07:12:13 +00:00
@commands.command ( name = ' e621p ' , aliases = [ ' e6p ' , ' 6p ' ] )
@checks.del_ctx ( )
@checks.is_nsfw ( )
async def e621_paginator ( self , ctx , * args ) :
2017-10-13 02:34:48 +00:00
def on_react ( reaction , user ) :
2017-10-15 16:57:31 +00:00
if reaction . emoji == ' 🚫 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Abort
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' 📁 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Save
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' ⬅ ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Left
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' 🔢 ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . GoTo
2017-10-15 16:57:31 +00:00
elif reaction . emoji == ' ➡ ' and reaction . message . content == paginator . content and ( user is ctx . author or user . id == u . config [ ' owner_id ' ] ) :
2017-10-13 02:34:48 +00:00
raise exc . Right
else :
return False
def on_message ( msg ) :
2017-10-15 16:57:31 +00:00
with suppress ( ValueError ) :
if int ( msg . content ) < = len ( posts ) and msg . author is ctx . author and msg . channel is ctx . channel :
2017-10-13 02:34:48 +00:00
return True
2017-10-15 16:57:31 +00:00
return False
2017-10-11 07:12:13 +00:00
args = list ( args )
2017-10-13 02:34:48 +00:00
limit = self . LIMIT / 5
starred = [ ]
c = 1
2017-10-11 07:12:13 +00:00
try :
await ctx . trigger_typing ( )
2017-10-13 02:34:48 +00:00
posts = await self . check_return_posts ( ctx = ctx , booru = ' e621 ' , tags = args , limit = limit )
keys = list ( posts . keys ( ) )
values = list ( posts . values ( ) )
embed = d . Embed (
2017-10-13 08:19:49 +00:00
title = values [ c - 1 ] [ ' author ' ] , url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] ) , color = ctx . me . color ) . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
embed . set_author ( name = formatter . tostring ( args , random = True ) ,
2017-10-15 16:57:31 +00:00
url = ' https://e621.net/post?tags= {} ' . format ( ' , ' . join ( args ) ) , icon_url = ctx . author . avatar_url )
2017-10-13 02:34:48 +00:00
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-11 07:12:13 +00:00
paginator = await ctx . send ( embed = embed )
await paginator . add_reaction ( ' 🚫 ' )
await paginator . add_reaction ( ' 📁 ' )
2017-10-13 02:34:48 +00:00
await paginator . add_reaction ( ' ⬅ ' )
await paginator . add_reaction ( ' 🔢 ' )
2017-10-11 07:12:13 +00:00
await paginator . add_reaction ( ' ➡ ' )
await asyncio . sleep ( 1 )
2017-10-13 05:24:57 +00:00
while not self . bot . is_closed ( ) :
2017-10-11 07:12:13 +00:00
try :
2017-10-13 02:34:48 +00:00
await self . bot . wait_for ( ' reaction_add ' , check = on_react , timeout = 10 * 60 )
2017-10-11 07:12:13 +00:00
except exc . Left :
if c > 1 :
c - = 1
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
else :
await paginator . edit ( content = ' ❌ **First image.** ' )
except exc . GoTo :
await paginator . edit ( content = ' **Enter image number...** ' )
number = await self . bot . wait_for ( ' message ' , check = on_message , timeout = 10 * 60 )
c = int ( number . content )
await number . delete ( )
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
2017-10-11 07:12:13 +00:00
except exc . Save :
2017-10-13 08:19:49 +00:00
if values [ c - 1 ] [ ' url ' ] not in starred :
starred . append ( values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = ' **Image** ` {} ` **saved.** ' . format ( len ( starred ) ) )
2017-10-11 07:12:13 +00:00
except exc . Right :
if c % limit == 0 :
await ctx . trigger_typing ( )
2017-10-13 02:34:48 +00:00
try :
posts . update ( await self . check_return_posts ( ctx = ctx , booru = ' e621 ' , tags = args , limit = limit , previous = posts ) )
2017-10-14 03:40:38 +00:00
2017-10-11 07:12:13 +00:00
except exc . NotFound :
await paginator . edit ( content = ' ❌ **No more images found.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
keys = list ( posts . keys ( ) )
values = list ( posts . values ( ) )
2017-10-11 07:12:13 +00:00
c + = 1
2017-10-13 08:19:49 +00:00
embed . title = values [ c - 1 ] [ ' author ' ]
2017-10-13 02:34:48 +00:00
embed . url = ' https://e621.net/post/show/ {} ' . format ( keys [ c - 1 ] )
embed . set_footer ( text = ' {} / {} ' . format ( c , len ( posts ) ) ,
2017-10-15 21:21:08 +00:00
icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-13 08:19:49 +00:00
embed . set_image ( url = values [ c - 1 ] [ ' url ' ] )
2017-10-13 02:34:48 +00:00
await paginator . edit ( content = None , embed = embed )
except exc . Abort :
await paginator . edit ( content = ' 🚫 **Exited paginator.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except asyncio . TimeoutError :
await paginator . edit ( content = ' ❌ **Paginator timed out.** ' )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . NotFound as e :
await ctx . send ( ' ❌ ` {} ` **not found.** ' . format ( e ) , delete_after = 10 )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . TagBlacklisted as e :
await ctx . send ( ' ❌ ` {} ` **blacklisted.** ' . format ( e ) , delete_after = 10 )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . TagBoundsError as e :
await ctx . send ( ' ❌ ` {} ` **out of bounds.** Tags limited to 5, currently. ' . format ( e ) , delete_after = 10 )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . Timeout :
await ctx . send ( ' ❌ **Request timed out.** ' )
2017-10-14 03:40:38 +00:00
2017-10-11 07:12:13 +00:00
finally :
2017-10-13 02:34:48 +00:00
for url in starred :
2017-10-15 16:57:31 +00:00
await ctx . author . send ( url )
2017-10-13 02:34:48 +00:00
if len ( starred ) > 5 :
await asyncio . sleep ( 2.1 )
2017-10-11 07:12:13 +00:00
@e621_paginator.error
async def e621_paginator_error ( self , ctx , error ) :
if isinstance ( error , errext . CheckFailure ) :
2017-10-15 21:21:08 +00:00
return await ctx . send ( ' ⛔️ {} **is not an NSFW channel.** ' . format ( ctx . channel . mention ) , delete_after = 10 )
2017-09-24 15:05:28 +00:00
# Searches for and returns images from e621.net given tags when not blacklisted
@commands.command ( aliases = [ ' e6 ' , ' 6 ' ] , brief = ' e621 | NSFW ' , description = ' e621 | NSFW \n Tag-based search for e621.net \n \n You can only search 5 tags and 6 images at once for now. \n e6 [tags...] ([# of images]) ' )
@checks.del_ctx ( )
@checks.is_nsfw ( )
async def e621 ( self , ctx , * args ) :
args = list ( args )
2017-10-11 07:12:13 +00:00
limit = 1
2017-09-24 15:05:28 +00:00
try :
await ctx . trigger_typing ( )
2017-10-11 07:12:13 +00:00
# Checks for, defines, and removes limit from args
for arg in args :
if len ( arg ) == 1 :
2017-10-15 16:57:31 +00:00
with suppress ( ValueError ) :
2017-10-14 01:57:57 +00:00
if int ( arg ) < = 6 and int ( arg ) > = 1 :
limit = int ( arg )
args . remove ( arg )
else :
raise exc . BoundsError ( arg )
2017-10-13 02:34:48 +00:00
posts = await self . check_return_posts ( ctx = ctx , booru = ' e621 ' , tags = args , limit = limit )
2017-10-13 08:19:49 +00:00
for ident , post in posts . items ( ) :
embed = d . Embed ( title = post [ ' author ' ] , url = ' https://e621.net/post/show/ {} ' . format ( ident ) ,
color = ctx . me . color ) . set_image ( url = post [ ' url ' ] )
2017-10-13 02:34:48 +00:00
embed . set_author ( name = formatter . tostring ( args , random = True ) ,
2017-10-15 16:57:31 +00:00
url = ' https://e621.net/post?tags= {} ' . format ( ' , ' . join ( args ) ) , icon_url = ctx . author . avatar_url )
2017-10-13 02:34:48 +00:00
embed . set_footer (
2017-10-15 21:21:08 +00:00
text = str ( ident ) , icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-11 07:12:13 +00:00
await ctx . send ( embed = embed )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . TagBlacklisted as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **blacklisted.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . BoundsError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **out of bounds.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . TagBoundsError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **out of bounds.** Tags limited to 5, currently. ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . NotFound as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **not found.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . Timeout :
await ctx . send ( ' ❌ **Request timed out.** ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
tools . command_dict . setdefault ( str ( ctx . author . id ) , { } ) . update (
2017-10-13 02:34:48 +00:00
{ ' command ' : ctx . command , ' args ' : ctx . args } )
2017-09-24 15:05:28 +00:00
@e621.error
async def e621_error ( self , ctx , error ) :
2017-10-11 07:12:13 +00:00
if isinstance ( error , errext . CheckFailure ) :
2017-10-15 21:21:08 +00:00
return await ctx . send ( ' ⛔️ {} **is not an NSFW channel.** ' . format ( ctx . channel . mention ) , delete_after = 10 )
2017-09-24 15:05:28 +00:00
# Searches for and returns images from e926.net given tags when not blacklisted
@commands.command ( aliases = [ ' e9 ' , ' 9 ' ] , brief = ' e926 | SFW ' , description = ' e926 | SFW \n Tag-based search for e926.net \n \n You can only search 5 tags and 6 images at once for now. \n e9 [tags...] ([# of images]) ' )
@checks.del_ctx ( )
async def e926 ( self , ctx , * args ) :
args = list ( args )
2017-10-11 07:12:13 +00:00
limit = 1
2017-09-24 15:05:28 +00:00
try :
await ctx . trigger_typing ( )
2017-10-11 07:12:13 +00:00
# Checks for, defines, and removes limit from args
for arg in args :
if len ( arg ) == 1 :
2017-10-15 16:57:31 +00:00
with suppress ( ValueError ) :
2017-10-14 01:57:57 +00:00
if int ( arg ) < = 6 and int ( arg ) > = 1 :
limit = int ( arg )
args . remove ( arg )
else :
raise exc . BoundsError ( arg )
2017-10-13 02:34:48 +00:00
posts = await self . check_return_posts ( ctx = ctx , booru = ' e926 ' , tags = args , limit = limit )
2017-10-13 08:19:49 +00:00
for ident , post in posts . items ( ) :
embed = d . Embed ( title = post [ ' author ' ] , url = ' https://e926.net/post/show/ {} ' . format ( ident ) ,
color = ctx . me . color ) . set_image ( url = post [ ' url ' ] )
2017-10-13 02:34:48 +00:00
embed . set_author ( name = formatter . tostring ( args , random = True ) ,
2017-10-15 16:57:31 +00:00
url = ' https://e621.net/post?tags= {} ' . format ( ' , ' . join ( args ) ) , icon_url = ctx . author . avatar_url )
2017-10-13 02:34:48 +00:00
embed . set_footer (
2017-10-15 21:21:08 +00:00
text = str ( ident ) , icon_url = ' http://lh6.ggpht.com/d3pNZNFCcJM8snBsRSdKUhR9AVBnJMcYYrR92RRDBOzCrxZMhuTeoGOQSmSEn7DAPQ=w300 ' )
2017-10-11 07:12:13 +00:00
await ctx . send ( embed = embed )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except exc . TagBlacklisted as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **blacklisted.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . BoundsError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **out of bounds.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . TagBoundsError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **out of bounds.** Tags limited to 5, currently. ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . NotFound as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **not found.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
except exc . Timeout :
await ctx . send ( ' ❌ **Request timed out.** ' )
2017-09-24 15:05:28 +00:00
2017-10-15 21:21:08 +00:00
@commands.group ( aliases = [ ' faves ' , ' f ' ] )
2017-10-14 19:29:53 +00:00
@checks.del_ctx ( )
async def favorites ( self , ctx ) :
pass
@favorites.error
async def favorites_error ( self , ctx , error ) :
pass
2017-10-15 21:21:08 +00:00
@favorites.group ( name = ' get ' , aliases = [ ' g ' ] )
2017-10-14 19:29:53 +00:00
async def _get_favorites ( self , ctx ) :
2017-10-15 21:21:08 +00:00
pass
@_get_favorites.command ( name = ' tags ' , aliases = [ ' t ' ] )
async def __get_favorites_tags ( self , ctx ) :
await ctx . send ( ' ⭐ {} ** \' s favorites tags:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( self . favorites . get ( ctx . author . id , { } ) . get ( ' tags ' , set ( ) ) ) ) )
@favorites.command ( name = ' posts ' , aliases = [ ' p ' ] )
async def __get_favorites_posts ( self , ctx ) :
pass
2017-10-14 19:29:53 +00:00
2017-10-15 21:21:08 +00:00
@favorites.group ( name = ' add ' , aliases = [ ' a ' ] )
async def _add_favorites ( self , ctx ) :
pass
@_add_favorites.command ( name = ' tags ' , aliases = [ ' t ' ] )
async def __add_favorites_tags ( self , ctx , * tags ) :
self . favorites . setdefault ( ctx . author . id , { } ) . setdefault ( ' tags ' , set ( ) ) . update ( tags )
2017-10-14 19:29:53 +00:00
u . dump ( self . favorites , ' cogs/favorites.pkl ' )
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} **added:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( tags ) ) )
2017-10-14 19:29:53 +00:00
2017-10-15 21:21:08 +00:00
@_add_favorites.command ( name = ' posts ' , aliases = [ ' p ' ] )
async def __add_favorites_posts ( self , ctx , * posts ) :
pass
@favorites.group ( name = ' remove ' , aliases = [ ' r ' ] )
async def _remove_favorites ( self , ctx ) :
pass
@_remove_favorites.command ( name = ' tags ' , aliases = [ ' t ' ] )
async def __remove_favorites_tags ( self , ctx , * tags ) :
2017-10-14 19:29:53 +00:00
try :
for tag in tags :
try :
2017-10-15 21:21:08 +00:00
self . favorites [ ctx . author . id ] . get ( ' tags ' , set ( ) ) . remove ( tag )
2017-10-14 19:29:53 +00:00
except KeyError :
raise exc . TagError ( tag )
u . dump ( self . favorites , ' cogs/favorites.pkl ' )
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} **removed:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( tags ) ) , delete_after = 5 )
2017-10-14 19:29:53 +00:00
except exc . TagError as e :
await ctx . send ( ' ❌ ` {} ` **not in favorites.** ' . format ( e ) , delete_after = 10 )
2017-10-15 21:21:08 +00:00
@_remove_favorites.command ( name = ' posts ' , aliases = [ ' p ' ] )
async def __remove_favorites_posts ( self , ctx ) :
pass
@favorites.group ( name = ' clear ' , aliases = [ ' c ' ] )
2017-10-14 19:29:53 +00:00
async def _clear_favorites ( self , ctx ) :
2017-10-15 21:21:08 +00:00
pass
@_clear_favorites.command ( name = ' tags ' , aliases = [ ' t ' ] )
async def __clear_favorites_tags ( self , ctx ) :
2017-10-15 16:57:31 +00:00
with suppress ( KeyError ) :
del self . favorites [ ctx . author . id ]
u . dump ( self . favorites , ' cogs/favorites.pkl ' )
2017-10-14 19:29:53 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} ** \' s favorites cleared.** ' . format ( ctx . author . mention ) )
2017-10-14 19:29:53 +00:00
2017-10-15 21:21:08 +00:00
@_clear_favorites.command ( name = ' posts ' , aliases = [ ' p ' ] )
async def __clear_favorites_posts ( self , ctx ) :
pass
2017-09-24 15:05:28 +00:00
# Umbrella command structure to manage global, channel, and user blacklists
@commands.group ( aliases = [ ' bl ' , ' b ' ] , brief = ' Manage blacklists ' , description = ' Blacklist base command for managing blacklists \n \n `bl get [blacklist]` to show a blacklist \n `bl set [blacklist] [tags]` to replace a blacklist \n `bl clear [blacklist]` to clear a blacklist \n `bl add [blacklist] [tags]` to add tags to a blacklist \n `bl remove [blacklist] [tags]` to remove tags from a blacklist ' , usage = ' [flag] [blacklist] ([tags]) ' )
@checks.del_ctx ( )
async def blacklist ( self , ctx ) :
if ctx . invoked_subcommand is None :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ **Use a flag to manage blacklists.** \n *Type* ` {} help bl` *for more info.* ' . format ( ctx . prefix ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
2017-10-15 21:21:08 +00:00
# @blacklist.error
# async def blacklist_error(self, ctx, error):
2017-10-14 03:40:38 +00:00
# if isinstance(error, KeyError):
# return await ctx.send('❌ **Blacklist does not exist.**', delete_after=10)
2017-09-24 15:05:28 +00:00
@blacklist.group ( name = ' get ' , aliases = [ ' g ' ] )
async def _get_blacklist ( self , ctx ) :
if ctx . invoked_subcommand is None :
2017-10-13 02:34:48 +00:00
await ctx . send ( ' ❌ **Invalid blacklist.** ' , delete_after = 10 )
2017-09-24 15:05:28 +00:00
@_get_blacklist.command ( name = ' global ' , aliases = [ ' gl ' , ' g ' ] )
async def __get_global_blacklist ( self , ctx ) :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' 🚫 **Global blacklist:** \n ``` \n {} ``` ' . format ( formatter . tostring ( self . blacklists [ ' global_blacklist ' ] ) ) )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_get_blacklist.command ( name = ' channel ' , aliases = [ ' ch ' , ' c ' ] )
async def __get_channel_blacklist ( self , ctx ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' 🚫 {} **blacklist:** \n ``` \n {} ``` ' . format ( ctx . channel . mention , formatter . tostring ( self . blacklists [ ' guild_blacklist ' ] . get ( guild . id , { } ) . get ( ctx . channel . id , set ( ) ) ) ) )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_get_blacklist.command ( name = ' me ' , aliases = [ ' m ' ] )
async def __get_user_blacklist ( self , ctx ) :
2017-10-15 16:57:31 +00:00
await ctx . send ( ' 🚫 {} ** \' s blacklist:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( self . blacklists [ ' user_blacklist ' ] . get ( ctx . author . id , set ( ) ) ) ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_get_blacklist.command ( name = ' here ' , aliases = [ ' h ' ] )
async def __get_here_blacklists ( self , ctx ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' 🚫 **__Blacklisted:__** \n \n **Global:** \n ``` \n {} ``` \n ** {} :** \n ``` \n {} ``` ' . format ( formatter . tostring ( self . blacklists [ ' global_blacklist ' ] ) , ctx . channel . mention , formatter . tostring ( self . blacklists [ ' guild_blacklist ' ] . get ( guild . id , { } ) . get ( ctx . channel . id , set ( ) ) ) ) )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_get_blacklist.group ( name = ' all ' , aliases = [ ' a ' ] )
async def __get_all_blacklists ( self , ctx ) :
if ctx . invoked_subcommand is None :
await ctx . send ( ' ❌ **Invalid blacklist.** ' )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@__get_all_blacklists.command ( name = ' guild ' , aliases = [ ' g ' ] )
@commands.has_permissions ( manage_channels = True )
async def ___get_all_guild_blacklists ( self , ctx ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-14 03:40:38 +00:00
await ctx . send ( ' 🚫 **__ {} blacklists:__** \n \n {} ' . format ( guild . name , formatter . dict_tostring ( self . blacklists [ ' guild_blacklist ' ] . get ( guild . id , { } ) ) ) )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@__get_all_blacklists.command ( name = ' user ' , aliases = [ ' u ' , ' member ' , ' m ' ] )
@commands.is_owner ( )
async def ___get_all_user_blacklists ( self , ctx ) :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' 🚫 **__User blacklists:__** \n \n {} ' . format ( formatter . dict_tostring ( self . blacklists [ ' user_blacklist ' ] ) ) )
2017-09-24 15:05:28 +00:00
@blacklist.group ( name = ' add ' , aliases = [ ' a ' ] )
async def _add_tags ( self , ctx ) :
if ctx . invoked_subcommand is None :
2017-10-13 02:34:48 +00:00
await ctx . send ( ' ❌ **Invalid blacklist.** ' , delete_after = 10 )
2017-09-24 15:05:28 +00:00
@_add_tags.command ( name = ' global ' , aliases = [ ' gl ' , ' g ' ] )
@commands.is_owner ( )
async def __add_global_tags ( self , ctx , * tags ) :
2017-10-15 16:57:31 +00:00
await ctx . trigger_typing ( )
2017-10-13 02:34:48 +00:00
self . blacklists [ ' global_blacklist ' ] . update ( tags )
for tag in tags :
alias_request = await u . fetch ( ' https://e621.net/tag_alias/index.json ' , params = { ' aliased_to ' : tag , ' approved ' : ' true ' } , json = True )
if alias_request :
2017-10-02 23:37:58 +00:00
for dic in alias_request :
2017-10-13 02:34:48 +00:00
self . aliases . setdefault ( tag , set ( ) ) . add ( dic [ ' name ' ] )
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
u . dump ( self . aliases , ' cogs/aliases.pkl ' )
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ✅ **Added to global blacklist:** \n ``` \n {} ``` ' . format ( formatter . tostring ( tags ) ) , delete_after = 5 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_add_tags.command ( name = ' channel ' , aliases = [ ' ch ' , ' c ' ] )
@commands.has_permissions ( manage_channels = True )
async def __add_channel_tags ( self , ctx , * tags ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-15 16:57:31 +00:00
await ctx . trigger_typing ( )
2017-10-13 02:34:48 +00:00
self . blacklists [ ' guild_blacklist ' ] . setdefault (
2017-10-15 16:57:31 +00:00
guild . id , { } ) . setdefault ( ctx . channel . id , set ( ) ) . update ( tags )
2017-10-13 02:34:48 +00:00
for tag in tags :
alias_request = await u . fetch ( ' https://e621.net/tag_alias/index.json ' , params = { ' aliased_to ' : tag , ' approved ' : ' true ' } , json = True )
if alias_request :
2017-10-02 23:37:58 +00:00
for dic in alias_request :
2017-10-13 02:34:48 +00:00
self . aliases . setdefault ( tag , set ( ) ) . add ( dic [ ' name ' ] )
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
u . dump ( self . aliases , ' cogs/aliases.pkl ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ **Added to** {} **blacklist:** \n ``` \n {} ``` ' . format ( ctx . channel . mention , formatter . tostring ( tags ) ) , delete_after = 5 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_add_tags.command ( name = ' me ' , aliases = [ ' m ' ] )
async def __add_user_tags ( self , ctx , * tags ) :
2017-10-15 16:57:31 +00:00
await ctx . trigger_typing ( )
2017-10-13 02:34:48 +00:00
2017-10-15 16:57:31 +00:00
self . blacklists [ ' user_blacklist ' ] . setdefault ( ctx . author . id , set ( ) ) . update ( tags )
2017-10-13 02:34:48 +00:00
for tag in tags :
alias_request = await u . fetch ( ' https://e621.net/tag_alias/index.json ' , params = { ' aliased_to ' : tag , ' approved ' : ' true ' } , json = True )
if alias_request :
2017-10-02 23:37:58 +00:00
for dic in alias_request :
2017-10-13 02:34:48 +00:00
self . aliases . setdefault ( tag , set ( ) ) . add ( dic [ ' name ' ] )
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
u . dump ( self . aliases , ' cogs/aliases.pkl ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} **added:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( tags ) ) , delete_after = 5 )
2017-09-24 15:05:28 +00:00
@blacklist.group ( name = ' remove ' , aliases = [ ' rm ' , ' r ' ] )
async def _remove_tags ( self , ctx ) :
if ctx . invoked_subcommand is None :
2017-10-13 02:34:48 +00:00
await ctx . send ( ' ❌ **Invalid blacklist.** ' , delete_after = 10 )
2017-09-24 15:05:28 +00:00
@_remove_tags.command ( name = ' global ' , aliases = [ ' gl ' , ' g ' ] )
@commands.is_owner ( )
async def __remove_global_tags ( self , ctx , * tags ) :
2017-10-02 23:37:58 +00:00
try :
for tag in tags :
2017-10-13 02:34:48 +00:00
try :
self . blacklists [ ' global_blacklist ' ] . remove ( tag )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except KeyError :
2017-10-02 23:37:58 +00:00
raise exc . TagError ( tag )
2017-10-14 03:40:38 +00:00
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
await ctx . send ( ' ✅ **Removed from global blacklist:** \n ``` \n {} ``` ' . format ( formatter . tostring ( tags ) ) , delete_after = 5 )
2017-10-14 03:40:38 +00:00
2017-10-02 23:37:58 +00:00
except exc . TagError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **not in blacklist.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_remove_tags.command ( name = ' channel ' , aliases = [ ' ch ' , ' c ' ] )
@commands.has_permissions ( manage_channels = True )
async def __remove_channel_tags ( self , ctx , * tags ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-14 03:40:38 +00:00
2017-10-02 23:37:58 +00:00
try :
for tag in tags :
2017-10-13 02:34:48 +00:00
try :
2017-10-15 16:57:31 +00:00
self . blacklists [ ' guild_blacklist ' ] [ guild . id ] [ ctx . channel . id ] . remove ( tag )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except KeyError :
2017-10-02 23:37:58 +00:00
raise exc . TagError ( tag )
2017-10-14 03:40:38 +00:00
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ **Removed from** {} **blacklist:** \n ``` \n {} ``` ' . format ( ctx . channel . mention , formatter . tostring ( tags ) , delete_after = 5 ) )
2017-10-14 03:40:38 +00:00
2017-10-02 23:37:58 +00:00
except exc . TagError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **not in blacklist.** ' . format ( e ) , delete_after = 10 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_remove_tags.command ( name = ' me ' , aliases = [ ' m ' ] )
async def __remove_user_tags ( self , ctx , * tags ) :
2017-10-02 23:37:58 +00:00
try :
for tag in tags :
2017-10-13 02:34:48 +00:00
try :
2017-10-15 16:57:31 +00:00
self . blacklists [ ' user_blacklist ' ] [ ctx . author . id ] . remove ( tag )
2017-10-14 03:40:38 +00:00
2017-10-13 02:34:48 +00:00
except KeyError :
2017-10-02 23:37:58 +00:00
raise exc . TagError ( tag )
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} **removed:** \n ``` \n {} ``` ' . format ( ctx . author . mention , formatter . tostring ( tags ) ) , delete_after = 5 )
2017-10-14 03:40:38 +00:00
2017-10-02 23:37:58 +00:00
except exc . TagError as e :
2017-10-14 03:40:38 +00:00
await ctx . send ( ' ❌ ` {} ` **not in blacklist.** ' . format ( e ) , delete_after = 10 )
2017-09-24 15:05:28 +00:00
@blacklist.group ( name = ' clear ' , aliases = [ ' cl ' , ' c ' ] )
async def _clear_blacklist ( self , ctx ) :
if ctx . invoked_subcommand is None :
2017-10-13 02:34:48 +00:00
await ctx . send ( ' ❌ **Invalid blacklist.** ' , delete_after = 10 )
2017-09-24 15:05:28 +00:00
@_clear_blacklist.command ( name = ' global ' , aliases = [ ' gl ' , ' g ' ] )
@commands.is_owner ( )
async def __clear_global_blacklist ( self , ctx ) :
2017-10-15 16:57:31 +00:00
self . blacklists [ ' global_blacklist ' ] . clear ( )
2017-10-14 19:29:53 +00:00
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
2017-10-14 03:40:38 +00:00
2017-09-24 15:05:28 +00:00
await ctx . send ( ' ✅ **Global blacklist cleared.** ' , delete_after = 5 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_clear_blacklist.command ( name = ' channel ' , aliases = [ ' ch ' , ' c ' ] )
@commands.has_permissions ( manage_channels = True )
async def __clear_channel_blacklist ( self , ctx ) :
2017-10-15 01:55:10 +00:00
guild = ctx . guild if isinstance (
ctx . guild , d . Guild ) else ctx . channel
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
with suppress ( KeyError ) :
del self . blacklists [ ' guild_blacklist ' ] [ guild . id ] [ ctx . channel . id ]
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} **blacklist cleared.** ' . format ( ctx . channel . mention ) , delete_after = 5 )
2017-10-13 02:34:48 +00:00
2017-09-24 15:05:28 +00:00
@_clear_blacklist.command ( name = ' me ' , aliases = [ ' m ' ] )
async def __clear_user_blacklist ( self , ctx ) :
2017-10-15 16:57:31 +00:00
with suppress ( KeyError ) :
del self . blacklists [ ' user_blacklist ' ] [ ctx . author . id ]
u . dump ( self . blacklists , ' cogs/blacklists.pkl ' )
2017-10-14 03:40:38 +00:00
2017-10-15 16:57:31 +00:00
await ctx . send ( ' ✅ {} ** \' s blacklist cleared.** ' . format ( ctx . author . mention ) , delete_after = 5 )