2017-09-24 11:05:28 -04:00
import json
try :
with open ( ' config.json ' ) as infile :
config = json . load ( infile )
print ( ' \" config.json \" loaded. ' )
except FileNotFoundError :
with open ( ' config.json ' , ' w ' ) as outfile :
2017-10-11 11:08:32 -04:00
json . dump ( { ' client_id ' : 0 , ' listed_ids ' : [ 0 ] , ' owner_id ' : 0 , ' permissions ' : 126016 , ' prefix ' : ' , ' , ' shutdown_channel ' : 0 , ' startup_channel ' : 0 , ' token ' : ' str ' } , outfile , indent = 4 , sort_keys = True )
2017-09-24 11:05:28 -04:00
raise FileNotFoundError ( ' Config file not found: \" config.json \" created with abstract values. Restart \" run.py \" with correct values. ' )
2017-09-24 14:09:02 -04:00
import asyncio
2017-10-11 02:57:17 -04:00
import datetime as dt
import discord as d
2017-10-02 17:29:39 -04:00
import os
import subprocess
2017-10-11 02:57:17 -04:00
import sys
2017-09-24 14:09:02 -04:00
import traceback
from discord import utils
from discord . ext import commands
2017-10-11 02:57:17 -04:00
from cogs import booru , info , owner , management , tools
2017-09-24 14:09:02 -04:00
from misc import checks
from misc import exceptions as exc
2017-10-11 02:57:17 -04:00
from utils import utils as u
2017-09-24 14:09:02 -04:00
2017-10-11 02:57:17 -04:00
import logging
logging . basicConfig ( level = logging . INFO )
print ( ' PID {} ' . format ( os . getpid ( ) ) )
bot = commands . Bot ( command_prefix = config [ ' prefix ' ] , description = ' Experimental booru bot ' )
2017-09-24 11:05:28 -04:00
# Send and print ready message to #testing and console after logon
@bot.event
async def on_ready ( ) :
2017-10-11 02:57:17 -04:00
global bot
bot . add_cog ( tools . Utils ( bot ) )
bot . add_cog ( owner . Tools ( bot ) )
bot . add_cog ( management . Administration ( bot ) )
bot . add_cog ( info . Info ( bot ) )
bot . add_cog ( booru . MsG ( bot ) )
# bot.loop.create_task(u.clear(booru.temp_urls, 30*60))
if isinstance ( bot . get_channel ( config [ ' startup_channel ' ] ) , d . TextChannel ) :
await bot . get_channel ( config [ ' startup_channel ' ] ) . send ( ' **Started.** ☀️ ' )
print ( ' CONNECTED ' )
print ( bot . user . name )
2017-09-24 11:05:28 -04:00
print ( ' ------- ' )
# Close connection to Discord - immediate offline
2017-10-11 02:57:17 -04:00
@bot.command ( name = ' ,die ' , aliases = [ ' ,d ' ] , brief = ' Kills the bot ' , description = ' BOT OWNER ONLY \n Closes the connection to Discord ' , hidden = True )
2017-09-24 11:05:28 -04:00
@commands.is_owner ( )
2017-09-25 15:31:51 -04:00
@checks.del_ctx ( )
2017-09-24 11:05:28 -04:00
async def die ( ctx ) :
try :
2017-10-11 02:57:17 -04:00
if isinstance ( bot . get_channel ( config [ ' startup_channel ' ] ) , d . TextChannel ) :
await bot . get_channel ( config [ ' shutdown_channel ' ] ) . send ( ' **Shutting down...** 🌙 ' )
2017-09-24 11:05:28 -04:00
await bot . close ( )
print ( ' ------- ' )
2017-10-11 02:57:17 -04:00
print ( ' CLOSED ' )
2017-09-24 11:05:28 -04:00
except Exception :
2017-10-11 02:57:17 -04:00
await ctx . send ( exc . base + ' \n ``` ' + traceback . format_exc ( limit = 1 ) + ' ``` ' )
2017-09-24 11:05:28 -04:00
traceback . print_exc ( limit = 1 )
2017-10-02 17:29:39 -04:00
@bot.command ( name = ' ,restart ' , aliases = [ ' ,res ' , ' ,r ' ] , hidden = True )
2017-10-02 15:24:57 -04:00
@commands.is_owner ( )
2017-10-02 17:32:53 -04:00
@checks.del_ctx ( )
2017-10-02 17:29:39 -04:00
async def restart ( ctx ) :
try :
2017-10-11 02:53:40 -04:00
print ( ' RESTARTING ' )
2017-10-02 17:29:39 -04:00
print ( ' ------- ' )
2017-10-11 02:53:40 -04:00
if isinstance ( bot . get_channel ( config [ ' startup_channel ' ] ) , d . TextChannel ) :
await bot . get_channel ( config [ ' shutdown_channel ' ] ) . send ( ' **Restarting...** 💤 ' )
os . execl ( sys . executable , ' python3 ' , ' run.py ' )
2017-10-02 17:29:39 -04:00
except Exception :
2017-10-11 02:53:40 -04:00
await ctx . send ( ' {} \n ``` {} ``` ' . format ( exc . base , traceback . format_exc ( limit = 1 ) ) )
2017-10-02 17:29:39 -04:00
traceback . print_exc ( limit = 1 )
2017-10-02 15:24:57 -04:00
2017-09-24 11:05:28 -04:00
# Invite bot to bot owner's server
@bot.command ( name = ' ,invite ' , aliases = [ ' ,inv ' , ' ,link ' ] , brief = ' Invite the bot ' , description = ' BOT OWNER ONLY \n Invite the bot to a server (Requires admin) ' , hidden = True )
@commands.is_owner ( )
2017-09-25 15:31:51 -04:00
@checks.del_ctx ( )
2017-09-24 11:05:28 -04:00
async def invite ( ctx ) :
try :
2017-10-11 02:57:17 -04:00
await ctx . send ( ' 🔗 https://discordapp.com/oauth2/authorize?&client_id= {} &scope=bot&permissions= {} ' . format ( config [ ' client_id ' ] , config [ ' permissions ' ] ) , delete_after = 10 )
2017-09-24 11:05:28 -04:00
except Exception :
2017-10-11 02:57:17 -04:00
await ctx . send ( ' {} \n ``` {} ``` ' . format ( exc . base , traceback . format_exc ( limit = 1 ) ) )
2017-09-24 11:05:28 -04:00
traceback . print_exc ( limit = 1 )
@bot.command ( brief = ' [IN TESTING] ' , description = ' [IN TESTING] ' , hidden = True )
async def hi ( ctx ) :
2017-10-11 02:57:17 -04:00
user = ctx . message . author
2017-09-24 11:05:28 -04:00
try :
2017-10-11 02:57:17 -04:00
hello = ' Hewwo, {} . ' . format ( user . mention )
if user . id == checks . owner_id :
2017-09-24 11:05:28 -04:00
hello + = ' .. ***Master.*** uwu '
2017-10-11 02:57:17 -04:00
elif user . guild_permissions . administrator :
hello = ' {} **Admin** {} ' . format ( hello [ : 7 ] , hello [ 7 : ] )
elif user . guild_permissions . ban_members :
hello = ' {} **Mod** {} ' . format ( hello [ : 7 ] , hello [ 7 : ] )
2017-09-24 11:05:28 -04:00
await ctx . send ( hello )
except Exception :
2017-10-11 02:57:17 -04:00
await ctx . send ( ' {} \n ``` {} ``` ' . format ( exc . base , traceback . format_exc ( limit = 1 ) ) )
2017-09-24 11:05:28 -04:00
traceback . print_exc ( limit = 1 )
2017-10-11 02:57:17 -04:00
@bot.command ( name = ' ,test ' , hidden = True )
2017-09-25 15:31:51 -04:00
@commands.is_owner ( )
2017-09-24 11:05:28 -04:00
@checks.del_ctx ( )
async def test ( ctx ) :
2017-10-11 02:57:17 -04:00
embed = d . Embed ( title = ' /post/xxxxxx ' , url = ' https://static1.e621.net/data/4b/3e/4b3ec0c2e8580f418e4ce019dfd5ac32.png ' , timestamp = dt . datetime . utcnow ( ) , color = ctx . me . color )
embed . set_image ( url = ' https://static1.e621.net/data/27/0f/270fd28caa5e6d8bf542a76515848e02.png ' )
embed . set_footer ( text = ' e621 ' , icon_url = ' http://ndl.mgccw.com/mu3/app/20141013/18/1413204353554/icon/icon_xl.png ' )
embed . set_author ( name = ' tags ' , url = ctx . message . author . avatar_url , icon_url = ctx . message . author . avatar_url )
embed . add_field ( name = ' Link ' , value = ' https://static1.e621.net/data/c2/55/c255792b5a307ee6efa51d6bb3edf878.jpg ' )
await ctx . send ( embed = embed )
2017-09-24 11:05:28 -04:00
bot . run ( config [ ' token ' ] )