1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-12-23 22:27:27 +00:00

WIP fix for reconnection issues

This commit is contained in:
Myned 2018-03-31 16:37:05 -04:00
parent 9f487cb57c
commit 313dda5096

View file

@ -1,266 +1,272 @@
import asyncio import asyncio
from datetime import datetime as dt from datetime import datetime as dt
import json import json
import logging as log import logging as log
import subprocess import subprocess
import sys import sys
import traceback as tb import traceback as tb
from contextlib import suppress from contextlib import suppress
from pprint import pprint from pprint import pprint
from hurry.filesize import size, alternative from hurry.filesize import size, alternative
from urllib.parse import urlparse from urllib.parse import urlparse
import discord as d import discord as d
from discord import errors as err from discord import errors as err
from discord import utils from discord import utils
from discord.ext import commands as cmds from discord.ext import commands as cmds
from discord.ext.commands import errors as errext from discord.ext.commands import errors as errext
from misc import exceptions as exc from misc import exceptions as exc
from misc import checks from misc import checks
from utils import utils as u from utils import utils as u
log.basicConfig(level=log.WARNING) log.basicConfig(level=log.WARNING)
# class HelpFormatter(cmds.HelpFormatter): # class HelpFormatter(cmds.HelpFormatter):
# #
# async def format(self): # async def format(self):
# self._paginator = cmds.Paginator() # self._paginator = cmds.Paginator()
# #
# # we need a padding of ~80 or so # # we need a padding of ~80 or so
# #
# description = self.command.description if not self.is_cog() else inspect.getdoc(self.command) # description = self.command.description if not self.is_cog() else inspect.getdoc(self.command)
# #
# if description: # if description:
# # <description> portion # # <description> portion
# self._paginator.add_line(description, empty=True) # self._paginator.add_line(description, empty=True)
# #
# if isinstance(self.command, cmds.Command): # if isinstance(self.command, cmds.Command):
# # <signature portion> # # <signature portion>
# signature = self.get_command_signature() # signature = self.get_command_signature()
# self._paginator.add_line(signature, empty=True) # self._paginator.add_line(signature, empty=True)
# #
# # <long doc> section # # <long doc> section
# if self.command.help: # if self.command.help:
# self._paginator.add_line(self.command.help, empty=True) # self._paginator.add_line(self.command.help, empty=True)
# #
# # end it here if it's just a regular command # # end it here if it's just a regular command
# if not self.has_subcommands(): # if not self.has_subcommands():
# self._paginator.close_page() # self._paginator.close_page()
# return self._paginator.pages # return self._paginator.pages
# #
# max_width = self.max_name_size # max_width = self.max_name_size
def get_prefix(bot, message): def get_prefix(bot, message):
with suppress(AttributeError): with suppress(AttributeError):
return u.settings['prefixes'].get(message.guild.id, u.config['prefix']) return u.settings['prefixes'].get(message.guild.id, u.config['prefix'])
return u.config['prefix'] return u.config['prefix']
bot = cmds.Bot(command_prefix=get_prefix, self_bot=u.config['selfbot'], formatter=cmds.HelpFormatter(show_check_failure=True), description='Modufur - A booru bot with a side of management and automated tasking\nMade by @Myned#3985\n\nNSFW for Not Safe For Wumpus commands\n(G) for group commands\n@permission@ for required permissions\n!notice! for important information\np for prefix\n\n\{\} for mandatory argument\n[] for optional argument\n... for one or more arguments', help_attrs={'aliases': ['h']}, pm_help=None) bot = cmds.Bot(command_prefix=get_prefix, self_bot=u.config['selfbot'], formatter=cmds.HelpFormatter(show_check_failure=True), description='Modufur - A booru bot with a side of management and automated tasking\nMade by @Myned#3985\n\nNSFW for Not Safe For Wumpus commands\n(G) for group commands\n@permission@ for required permissions\n!notice! for important information\np for prefix\n\n\{\} for mandatory argument\n[] for optional argument\n... for one or more arguments', help_attrs={'aliases': ['h']}, pm_help=None)
@bot.command(help='help', brief='brief', description='description', usage='usage', hidden=True) @bot.command(help='help', brief='brief', description='description', usage='usage', hidden=True)
async def test(ctx): async def test(ctx):
await ctx.send('test') await ctx.send('test')
# Send and print ready message to #testing and console after logon # Send and print ready message to #testing and console after logon
@bot.event @bot.event
async def on_ready(): async def on_ready():
if not checks.ready: if not checks.ready:
# d.opus.load_opus('opuslib') # d.opus.load_opus('opuslib')
from cogs import booru, info, management, owner, tools from cogs import booru, info, management, owner, tools
for cog in (tools.Utils(bot), owner.Bot(bot), owner.Tools(bot), management.Administration(bot), info.Info(bot), booru.MsG(bot)): 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) bot.add_cog(cog)
print(f'COG : {type(cog).__name__}') print(f'COG : {type(cog).__name__}')
# bot.loop.create_task(u.clear(booru.temp_urls, 30*60)) # bot.loop.create_task(u.clear(booru.temp_urls, 30*60))
if u.config['playing'] is not '': if u.config['playing'] is not '':
await bot.change_presence(game=d.Game(name=u.config['playing'])) await bot.change_presence(game=d.Game(name=u.config['playing']))
print('\n> > > > > > > > >\nC O N N E C T E D : {}\n> > > > > > > > >\n'.format(bot.user.name)) print('\n> > > > > > > > >\nC O N N E C T E D : {}\n> > > > > > > > >\n'.format(bot.user.name))
await bot.get_channel(u.config['info_channel']).send(f'**Started** \N{BLACK SUN WITH RAYS} `{"` or `".join(u.config["prefix"])}`') await bot.get_channel(u.config['info_channel']).send(f'**Started** \N{BLACK SUN WITH RAYS} `{"` or `".join(u.config["prefix"])}`')
# u.notify('C O N N E C T E D') # u.notify('C O N N E C T E D')
if u.temp['startup']: if u.temp['startup']:
with suppress(err.NotFound): with suppress(err.NotFound):
if u.temp['startup'][0] == 'guild': if u.temp['startup'][0] == 'guild':
dest = bot.get_channel(u.temp['startup'][1]) dest = bot.get_channel(u.temp['startup'][1])
else: else:
dest = bot.get_user(u.temp['startup'][1]) dest = bot.get_user(u.temp['startup'][1])
message = await dest.get_message(u.temp['startup'][2]) message = await dest.get_message(u.temp['startup'][2])
await message.add_reaction('\N{WHITE HEAVY CHECK MARK}') await message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
u.temp['startup'] = () u.temp['startup'] = ()
u.dump(u.temp, 'temp/temp.pkl') u.dump(u.temp, 'temp/temp.pkl')
checks.ready = True checks.ready = True
else: else:
print('\n- - - -\nI N F O : reconnected, skipping initialization\n- - - -') print('\n- - - -\nI N F O : reconnected, reinitializing\n- - - -')
for cog in (tools.Utils(bot), owner.Bot(bot), owner.Tools(bot), management.Administration(bot), info.Info(bot), booru.MsG(bot)):
@bot.event bot.add_cog(cog)
async def on_message(message): print(f'COG : {type(cog).__name__}')
if not u.config['selfbot']:
if message.author is not bot.user and not message.author.bot: if u.config['playing'] is not '':
await bot.process_commands(message) await bot.change_presence(game=d.Game(name=u.config['playing']))
else:
if not message.author.bot:
await bot.process_commands(message) @bot.event
async def on_message(message):
if not u.config['selfbot']:
@bot.event if message.author is not bot.user and not message.author.bot:
async def on_error(error, *args, **kwargs): await bot.process_commands(message)
print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr) else:
tb.print_exc() if not message.author.bot:
await bot.get_user(u.config['owner_id']).send('**ERROR** \N{WARNING SIGN}\n```\n{}```'.format(error)) await bot.process_commands(message)
await bot.get_channel(u.config['info_channel']).send('**ERROR** \N{WARNING SIGN}\n```\n{}```'.format(error))
if u.temp['startup']: @bot.event
with suppress(err.NotFound): async def on_error(error, *args, **kwargs):
if u.temp['startup'][0] == 'guild': print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr)
dest = bot.get_channel(u.temp['startup'][1]) tb.print_exc()
else: await bot.get_user(u.config['owner_id']).send('**ERROR** \N{WARNING SIGN}\n```\n{}```'.format(error))
dest = bot.get_user(u.temp['startup'][1]) await bot.get_channel(u.config['info_channel']).send('**ERROR** \N{WARNING SIGN}\n```\n{}```'.format(error))
message = await dest.get_message(u.temp['startup'][2])
if u.temp['startup']:
await message.add_reaction('\N{WARNING SIGN}') with suppress(err.NotFound):
if u.temp['startup'][0] == 'guild':
u.temp.clear() dest = bot.get_channel(u.temp['startup'][1])
u.dump(u.temp, 'temp/temp.pkl') else:
# u.notify('E R R O R') dest = bot.get_user(u.temp['startup'][1])
await bot.logout() message = await dest.get_message(u.temp['startup'][2])
u.close(bot.loop)
await message.add_reaction('\N{WARNING SIGN}')
@bot.event u.temp.clear()
async def on_command_error(ctx, error): u.dump(u.temp, 'temp/temp.pkl')
if isinstance(error, err.NotFound): # u.notify('E R R O R')
print('NOT FOUND') await bot.logout()
elif isinstance(error, errext.MissingRequiredArgument):
await ctx.send('**Missing required argument**', delete_after=7)
await ctx.message.add_reaction('\N{CROSS MARK}') @bot.event
elif isinstance(error, errext.BadArgument): async def on_command_error(ctx, error):
await ctx.send(f'**Invalid argument.** {error}', delete_after=7) if isinstance(error, err.NotFound):
await ctx.message.add_reaction('\N{CROSS MARK}') print('NOT FOUND')
elif isinstance(error, errext.CheckFailure): elif isinstance(error, errext.MissingRequiredArgument):
await ctx.send('**Insufficient permissions**', delete_after=7) await ctx.send('**Missing required argument**', delete_after=7)
await ctx.message.add_reaction('\N{NO ENTRY}') await ctx.message.add_reaction('\N{CROSS MARK}')
elif isinstance(error, errext.CommandNotFound): elif isinstance(error, errext.BadArgument):
print('INVALID COMMAND : {}'.format(error), file=sys.stderr) await ctx.send(f'**Invalid argument.** {error}', delete_after=7)
await ctx.message.add_reaction('\N{BLACK QUESTION MARK ORNAMENT}') await ctx.message.add_reaction('\N{CROSS MARK}')
else: elif isinstance(error, errext.CheckFailure):
print('\n! ! ! ! ! ! ! ! ! ! ! !\nC O M M A N D E R R O R : {}\n! ! ! ! ! ! ! ! ! ! ! !\n'.format( await ctx.send('**Insufficient permissions**', delete_after=7)
error), file=sys.stderr) await ctx.message.add_reaction('\N{NO ENTRY}')
tb.print_exception(type(error), error, error.__traceback__, file=sys.stderr) elif isinstance(error, errext.CommandNotFound):
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__ if len(str(error.__traceback__)) < 1500 else str(error.__traceback__)[:1500])))) print('INVALID COMMAND : {}'.format(error), file=sys.stderr)
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)) await ctx.message.add_reaction('\N{BLACK QUESTION MARK ORNAMENT}')
await exc.send_error(ctx, error) else:
await ctx.message.add_reaction('\N{WARNING SIGN}') print('\n! ! ! ! ! ! ! ! ! ! ! !\nC O M M A N D E R R O R : {}\n! ! ! ! ! ! ! ! ! ! ! !\n'.format(
# u.notify('C O M M A N D E R R O R') error), file=sys.stderr)
tb.print_exception(type(error), error, error.__traceback__, file=sys.stderr)
# @bot.event 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__ if len(str(error.__traceback__)) < 1500 else str(error.__traceback__)[:1500]))))
# async def on_command(ctx): 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))
# if ctx.guild.id in u.settings['del_resp']: await exc.send_error(ctx, error)
# pass await ctx.message.add_reaction('\N{WARNING SIGN}')
# u.notify('C O M M A N D E R R O R')
@bot.event
async def on_command_completion(ctx): # @bot.event
with suppress(err.NotFound): # async def on_command(ctx):
with suppress(AttributeError): # if ctx.guild.id in u.settings['del_resp']:
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): # pass
await ctx.message.delete()
@bot.event
await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}') async def on_command_completion(ctx):
with suppress(err.NotFound):
for command in ('lastcommand', ',restart', ',die'): with suppress(AttributeError):
if ctx.command.name == command: 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):
return await ctx.message.delete()
u.last_commands[ctx.author.id] = ctx await ctx.message.add_reaction('\N{WHITE HEAVY CHECK MARK}')
@bot.event for command in ('lastcommand', ',restart', ',die'):
async def on_guild_remove(guild): if ctx.command.name == command:
print(f'LEFT : {guild.name}') return
for task, idents in u.tasks.items(): u.last_commands[ctx.author.id] = ctx
for channel in guild.channels:
if channel.id in idents: @bot.event
idents.remove(channel.id) async def on_guild_remove(guild):
print(f'STOPPED : {task} in #{channel.id}') print(f'LEFT : {guild.name}')
u.dump(u.tasks, 'cogs/tasks.pkl')
for task, idents in u.tasks.items():
for channel in guild.channels:
async def wait(voice): if channel.id in idents:
asyncio.sleep(5) idents.remove(channel.id)
await voice.disconnect() print(f'STOPPED : {task} in #{channel.id}')
u.dump(u.tasks, 'cogs/tasks.pkl')
def after(voice, error):
coro = voice.disconnect() async def wait(voice):
future = asyncio.run_coroutine_threadsafe(coro, voice.loop) asyncio.sleep(5)
future.result() await voice.disconnect()
# suggested = u.setdefault('cogs/suggested.pkl', {'last_update': 'None', 'tags': {}, 'total': 0})
@bot.command(name=',test', hidden=True) def after(voice, error):
@cmds.is_owner() coro = voice.disconnect()
async def test(ctx): future = asyncio.run_coroutine_threadsafe(coro, voice.loop)
post = await u.fetch('https://e621.net/post/show.json?id=1145042', json=True) future.result()
tags = [] # suggested = u.setdefault('cogs/suggested.pkl', {'last_update': 'None', 'tags': {}, 'total': 0})
if post['tags']: @bot.command(name=',test', hidden=True)
temptags = post['tags'].split(' ') @cmds.is_owner()
cis = [] async def test(ctx):
for tag in suggested: post = await u.fetch('https://e621.net/post/show.json?id=1145042', json=True)
pass
for tag in temptags: tags = []
tags.append(f'[{tag}](https://e621.net/post?tags={tag})') if post['tags']:
# tags = ' '.join(tags) temptags = post['tags'].split(' ')
else: cis = []
tags = 'None' for tag in suggested:
pass
if post['description']: for tag in temptags:
post_description = post['description'] if len(post['description']) < 200 else f'{post["description"][:200]}...' tags.append(f'[{tag}](https://e621.net/post?tags={tag})')
else: # tags = ' '.join(tags)
post_description = 'None' else:
tags = 'None'
title = ', '.join(post['artist'])
description = f'posted by: *[{post["author"]}](https://e621.net/post?tags=user:{post["author"]})*' if post['description']:
url = f'https://e621.net/post?tags={",".join(post["artist"])}' post_description = post['description'] if len(post['description']) < 200 else f'{post["description"][:200]}...'
# timestamp = dt.utcnow() else:
color = ctx.me.color post_description = 'None'
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' title = ', '.join(post['artist'])
thumbnail = post['file_url'] description = f'posted by: *[{post["author"]}](https://e621.net/post?tags=user:{post["author"]})*'
author = {'name': post['id'], 'url': f'https://e621.net/post/show/{post["id"]}', 'icon_url': ctx.author.avatar_url} url = f'https://e621.net/post?tags={",".join(post["artist"])}'
# timestamp = dt.utcnow()
fields = [] color = ctx.me.color
names = ('File', 'Sources', 'Description', 'tags', 'tags (ext.)') 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'}
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:])) # image = 'https://e621.net/post/show/54360'
inlines = (False, False, False, True, True) thumbnail = post['file_url']
for name, value, inline in zip(names, values, inlines): author = {'name': post['id'], 'url': f'https://e621.net/post/show/{post["id"]}', 'icon_url': ctx.author.avatar_url}
fields.append({'name': name, 'value': value, 'inline': inline})
fields = []
embed = u.generate_embed(ctx, title=title, description=description, url=url, colour=color, footer=footer, thumbnail=thumbnail, author=author, fields=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:]))
await ctx.send(embed=embed) inlines = (False, False, False, True, True)
# print(ctx.args) for name, value, inline in zip(names, values, inlines):
# print(ctx.kwargs) fields.append({'name': name, 'value': value, 'inline': inline})
# if '<:N_:368917475531816962>' in message:
# await ctx.send('<:N_:368917475531816962>') embed = u.generate_embed(ctx, title=title, description=description, url=url, colour=color, footer=footer, thumbnail=thumbnail, author=author, fields=fields)
# logs = []
# async for entry in ctx.guild.audit_logs(limit=None, action=d.AuditLogAction.message_delete): await ctx.send(embed=embed)
# logs.append( # print(ctx.args)
# f'@{entry.user.name} deleted {entry.extra.count} messages from @{entry.target.name} in #{entry.extra.channel.name}') # print(ctx.kwargs)
# pprint(logs) # if '<:N_:368917475531816962>' in message:
# channel = bot.get_channel(int(cid)) # await ctx.send('<:N_:368917475531816962>')
# voice = await channel.connect() # logs = []
# voice.play(d.AudioSource, after=lambda: after(voice)) # async for entry in ctx.guild.audit_logs(limit=None, action=d.AuditLogAction.message_delete):
# logs.append(
bot.run(u.config['token'], bot=not u.config['selfbot']) # f'@{entry.user.name} deleted {entry.extra.count} messages from @{entry.target.name} in #{entry.extra.channel.name}')
# pprint(logs)
# channel = bot.get_channel(int(cid))
# voice = await channel.connect()
# voice.play(d.AudioSource, after=lambda: after(voice))
bot.run(u.config['token'], bot=not u.config['selfbot'])