1
0
mirror of https://github.com/MrDetonia/Maki.git synced 2024-11-10 15:48:06 -05:00
Maki/bot.py

103 lines
2.4 KiB
Python
Raw Normal View History

2016-03-30 16:28:59 -04:00
# Maki
# ----
# Discord bot by MrDetonia
#
2017-04-22 11:03:11 -04:00
# Copyright 2017 Zac Herd
2016-03-30 16:28:59 -04:00
# Licensed under BSD 3-clause License, see LICENSE.md for more info
2016-03-30 16:28:59 -04:00
# IMPORTS
import discord
import asyncio
import os
import io
import requests
import sys
2016-05-25 08:10:24 -04:00
import shlex
import subprocess
2016-03-30 16:28:59 -04:00
import time
import datetime
import random
import re
import json
2016-04-19 10:14:11 -04:00
import logging
# LOCAL IMPORTS
from common import *
from helpers import *
from commands import *
from admincommands import *
2016-03-30 16:28:59 -04:00
# file in this directory called "secret.py" should contain these variables
2017-04-08 09:32:48 -04:00
from secret import token, lfmkey, steamkey
2016-03-30 16:28:59 -04:00
# DISCORD CLIENT INSTANCE
2016-03-30 16:28:59 -04:00
client = discord.Client()
2016-03-30 16:28:59 -04:00
# EVENT HANDLERS
# called when client ready
@client.event
@asyncio.coroutine
def on_ready():
# info on terminal
print('Connected')
print('User: ' + client.user.name)
print('ID: ' + client.user.id)
2016-03-30 16:28:59 -04:00
# set "Now Playing" to print version
game = discord.Game(name = version)
yield from client.change_presence(game=game)
2016-03-30 16:28:59 -04:00
2016-03-30 16:28:59 -04:00
# called when message received
@client.event
@asyncio.coroutine
def on_message(msg):
# print messages to terminal for info
timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
try:
print("{} {} - {} | {}: {}".format(timestr, msg.server.name, msg.channel.name, msg.author.name, msg.content))
except AttributeError:
2017-11-19 07:55:39 -05:00
print("{} PRIVATE | {}: {}".format(timestr, msg.author.name, msg.content))
# do not parse own messages or private messages
if msg.author != client.user and type(msg.channel) is not discord.PrivateChannel:
# log each message against users
if msg.content != "":
history[msg.server.id + msg.author.id] = (msg.server.id, time.time(), msg.content)
with open('hist.json', 'w') as fp:
json.dump(history, fp)
# log user messages for markov chains, ignoring messages with certain substrings
filters = ['`', 'http://', 'https://']
if not any(x in msg.content for x in filters):
try:
with open('./markovs/' + msg.server.id + '-' + msg.author.id, 'a') as fp:
fp.write('\n' + msg.content)
except PermissionError: pass
# react to stuff
yield from makireacts(client, msg)
# check for commands
if msg.content.startswith(prefix):
cmd = msg.content.split(' ', 1)[0][1:]
if cmd in commands:
yield from commands[cmd](client, msg)
elif cmd in admincommands and msg.author.id in admins:
yield from admincommands[cmd](client, msg)
# MAIN FUNCTION
def main():
logger()
client.run(token)
exit(0)
if __name__ == "__main__":
main()