diff --git a/old.py b/old.py new file mode 100644 index 0000000..9a45216 --- /dev/null +++ b/old.py @@ -0,0 +1,160 @@ +"""Old unused code or work in progress""" + +""" +Used for testing if the bot is responsive to inner main command requests + +@client.command() +async def ctest(ctx): + try: + await ctx.message.delete() + except discord.HTTPException: + pass + await ctx.send(f'I heard you! {ctx.author.mention}', delete_after=5) +""" + + + +""" +@client.event +async def on_command_error(ctx, error): + error = getattr(error, 'original', error) + print("".join(traceback.format_exception(type(error), error, error.__traceback__))) + if isinstance(error, commands.CommandOnCooldown): + await ctx.send(f'Whoa calm down! {error}', delete_after=5) +""" + + +""" +@client.event +async def on_member_join(member): + await member.channel.send(f'Welcome {member.mention} to {member.guild.name}!') + await client.process_commands(message) #So that events don't disable commands +""" + +""" +Ignores commands with local handlers +if hasattr(ctx.command, 'on_error'): + return +""" + +""" +@commands.command(aliases=['danbooru', 'danb', 'donmai', 'animepics']) +@commands.cooldown(2, 4, commands.BucketType.user) +async def danbooru_post(self, ctx, f_arg=" ", *args): + user_tag = f_arg + if f_arg != " ": + for arg in args: + user_tag = str(user_tag) + "_" + str(arg) + urllib.parse.quote(user_tag) + + sessionLink = f'https://danbooru.donmai.us/post/index.json?tags={user_tag}&limit=1000' + async with aiohttp.ClientSession() as session: + async with session.get(sessionLink) as req: + if req.status == 200: + resp = json.loads(await req.text()) + # pprint (resp) #testing json resp + resultsCount = len(resp) + # print (resultsCount) #testing count + if resultsCount <= 1: + embed = discord.Embed(title=f"{ctx.author.name}'s SFW search on Danbooru for {user_tag}", + description=f'No posts found for the tags: "{user_tag}".', + color=colorCode[2]) + await ctx.send(embed=embed) + return + postId = random.randint(0, resultsCount - 1) + # print (postId) #testing post id + counter = 0 + while resp[postId]['rating'] != 's': + counter += 1 + postId = random.randint(0, resultsCount - 1) + if counter >= 20: + embed = discord.Embed(title=f"{ctx.author.name}'s SFW search on Danbooru for {user_tag}", + description=f'No SFW posts found for the tags: "{user_tag}".', + color=colorCode[2]) + await ctx.send(embed=embed) + return + + postTags = resp[postId]['tags'].replace(" ", ", ") + while len(postTags) > 185: + postTags = postTags[:185] # if too many tags cut them in half + # print(postTags) #testing + postTags = postTags.rsplit(', ', 1)[0] + if len(postTags) > 2000: + postTags = postTags[:len(postTags) / 2] # if too many tags cut them in half + if user_tag == " ": + user_tag = 'anything' + + embed = discord.Embed(title=f"{ctx.author.name}'s SFW search on Danbooru for {user_tag}", + description=f'**Tags**: {postTags}', + color=colorCode[0]) + embed.add_field(name='Image Link', value=f"[Link]({resp[postId]['file_url']})", inline=True) + embed.add_field(name='Rating', value=resp[postId]['rating'], inline=True) + embed.add_field(name='Height', value=resp[postId]['height'], inline=True) + embed.add_field(name='Width', value=resp[postId]['width'], inline=True) + embed.set_image(url=resp[postId]['file_url']) + await ctx.send(embed=embed) + + else: + print(f'Api Req Failed: {req.reason}, {req.status}') + + +@commands.command(aliases=['danbooruh', 'danbh', 'donmaih', 'danbnsfw', 'danbooruhen', 'hen']) +@commands.cooldown(2, 4, commands.BucketType.user) +async def danbooru_post_nsfw(self, ctx, f_arg=" ", *args): + if ctx.message.guild != None: + if ctx.message.channel.is_nsfw() == False: + await ctx.send('You can only use this command in NSFW channels.') + return + user_tag = f_arg + if f_arg != " ": + for arg in args: + user_tag = str(user_tag) + "_" + str(arg) + urllib.parse.quote(user_tag) + sessionLink = f'https://danbooru.donmai.us/post/index.json?tags={user_tag}&limit=500' + async with aiohttp.ClientSession() as session: + async with session.get(sessionLink) as req: + if req.status == 200: + resp = json.loads(await req.text()) + # pprint (resp) #testing json resp + resultsCount = len(resp) + # print (resultsCount) #testing count + if resultsCount <= 1: + embed = discord.Embed(title=f"{ctx.author.name}'s NSFW search on Danbooru for {user_tag}", + description=f'No posts found for the tags: "{user_tag}".', + color=colorCode[2]) + await ctx.send(embed=embed) + return + postId = random.randint(0, resultsCount - 1) + # print (postId) #testing post id + counter = 0 + while resp[postId]['rating'] == 's': + counter += 1 + postId = random.randint(0, resultsCount - 1) + if counter >= 20: + embed = discord.Embed(title=f"{ctx.author.name}'s NSFW search on Danbooru for {user_tag}", + description=f'No NSFW posts found for the tags: "{user_tag}".', + color=colorCode[2]) + await ctx.send(embed=embed) + return + + postTags = resp[postId]['tags'].replace(" ", ", ") + while len(postTags) > 185: + postTags = postTags[:185] # if too many tags cut them in half + # print(postTags) #testing + postTags = postTags.rsplit(', ', 1)[0] + if user_tag == " ": + user_tag = 'anything' + + embed = discord.Embed(title=f"{ctx.author.name}'s NSFW search on Danbooru for {user_tag}", + description=f'**Tags**: {postTags}', + color=colorCode[3]) + embed.add_field(name='Image Link', value=f"[Link]({resp[postId]['file_url']})", inline=True) + embed.add_field(name='Rating', value=resp[postId]['rating'], inline=True) + embed.add_field(name='Height', value=resp[postId]['height'], inline=True) + embed.add_field(name='Width', value=resp[postId]['width'], inline=True) + embed.set_image(url=resp[postId]['file_url']) + await ctx.send(embed=embed) + + else: + print(f'Api Req Failed: {req.reason}, {req.status}') +"""