Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.

103 řádky
2.4KB

  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. # set "Now Playing" to print version
  41. game = discord.Game(name = version)
  42. yield from client.change_presence(game=game)
  43. # called when message received
  44. @client.event
  45. @asyncio.coroutine
  46. def on_message(msg):
  47. # print messages to terminal for info
  48. timestr = time.strftime('%Y-%m-%d-%H:%M:%S: ')
  49. try:
  50. print("{} {} - {} | {}: {}".format(timestr, msg.server.name, msg.channel.name, msg.author.name, msg.content))
  51. except AttributeError:
  52. print("{} PRIVATE | {}: {}".format(timestr, msg.author.name, msg.content))
  53. # do not parse own messages or private messages
  54. if msg.author != client.user and type(msg.channel) is not discord.PrivateChannel:
  55. # log each message against users
  56. if msg.content != "":
  57. history[msg.server.id + msg.author.id] = (msg.server.id, time.time(), msg.content)
  58. with open('hist.json', 'w') as fp:
  59. json.dump(history, fp)
  60. # log user messages for markov chains, ignoring messages with certain substrings
  61. filters = ['`', 'http://', 'https://']
  62. if not any(x in msg.content for x in filters):
  63. try:
  64. with open('./markovs/' + msg.server.id + '-' + msg.author.id, 'a') as fp:
  65. fp.write('\n' + msg.content)
  66. except PermissionError: pass
  67. # react to stuff
  68. yield from makireacts(client, msg)
  69. # check for commands
  70. if msg.content.startswith(prefix):
  71. cmd = msg.content.split(' ', 1)[0][1:]
  72. if cmd in commands:
  73. yield from commands[cmd](client, msg)
  74. elif cmd in admincommands and msg.author.id in admins:
  75. yield from admincommands[cmd](client, msg)
  76. # MAIN FUNCTION
  77. def main():
  78. logger()
  79. client.run(token)
  80. exit(0)
  81. if __name__ == "__main__":
  82. main()