1
0
Fork 0
mirror of https://github.com/myned/modufur.git synced 2024-11-01 21:02:38 +00:00
modufur/src/main/run.py

140 lines
5 KiB
Python
Raw Normal View History

2017-09-24 18:09:02 +00:00
import asyncio
import datetime as dt
import json
2017-10-17 21:59:58 +00:00
# import logging as log
2017-10-02 21:29:39 +00:00
import subprocess
import sys
import traceback as tb
from contextlib import suppress
2017-10-21 20:40:06 +00:00
from pprint import pprint
import discord as d
from discord import errors as err
2017-09-24 18:09:02 +00:00
from discord import utils
from discord.ext import commands
from discord.ext.commands import errors as errext
2017-09-24 18:09:02 +00:00
from misc import exceptions as exc
from misc import checks
from utils import utils as u
2017-09-24 18:09:02 +00:00
2017-10-17 21:59:58 +00:00
# log.basicConfig(level=log.INFO)
2017-11-07 04:51:07 +00:00
class HelpFormatter(commands.HelpFormatter):
async def format():
pass
2017-10-20 20:16:05 +00:00
def get_prefix(bot, message):
if isinstance(message.guild, d.Guild) and message.guild.id in u.settings['prefixes']:
return u.settings['prefixes'][message.guild.id]
return u.config['prefix']
2017-11-07 04:51:07 +00:00
help_formatter = HelpFormatter(show_check_failure=True)
bot = commands.Bot(command_prefix=get_prefix, formatter=help_formatter, description='Experimental miscellaneous bot')
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-20 20:16:05 +00:00
# bot.loop.create_task(u.clear(booru.temp_urls, 30*60))
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> > > > > > > > >\nC O N N E C T E D : {}\n> > > > > > > > >\n'.format(bot.user.name))
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:
channel = bot.get_channel(u.temp['startup_chan'])
message = await channel.get_message(u.temp['startup_msg'])
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
@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)
@bot.event
async def on_error(error, *args, **kwargs):
print('\n! ! ! ! !\nE R R O R : {}\n! ! ! ! !\n'.format(error), file=sys.stderr)
tb.print_exc()
2017-11-09 03:32:28 +00:00
await bot.get_user(u.config['owner_id']).send('**ERROR** \N{WARNING SIGN}\n```\n{}```'.format(''.join(tb.format_exception(type(error), error, error.__traceback__))))
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:
channel = bot.get_channel(u.temp['startup_chan'])
message = await channel.get_message(u.temp['startup_msg'])
await message.add_reaction('\N{WARNING SIGN}')
2017-10-20 20:19:58 +00:00
u.temp.clear()
# u.notify('E R R O R')
await bot.logout()
u.close(bot.loop)
@bot.event
async def on_command_error(ctx, error):
if isinstance(error, err.NotFound):
pass
elif isinstance(error, errext.CheckFailure):
await ctx.send('**Insufficient permissions**', delete_after=10)
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)
await ctx.message.add_reaction('\N{BLACK QUESTION MARK ORNAMENT}')
2017-10-20 20:19:58 +00:00
else:
print('\n! ! ! ! ! ! ! ! ! ! ! !\nC 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)
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-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-09-24 15:05:28 +00:00
@bot.command(name=',test', hidden=True)
2017-09-25 19:31:51 +00:00
@commands.is_owner()
2017-09-24 15:05:28 +00:00
@checks.del_ctx()
2017-11-06 07:02:08 +00:00
async def test(ctx, *, test):
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'])