2017-09-24 15:05:28 +00:00
import asyncio
2017-11-20 04:10:48 +00:00
from datetime import datetime as dt
2017-10-02 19:24:34 +00:00
import mimetypes
import os
import tempfile
2017-10-13 02:27:21 +00:00
import traceback as tb
2017-10-02 19:24:34 +00:00
import webbrowser
2017-10-13 02:27:21 +00:00
2017-10-30 04:21:00 +00:00
import discord as d
2017-10-13 02:27:21 +00:00
import httplib2
import requests_oauthlib as ro
from apiclient import http
from apiclient . discovery import build
2017-11-20 11:18:42 +00:00
from discord . ext import commands as cmds
2017-10-13 02:27:21 +00:00
from oauth2client . client import flow_from_clientsecrets
2017-10-11 07:01:13 +00:00
#from run import config
2017-09-24 15:05:28 +00:00
from cogs import booru
from misc import exceptions as exc
2017-10-13 02:27:21 +00:00
from misc import checks
from utils import utils as u
2017-09-24 15:05:28 +00:00
from utils import formatter
2017-10-11 07:01:13 +00:00
youtube = None
2017-10-02 19:24:34 +00:00
2017-10-11 07:01:13 +00:00
tempfile . tempdir = os . getcwd ( )
2017-10-02 19:24:34 +00:00
2017-10-13 02:27:21 +00:00
2017-09-24 15:05:28 +00:00
class Utils :
2017-10-20 20:26:13 +00:00
def __init__ ( self , bot ) :
self . bot = bot
2017-10-17 23:04:45 +00:00
2017-11-20 11:18:42 +00:00
@cmds.command ( name = ' lastcommand ' , aliases = [ ' last ' , ' l ' , ' , ' ] , brief = ' Reinvokes last successful command ' , description = ' Executes last successfully executed command ' )
2017-11-20 04:05:38 +00:00
async def last_command ( self , ctx , arg = ' None ' ) :
try :
context = u . last_commands [ ctx . author . id ]
if arg == ' show ' or arg == ' sh ' or arg == ' s ' :
await ctx . send ( f ' ` { context . prefix } { context . invoked_with } { " " . join ( context . args [ 2 : ] ) } ` ' , delete_after = 7 )
else :
await ctx . invoke ( context . command , * context . args [ 2 : ] , * * context . kwargs )
2017-10-17 23:04:45 +00:00
2017-11-20 04:05:38 +00:00
except KeyError :
await ctx . send ( ' **No last command** ' , delete_after = 7 )
await ctx . message . add_reaction ( ' \N{CROSS MARK} ' )
2017-10-17 23:04:45 +00:00
2017-10-20 20:26:13 +00:00
# Displays latency
2017-11-20 11:18:42 +00:00
@cmds.command ( aliases = [ ' p ' ] , brief = ' Pong! ' , description = ' Returns latency from bot to Discord servers, not to user ' )
2017-10-20 20:26:13 +00:00
async def ping ( self , ctx ) :
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{TABLE TENNIS PADDLE AND BALL} ' )
await ctx . send ( ctx . author . mention + ' \N{TABLE TENNIS PADDLE AND BALL} ` ' + str ( round ( self . bot . latency * 1000 ) ) + ' ms` ' , delete_after = 5 )
2017-10-17 23:04:45 +00:00
2017-11-20 11:18:42 +00:00
@cmds.command ( aliases = [ ' pre ' ] , brief = ' List bot prefixes ' , description = ' Shows all used prefixes ' )
2017-10-20 20:26:13 +00:00
async def prefix ( self , ctx ) :
2017-10-20 20:25:40 +00:00
await ctx . send ( ' **Prefix:** ` {} ` ' . format ( ' ` or ` ' . join ( u . settings [ ' prefixes ' ] [ ctx . guild . id ] if ctx . guild . id in u . settings [ ' prefixes ' ] else u . config [ ' prefix ' ] ) ) )
2017-10-20 20:26:13 +00:00
2017-11-20 11:18:42 +00:00
@cmds.group ( name = ' ,send ' , aliases = [ ' ,s ' ] , hidden = True )
@cmds.is_owner ( )
2017-10-20 20:26:13 +00:00
async def send ( self , ctx ) :
pass
@send.command ( name = ' guild ' , aliases = [ ' g ' , ' server ' , ' s ' ] )
async def send_guild ( self , ctx , guild , channel , * , message ) :
2017-10-30 04:21:45 +00:00
try :
tempchannel = d . utils . find ( lambda m : m . name == channel , d . utils . find (
lambda m : m . name == guild , self . bot . guilds ) . channels )
try :
await tempchannel . send ( message )
except AttributeError :
2017-11-19 17:03:49 +00:00
await ctx . send ( ' **Invalid channel** ' , delete_after = 7 )
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{CROSS MARK} ' )
2017-10-30 04:21:45 +00:00
except AttributeError :
2017-11-19 17:03:49 +00:00
await ctx . send ( ' **Invalid guild** ' , delete_after = 7 )
2017-11-06 07:00:58 +00:00
await ctx . message . add_reaction ( ' \N{CROSS MARK} ' )
2017-10-20 20:26:13 +00:00
@send.command ( name = ' user ' , aliases = [ ' u ' , ' member ' , ' m ' ] )
async def send_user ( self , ctx , user , * , message ) :
2017-10-30 04:21:45 +00:00
await d . utils . get ( self . bot . get_all_members ( ) , id = int ( user ) ) . send ( message )
2017-10-20 20:26:13 +00:00
2017-11-20 11:18:42 +00:00
@cmds.command ( aliases = [ ' authenticateupload ' , ' authupload ' , ' authup ' , ' auth ' ] , hidden = True )
2017-10-20 20:26:13 +00:00
async def authenticate_upload ( self , ctx ) :
global youtube
flow = flow_from_clientsecrets ( ' client_secrets.json ' , scope = ' https://www.googleapis.com/auth/youtube.upload ' ,
login_hint = ' botmyned@gmail.com ' , redirect_uri = ' urn:ietf:wg:oauth:2.0:oob ' )
flow . params [ ' access_type ' ] = ' offline '
webbrowser . open_new_tab ( flow . step1_get_authorize_url ( ) )
credentials = flow . step2_exchange ( input ( ' Authorization code: ' ) )
youtube = build ( ' youtube ' , ' v3 ' , http = credentials . authorize ( http . build_http ( ) ) )
print ( ' Service built. ' )
2017-11-20 11:18:42 +00:00
@cmds.command ( aliases = [ ' up ' , ' u ' , ' vid ' , ' v ' ] , hidden = True )
@cmds.has_permissions ( administrator = True )
2017-10-20 20:26:13 +00:00
async def upload ( self , ctx ) :
global youtube
attachments = ctx . message . attachments
try :
if not attachments :
raise exc . MissingAttachment
if len ( attachments ) > 1 :
raise exc . TooManyAttachments ( len ( attachments ) )
mime = mimetypes . guess_type ( attachments [ 0 ] . filename ) [ 0 ]
if ' video/ ' in mime :
with tempfile . NamedTemporaryFile ( ) as temp :
await attachments [ 0 ] . save ( temp )
else :
raise exc . InvalidVideoFile ( mime )
print ( ' https://www.youtube.com/watch?v= ' + youtube . videos ( ) . insert ( part = ' snippet ' ,
body = { ' categoryId ' : ' 24 ' , ' title ' : ' Test ' } , media_body = http . MediaFileUpload ( temp . name , chunksize = - 1 ) ) )
except exc . InvalidVideoFile as e :
2017-11-19 17:03:49 +00:00
await ctx . send ( ' ` ' + str ( e ) + ' ` **invalid video type** ' , delete_after = 7 )
2017-10-20 20:26:13 +00:00
except exc . TooManyAttachments as e :
2017-11-19 17:03:49 +00:00
await ctx . send ( ' ` ' + str ( e ) + ' ` **too many attachments.** Only one attachment is permitted to upload. ' , delete_after = 7 )
2017-10-20 20:26:13 +00:00
except exc . MissingAttachment :
2017-11-19 17:03:49 +00:00
await ctx . send ( ' **Missing attachment** ' , delete_after = 7 )
2017-10-20 20:26:13 +00:00
@upload.error
async def upload_error ( self , ctx , error ) :
pass
2017-10-11 07:01:13 +00:00
# http.