Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

110 lines
3.0KB

  1. # Maki
  2. # ----
  3. # Discord bot by MrDetonia
  4. #
  5. # Copyright 2018 Zac Herd
  6. # Licensed under BSD 3-clause License, see LICENSE.md for more info
  7. # IMPORTS
  8. import discord
  9. import asyncio
  10. import os
  11. import io
  12. import requests
  13. import sys
  14. import shlex
  15. import subprocess
  16. import time
  17. import datetime
  18. import random
  19. import re
  20. import json
  21. import logging
  22. # LOCAL IMPORTS
  23. from common import *
  24. from helpers import *
  25. from commands import *
  26. from admincommands import *
  27. # file in this directory called "secret.py" should contain these variables
  28. from secret import token, lfmkey, steamkey
  29. # DISCORD CLIENT INSTANCE
  30. client = discord.Client()
  31. # EVENT HANDLERS
  32. # called when client ready
  33. @client.event
  34. @asyncio.coroutine
  35. def on_ready():
  36. # info on terminal
  37. print('Connected')
  38. print('User: ' + client.user.name)
  39. print('ID: ' + client.user.id)
  40. # populate D&D spells list
  41. for spell in requests.get('http://dnd5eapi.co/api/spells/').json()['results']:
  42. spellslist[spell['name']] = spell['url']
  43. # set "Now Playing" to print version
  44. game = discord.Game(name=version)
  45. yield from client.change_presence(game=game)
  46. # called when message received
  47. @client.event
  48. @asyncio.coroutine
  49. def on_message(msg):
  50. # print messages to terminal for info
  51. timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
  52. try:
  53. print("{} {} - {} | {}: {}".format(timestr, msg.server.name,
  54. msg.channel.name, msg.author.name,
  55. msg.content))
  56. except AttributeError:
  57. print("{} PRIVATE | {}: {}".format(timestr, msg.author.name,
  58. msg.content))
  59. # do not parse own messages or private messages
  60. if msg.author != client.user and type(
  61. msg.channel) is not discord.PrivateChannel:
  62. # log each message against users
  63. if msg.content != "":
  64. history[msg.server.id + msg.author.id] = (msg.server.id,
  65. time.time(), msg.content)
  66. with open('./persist/hist.json', 'w') as fp:
  67. json.dump(history, fp)
  68. # log user messages for markov chains, ignoring messages with certain substrings
  69. filters = ['`', 'http://', 'https://']
  70. if not any(x in msg.content for x in filters):
  71. try:
  72. with open('./persist/markovs/' + msg.server.id + '-' + msg.author.id,
  73. 'a') as fp:
  74. fp.write('\n' + msg.content)
  75. except PermissionError:
  76. pass
  77. # react to stuff
  78. yield from makireacts(client, msg)
  79. # check for commands
  80. if msg.content.startswith(prefix):
  81. cmd = msg.content.split(' ', 1)[0][1:]
  82. if cmd in commands:
  83. yield from commands[cmd](client, msg)
  84. elif cmd in admincommands and msg.author.id in admins:
  85. yield from admincommands[cmd](client, msg)
  86. # MAIN FUNCTION
  87. def main():
  88. logger()
  89. client.run(token)
  90. exit(0)
  91. if __name__ == "__main__":
  92. main()