2017-09-24 18:09:02 +00:00
import asyncio
2017-11-20 04:18:05 +00:00
from datetime import datetime as dt
2017-10-13 02:28:16 +00:00
import json
2017-11-20 03:59:37 +00:00
import logging as log
2017-10-02 21:29:39 +00:00
import subprocess
2017-10-11 06:57:17 +00:00
import sys
2017-10-13 02:28:16 +00:00
import traceback as tb
2017-10-16 20:52:54 +00:00
from contextlib import suppress
2017-10-21 20:40:06 +00:00
from pprint import pprint
2017-11-20 04:18:05 +00:00
from hurry . filesize import size , alternative
from urllib . parse import urlparse
2017-10-13 02:28:16 +00:00
import discord as d
2017-11-09 03:32:06 +00:00
from discord import errors as err
2017-09-24 18:09:02 +00:00
from discord import utils
2017-11-20 11:18:42 +00:00
from discord . ext import commands as cmds
2017-10-15 03:41:42 +00:00
from discord . ext . commands import errors as errext
2017-10-13 02:28:16 +00:00
2017-09-24 18:09:02 +00:00
from misc import exceptions as exc
2017-10-13 02:28:16 +00:00
from misc import checks
2017-10-11 06:57:17 +00:00
from utils import utils as u
2017-09-24 18:09:02 +00:00
2017-11-20 03:59:37 +00:00
log . basicConfig ( level = log . WARNING )
2017-10-11 06:57:17 +00:00
2017-10-13 02:28:16 +00:00
2017-11-20 11:18:42 +00:00
# class HelpFormatter(cmds.HelpFormatter):
2017-11-20 10:57:41 +00:00
#
# async def format(self):
2017-11-20 11:18:42 +00:00
# self._paginator = cmds.Paginator()
2017-11-20 10:57:41 +00:00
#
# # we need a padding of ~80 or so
#
# description = self.command.description if not self.is_cog() else inspect.getdoc(self.command)
#
# if description:
# # <description> portion
# self._paginator.add_line(description, empty=True)
#
2017-11-20 11:18:42 +00:00
# if isinstance(self.command, cmds.Command):
2017-11-20 10:57:41 +00:00
# # <signature portion>
# signature = self.get_command_signature()
# self._paginator.add_line(signature, empty=True)
#
# # <long doc> section
# if self.command.help:
# self._paginator.add_line(self.command.help, empty=True)
#
# # end it here if it's just a regular command
# if not self.has_subcommands():
# self._paginator.close_page()
# return self._paginator.pages
#
# max_width = self.max_name_size
2017-11-07 04:51:07 +00:00
2017-10-20 20:16:05 +00:00
def get_prefix ( bot , message ) :
2017-11-20 10:58:40 +00:00
return u . settings [ ' prefixes ' ] . get ( message . guild . id , u . config [ ' prefix ' ] )
2017-10-20 20:16:05 +00:00
2017-11-20 11:18:42 +00:00
bot = cmds . Bot ( command_prefix = get_prefix , formatter = cmds . HelpFormatter ( show_check_failure = True ) , description = ' Modumind - A booru bot with a side of management \n \n S for single command \n G for group command ' , help_attrs = { ' aliases ' : [ ' h ' ] } , pm_help = None )
2017-10-20 20:16:05 +00:00
2017-11-20 11:01:23 +00:00
@bot.command ( help = ' help ' , brief = ' brief ' , description = ' description ' , usage = ' usage ' )
async def test ( ctx ) :
pass
2017-10-13 02:28:16 +00:00
2017-10-14 03:38:58 +00:00
# Send and print ready message to #testing and console after logon
2017-10-20 20:17:16 +00:00
2017-09-24 15:05:28 +00:00
@bot.event
async def on_ready ( ) :
2017-10-20 20:16:05 +00:00
from cogs import booru , info , management , owner , tools
2017-10-14 03:59:14 +00:00
2017-10-20 20:17:16 +00:00
for cog in ( tools . Utils ( bot ) , owner . Bot ( bot ) , owner . Tools ( bot ) , management . Administration ( bot ) , info . Info ( bot ) , booru . MsG ( bot ) ) :
bot . add_cog ( cog )
print ( f ' COG : { type ( cog ) . __name__ } ' )
2017-10-11 06:57:17 +00:00
2017-10-20 20:16:05 +00:00
# bot.loop.create_task(u.clear(booru.temp_urls, 30*60))
2017-10-11 06:57:17 +00:00
2017-10-20 20:19:58 +00:00
if u . config [ ' playing ' ] is not ' None ' :
await bot . change_presence ( game = d . Game ( name = u . config [ ' playing ' ] ) )
else :
await bot . change_presence ( game = None )
2017-10-16 18:07:10 +00:00
2017-10-31 20:13:54 +00:00
print ( ' \n > > > > > > > > > \n C O N N E C T E D : {} \n > > > > > > > > > \n ' . format ( bot . user . name ) )
2017-11-06 07:00:58 +00:00
await bot . get_channel ( u . config [ ' info_channel ' ] ) . send ( ' **Started** \N{BLACK SUN WITH RAYS} . ' )
2017-10-20 20:19:58 +00:00
# u.notify('C O N N E C T E D')
if u . temp :
2017-10-31 03:28:37 +00:00
channel = bot . get_channel ( u . temp [ ' startup_chan ' ] )
message = await channel . get_message ( u . temp [ ' startup_msg ' ] )
2017-11-06 07:00:58 +00:00
await message . add_reaction ( ' \N{WHITE HEAVY CHECK MARK} ' )
2017-10-20 20:19:58 +00:00
u . temp . clear ( )
2017-09-24 15:05:28 +00:00
2017-10-13 02:28:16 +00:00
2017-10-20 20:17:55 +00:00
@bot.event
async def on_message ( message ) :
2017-10-29 21:54:50 +00:00
if message . author is not bot . user :
await bot . process_commands ( message )
2017-10-20 20:17:55 +00:00
2017-10-14 19:29:01 +00:00
@bot.event
2017-10-15 03:41:42 +00:00
async def on_error ( error , * args , * * kwargs ) :
2017-10-20 20:17:55 +00:00
print ( ' \n ! ! ! ! ! \n E R R O R : {} \n ! ! ! ! ! \n ' . format ( error ) , file = sys . stderr )
tb . print_exc ( )
2017-11-20 04:01:06 +00:00
await bot . get_user ( u . config [ ' owner_id ' ] ) . send ( ' **ERROR** \N{WARNING SIGN} \n ``` \n {} ``` ' . format ( error ) )
2017-11-06 07:00:58 +00:00
await bot . get_channel ( u . config [ ' info_channel ' ] ) . send ( ' **ERROR** \N{WARNING SIGN} \n ``` \n {} ``` ' . format ( error ) )
2017-10-20 20:19:58 +00:00
if u . temp :
2017-10-31 03:28:37 +00:00
channel = bot . get_channel ( u . temp [ ' startup_chan ' ] )
message = await channel . get_message ( u . temp [ ' startup_msg ' ] )
2017-11-06 07:00:58 +00:00
await message . add_reaction ( ' \N{WARNING SIGN} ' )
2017-10-20 20:19:58 +00:00
u . temp . clear ( )
2017-10-20 20:17:55 +00:00
# u.notify('E R R O R')
await bot . logout ( )
u . close ( bot . loop )
2017-10-14 19:29:01 +00:00
2017-10-13 02:28:16 +00:00
@bot.event
async def on_command_error ( ctx , error ) :
2017-11-09 03:32:06 +00:00
if isinstance ( error , err . NotFound ) :
pass
elif isinstance ( error , errext . CheckFailure ) :
await ctx . send ( ' **Insufficient permissions** ' , delete_after = 10 )
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{NO ENTRY} ' )
2017-10-20 20:19:58 +00:00
elif isinstance ( error , errext . CommandNotFound ) :
print ( ' INVALID COMMAND : {} ' . format ( error ) , file = sys . stderr )
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{BLACK QUESTION MARK ORNAMENT} ' )
2017-10-20 20:19:58 +00:00
else :
print ( ' \n ! ! ! ! ! ! ! ! ! ! ! ! \n C O M M A N D E R R O R : {} \n ! ! ! ! ! ! ! ! ! ! ! ! \n ' . format (
error ) , file = sys . stderr )
tb . print_exception ( type ( error ) , error , error . __traceback__ , file = sys . stderr )
2017-11-09 03:32:28 +00:00
await bot . get_user ( u . config [ ' owner_id ' ] ) . send ( ' **COMMAND ERROR** \N{WARNING SIGN} ` {} ` from {} in {} \n ``` \n {} ``` ' . format ( ctx . message . content , ctx . author . mention , ctx . channel . mention , ' ' . join ( tb . format_exception ( type ( error ) , error , error . __traceback__ ) ) ) )
await bot . get_channel ( u . config [ ' info_channel ' ] ) . send ( ' **COMMAND ERROR** \N{WARNING SIGN} ` {} ` from {} in {} \n ``` \n {} ``` ' . format ( ctx . message . content , ctx . author . mention , ctx . channel . mention , error ) )
2017-10-20 20:19:58 +00:00
await exc . send_error ( ctx , error )
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{WARNING SIGN} ' )
2017-10-20 20:19:58 +00:00
# u.notify('C O M M A N D E R R O R')
2017-10-13 02:28:16 +00:00
2017-11-19 16:40:08 +00:00
@bot.event
async def on_command_completion ( ctx ) :
2017-11-20 11:03:26 +00:00
with suppress ( err . NotFound ) :
with suppress ( AttributeError ) :
if ctx . guild . id in u . settings [ ' del_ctx ' ] and ctx . me . permissions_in ( ctx . channel ) . manage_messages and isinstance ( ctx . message . channel , d . TextChannel ) :
await ctx . message . delete ( )
await ctx . message . add_reaction ( ' \N{WHITE HEAVY CHECK MARK} ' )
2017-11-19 16:40:08 +00:00
if ctx . command . name != ' lastcommand ' :
u . last_commands [ ctx . author . id ] = ctx
2017-11-20 07:15:20 +00:00
@bot.event
async def on_guild_remove ( guild ) :
print ( f ' LEFT : { guild . name } ' )
for task , idents in u . tasks . items ( ) :
for channel in guild . channels :
if channel . id in idents :
idents . remove ( channel . id )
print ( f ' STOPPED : { task } in # { channel . id } ' )
u . dump ( u . tasks , ' cogs/tasks.pkl ' )
2017-10-19 08:39:41 +00:00
# d.opus.load_opus('opus')
async def wait ( voice ) :
2017-10-20 20:19:58 +00:00
asyncio . sleep ( 5 )
await voice . disconnect ( )
2017-10-19 08:39:41 +00:00
def after ( voice , error ) :
2017-10-20 20:19:58 +00:00
coro = voice . disconnect ( )
future = asyncio . run_coroutine_threadsafe ( coro , voice . loop )
future . result ( )
2017-10-19 08:39:41 +00:00
2017-11-20 04:18:05 +00:00
# suggested = u.setdefault('cogs/suggested.pkl', {'last_update': 'None', 'tags': {}, 'total': 0})
2017-10-11 06:57:17 +00:00
@bot.command ( name = ' ,test ' , hidden = True )
2017-11-20 11:18:42 +00:00
@cmds.is_owner ( )
2017-11-20 04:18:05 +00:00
async def test ( ctx ) :
post = await u . fetch ( ' https://e621.net/post/show.json?id=1145042 ' , json = True )
tags = [ ]
if post [ ' tags ' ] :
temptags = post [ ' tags ' ] . split ( ' ' )
cis = [ ]
for tag in suggested :
pass
for tag in temptags :
tags . append ( f ' [ { tag } ](https://e621.net/post?tags= { tag } ) ' )
# tags = ' '.join(tags)
else :
tags = ' None '
if post [ ' description ' ] :
post_description = post [ ' description ' ] if len ( post [ ' description ' ] ) < 200 else f ' { post [ " description " ] [ : 200 ] } ... '
else :
post_description = ' None '
title = ' , ' . join ( post [ ' artist ' ] )
description = f ' posted by: *[ { post [ " author " ] } ](https://e621.net/post?tags=user: { post [ " author " ] } )* '
url = f ' https://e621.net/post?tags= { " , " . join ( post [ " artist " ] ) } '
# timestamp = dt.utcnow()
color = ctx . me . color
footer = { ' text ' : post [ ' score ' ] , ' icon_url ' : ' https://images-ext-1.discordapp.net/external/W2k0ZzhU7ngvN_-CdqAa3H3FmkfCNYQTxPG_DsvacB4/https/emojipedia-us.s3.amazonaws.com/thumbs/320/twitter/103/sparkles_2728.png ' }
# image = 'https://e621.net/post/show/54360'
thumbnail = post [ ' file_url ' ]
author = { ' name ' : post [ ' id ' ] , ' url ' : f ' https://e621.net/post/show/ { post [ " id " ] } ' , ' icon_url ' : ctx . author . avatar_url }
fields = [ ]
names = ( ' File ' , ' Sources ' , ' Description ' , ' tags ' , ' tags (ext.) ' )
values = ( f ' [ { post [ " md5 " ] } ]( { post [ " file_url " ] } ) | [ { post [ " file_ext " ] } ](https://e621.net/post?tags=type: { post [ " file_ext " ] } ) \n \n **Size** [ { size ( post [ " file_size " ] , system = alternative ) } ](https://e621.net/post?tags=filesize: { post [ " file_size " ] } ) \n **Resolution** [ { post [ " width " ] } x { post [ " height " ] } ](https://e621.net/post?tags=width: { post [ " width " ] } ,height: { post [ " height " ] } ) | [ { u . get_aspectratio ( post [ " width " ] , post [ " height " ] ) } ](https://e621.net/post?tags=ratio: { post [ " width " ] / post [ " height " ] : .2f } ) ' , ' \n ' . join ( [ f ' [ { urlparse ( source ) . netloc } ]( { source } ) ' for source in post [ ' sources ' ] ] ) , post_description , ' ' . join ( tags [ : 20 ] ) , ' ' . join ( tags [ 20 : ] ) )
inlines = ( False , False , False , True , True )
for name , value , inline in zip ( names , values , inlines ) :
fields . append ( { ' name ' : name , ' value ' : value , ' inline ' : inline } )
embed = u . generate_embed ( ctx , title = title , description = description , url = url , colour = color , footer = footer , thumbnail = thumbnail , author = author , fields = fields )
await ctx . send ( embed = embed )
# print(ctx.args)
# print(ctx.kwargs)
2017-10-31 03:36:28 +00:00
# if '<:N_:368917475531816962>' in message:
# await ctx.send('<:N_:368917475531816962>')
2017-10-28 01:06:41 +00:00
# logs = []
# async for entry in ctx.guild.audit_logs(limit=None, action=d.AuditLogAction.message_delete):
# logs.append(
# f'@{entry.user.name} deleted {entry.extra.count} messages from @{entry.target.name} in #{entry.extra.channel.name}')
# pprint(logs)
2017-10-21 20:40:06 +00:00
# channel = bot.get_channel(int(cid))
# voice = await channel.connect()
# voice.play(d.AudioSource, after=lambda: after(voice))
2017-09-24 15:05:28 +00:00
2017-10-14 03:38:58 +00:00
bot . run ( u . config [ ' token ' ] )