2016-03-30 16:28:59 -04:00
|
|
|
# Maki
|
|
|
|
# ----
|
|
|
|
# Discord bot by MrDetonia
|
|
|
|
#
|
2018-01-27 07:04:12 -05:00
|
|
|
# Copyright 2018 Zac Herd
|
2016-03-30 16:28:59 -04:00
|
|
|
# Licensed under BSD 3-clause License, see LICENSE.md for more info
|
|
|
|
|
|
|
|
# IMPORTS
|
|
|
|
import discord
|
|
|
|
import asyncio
|
2016-03-30 18:33:53 -04:00
|
|
|
import os
|
2016-04-29 05:20:42 -04:00
|
|
|
import io
|
2016-12-01 16:11:11 -05:00
|
|
|
import requests
|
2016-04-29 05:20:42 -04:00
|
|
|
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
|
2016-04-03 17:19:10 -04:00
|
|
|
import random
|
2016-05-05 16:27:00 -04:00
|
|
|
import re
|
2016-03-30 18:33:53 -04:00
|
|
|
import json
|
2016-04-19 10:14:11 -04:00
|
|
|
import logging
|
|
|
|
|
2017-05-10 06:52:17 -04:00
|
|
|
# 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
|
|
|
|
2017-05-10 06:52:17 -04:00
|
|
|
# DISCORD CLIENT INSTANCE
|
2016-03-30 16:28:59 -04:00
|
|
|
client = discord.Client()
|
|
|
|
|
2016-04-01 09:32:18 -04:00
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# EVENT HANDLERS
|
|
|
|
# called when client ready
|
|
|
|
@client.event
|
|
|
|
@asyncio.coroutine
|
|
|
|
def on_ready():
|
2018-08-23 16:30:26 -04:00
|
|
|
# info on terminal
|
|
|
|
print('Connected')
|
|
|
|
print('User: ' + client.user.name)
|
|
|
|
print('ID: ' + client.user.id)
|
2016-03-30 16:28:59 -04:00
|
|
|
|
2018-08-23 16:30:26 -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
|
|
|
|
2017-05-10 06:52:17 -04:00
|
|
|
|
2016-03-30 16:28:59 -04:00
|
|
|
# called when message received
|
|
|
|
@client.event
|
|
|
|
@asyncio.coroutine
|
2017-05-10 06:52:17 -04:00
|
|
|
def on_message(msg):
|
2018-08-23 16:30:26 -04:00
|
|
|
# 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:
|
|
|
|
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)
|
2019-02-25 08:58:54 -05:00
|
|
|
with open('./persist/hist.json', 'w') as fp:
|
2018-08-23 16:30:26 -04:00
|
|
|
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:
|
2019-02-25 08:58:54 -05:00
|
|
|
with open('./persist/markovs/' + msg.server.id + '-' + msg.author.id,
|
2018-08-23 16:30:26 -04:00
|
|
|
'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)
|
2017-05-10 06:52:17 -04:00
|
|
|
|
|
|
|
|
|
|
|
# MAIN FUNCTION
|
|
|
|
def main():
|
2018-08-23 16:30:26 -04:00
|
|
|
logger()
|
|
|
|
client.run(token)
|
|
|
|
exit(0)
|
|
|
|
|
2017-05-10 06:52:17 -04:00
|
|
|
|
|
|
|
if __name__ == "__main__":
|
2018-08-23 16:30:26 -04:00
|
|
|
main()
|