1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-02 13:12:40 +00:00
modufur/src/main/cogs/tools.py

118 lines
4.6 KiB
Python
Raw Normal View History

2017-09-24 15:05:28 +00:00
import asyncio
2017-10-11 07:01:13 +00:00
import datetime as dt
import mimetypes
import os
import tempfile
2017-10-13 02:27:21 +00:00
import traceback as tb
import webbrowser
2017-10-13 02:27:21 +00:00
import discord
import httplib2
import requests_oauthlib as ro
from apiclient import http
from apiclient.discovery import build
2017-09-24 15:05:28 +00:00
from discord.ext import commands
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-11 07:01:13 +00:00
tempfile.tempdir = os.getcwd()
command_dict = {}
2017-10-13 02:27:21 +00:00
2017-09-24 15:05:28 +00:00
class Utils:
def __init__(self, bot):
self.bot = bot
@commands.command(name='last', aliases=['l', ','], brief='Reinvokes last command', description='Reinvokes previous command executed', hidden=True)
async def last_command(self, ctx):
2017-10-14 03:41:32 +00:00
global command_dict
if command_dict.get(str(ctx.author.id), {}).get('args', None) is not None:
args = command_dict.get(str(ctx.author.id), {})['args']
2017-10-13 02:27:21 +00:00
print(command_dict)
await ctx.invoke(command_dict.get(str(ctx.author.id), {}).get('command', None), args)
2017-09-24 15:05:28 +00:00
# [prefix]ping -> Pong!
@commands.command(aliases=['p'], brief='Pong!', description='Returns latency from bot to Discord servers, not to user')
@checks.del_ctx()
async def ping(self, ctx):
2017-10-14 03:41:32 +00:00
global command_dict
2017-10-13 02:27:21 +00:00
await ctx.message.add_reaction('🏓')
await ctx.send(ctx.author.mention + ' 🏓 `' + str(round(self.bot.latency * 1000)) + 'ms`', delete_after=5)
command_dict.setdefault(str(ctx.author.id), {}).update({'command': ctx.command})
2017-09-24 15:05:28 +00:00
@commands.command(aliases=['pre'], brief='List bot prefixes', description='Shows all used prefixes')
@checks.del_ctx()
async def prefix(self, ctx):
2017-10-13 02:27:21 +00:00
await ctx.send('**Prefix:** `{}`'.format(u.config['prefix']))
await ctx.message.add_reaction('')
2017-09-24 15:05:28 +00:00
@commands.group(name=',send', aliases=[',s'], hidden=True)
@commands.is_owner()
2017-09-25 19:31:51 +00:00
@checks.del_ctx()
2017-09-24 15:05:28 +00:00
async def send(self, ctx):
pass
2017-09-24 15:05:28 +00:00
@send.command(name='guild', aliases=['g', 'server', 's'])
async def send_guild(self, ctx, guild, channel, *, message):
await discord.utils.get(self.bot.get_all_channels(), guild__name=guild, name=channel).send(message)
await ctx.message.add_reaction('')
2017-10-13 02:27:21 +00:00
2017-09-24 15:05:28 +00:00
@send.command(name='user', aliases=['u', 'member', 'm'])
async def send_user(self, ctx, user, *, message):
await discord.utils.get(self.bot.get_all_members(), id=int(user)).send(message)
await ctx.message.add_reaction('')
2017-10-14 03:41:32 +00:00
@commands.command(aliases=['authenticateupload', 'authupload', 'authup', 'auth'])
2017-10-11 07:01:13 +00:00
async def authenticate_upload(self, ctx):
global youtube
2017-10-13 02:27:21 +00:00
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')
2017-10-11 07:01:13 +00:00
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-10-13 02:27:21 +00:00
@commands.command(aliases=['up', 'u', 'vid', 'v'])
2017-10-16 18:06:46 +00:00
@commands.has_permissions(administrator=True)
async def upload(self, ctx):
global youtube
2017-10-11 07:01:13 +00:00
attachments = ctx.message.attachments
try:
2017-10-11 07:01:13 +00:00
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)
2017-10-14 03:41:32 +00:00
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)))
2017-10-11 07:01:13 +00:00
except exc.InvalidVideoFile as e:
await ctx.send('`' + str(e) + '` **not valid video type.**', delete_after=10)
2017-10-11 07:01:13 +00:00
except exc.TooManyAttachments as e:
await ctx.send('`' + str(e) + '` **too many attachments.** Only one attachment is permitted to upload.', delete_after=10)
2017-10-11 07:01:13 +00:00
except exc.MissingAttachment:
await ctx.send('**Missing attachment.**', delete_after=10)
2017-10-13 02:27:21 +00:00
2017-10-11 07:01:13 +00:00
@upload.error
async def upload_error(self, ctx, error):
pass
# http.