drpg-warden/bot.py

133 lines
5.4 KiB
Python

import logging
import discord
from discord.ext import commands
import datetime
import psycopg2
import os
import requests
logging.basicConfig(level=logging.INFO, style='{', datefmt="%d.%m.%Y %H:%M:%S",
format="\n{asctime} [{levelname:<8}] {name}:\n{message}")
def get_pre(bot, msg):
if msg.server.id in bot.prefixes.keys():
return bot.prefixes[msg.server.id]
else:
return '#!'
bot = commands.Bot(description="drpg warden", command_prefix=get_pre)
if 'DATABASE_URL' in os.environ:
bot.db_conn = psycopg2.connect(os.getenv('DATABASE_URL'))
else:
import heroku3
heroku_conn = heroku3.from_key(os.getenv('HEROKUAPI'))
db_url = heroku_conn.app('drpg-warden').config()['DATABASE_URL']
bot.db_conn = psycopg2.connect(db_url)
bot.prefixes = {}
with bot.db_conn:
with bot.db_conn.cursor() as cur:
cur.execute("SELECT * FROM prefixes;")
pref = cur.fetchall()
for i in pref:
bot.prefixes[i[0]] = i[1]
bot.remove_command('help')
@bot.command(pass_context=True)
async def help(ctx):
await bot.say("__**DRPG Warden: Help**__\n"
"I am a simple bot with only one real function and that is "
"to remind you about the various cooldowns of the bot "
"'DiscordRPG' and I am mostly fuctionless without it.\n"
"For the common DiscordRPG commands I will begin counting "
"and once the cooldown for that command has expired I will "
"mention you to remind you that you can do the command "
"again.\n"
"Also I will scan all the adventure messages from "
"DiscordRPG and if your HP is below 20% I'll remind you to "
"heal. Stats messages will also be scanned currently you "
"will only get info about your weapon.\n\n"
"**Currently implemented cooldowns:**\n"
" __14 seconds:__ {0}adv\n"
" __300 seconds:__ {0}mine, {0}chop, {0}fish, {0}forage\n"
" __600 seconds:__ {0}search\n"
" __various:__ {0}travel (you must of done {0}location "
"in the same channel within the last 30s, you will get "
"pinged when you arive)\n\n"
"My commands:\n"
" **{0}help** shows this message\n"
" **{0}links** shows a message with links to add me to "
"your server or join my server.\n"
" **{0}setprefix** allows you to change my prefix to "
"what ever you want, if you forget what you set as the "
"prefix and message that mentions me and contains the word "
"prefix I will reply with your current prefix\n\n"
"If you have any questions or sugestion contact my master "
"**raatty#3522**"
.format(ctx.prefix))
@bot.command()
async def links():
await bot.say('```Link to join my server:```\n'
'<https://discord.gg/SeGrSUP>\n'
'```Link to add me to your own server:```\n'
'<https://discordapp.com/oauth2/authorize?client_id='
'410356358836256768&permissions=0&scope=bot>')
@bot.command(pass_context=True)
async def announce(ctx, *, something):
if ctx.message.author.id == '140652945032216576':
for s in bot.servers:
for c in s.channels:
if c.permissions_for(s.me).send_messages \
and str(c.type) == 'text':
await bot.send_message(c, something)
break
@bot.command(pass_context=True)
async def setprefix(ctx, prefix):
if len(prefix) == 1 or len(prefix) == 2 and isinstance(prefix, str):
if ctx.message.server.id in bot.prefixes:
with bot.db_conn:
with bot.db_conn.cursor() as cur:
cur.execute("UPDATE prefixes SET prefix='{}' WHERE "
"serverid='{}';".format(prefix,
ctx.message.server.id))
bot.prefixes[ctx.message.server.id] = prefix
await bot.say('Prefix updated')
else:
with bot.db_conn:
with bot.db_conn.cursor() as cur:
cur.execute("INSERT INTO prefixes (serverid, prefix) "
"VALUES (%s, %s)", (ctx.message.server.id,
prefix))
bot.prefixes[ctx.message.server.id] = prefix
await bot.say('Prefix updated')
else:
await bot.say('Prefix must be between 1 and 2 charactors long')
@bot.event
async def on_ready():
bot.load_extension("drpg")
await bot.change_presence(game=discord.Game(name='#!adv', type=2))
await bot.send_message(discord.Object(id="408902143119196161"),
"**Im ready!** " + str(datetime.datetime.now()))
requests.post('https://bots.discord.pw/api/bots/410356358836256768/stats',
headers={"Authorization": os.getenv('BOTS_PW_AUTH')},
json={"server_count": len(bot.servers)})
await bot.send_message(discord.Object(id="408902143119196161"),
f'**Connected to {len(bot.servers)} servers**')
bot.run(os.getenv('TOKEN'))