Maki is a Discord bot that does things. Written in Python 3 and relies on Discord.py API implementation.
選択できるのは25トピックまでです。 トピックは、先頭が英数字で、英数字とダッシュ('-')を使用した35文字以内のものにしてください。

107 行
2.9KB

  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,
  51. msg.channel.name, msg.author.name,
  52. msg.content))
  53. except AttributeError:
  54. print("{} PRIVATE | {}: {}".format(timestr, msg.author.name,
  55. msg.content))
  56. # do not parse own messages or private messages
  57. if msg.author != client.user and type(
  58. msg.channel) is not discord.PrivateChannel:
  59. # log each message against users
  60. if msg.content != "":
  61. history[msg.server.id + msg.author.id] = (msg.server.id,
  62. time.time(), msg.content)
  63. with open('hist.json', 'w') as fp:
  64. json.dump(history, fp)
  65. # log user messages for markov chains, ignoring messages with certain substrings
  66. filters = ['`', 'http://', 'https://']
  67. if not any(x in msg.content for x in filters):
  68. try:
  69. with open('./markovs/' + msg.server.id + '-' + msg.author.id,
  70. 'a') as fp:
  71. fp.write('\n' + msg.content)
  72. except PermissionError:
  73. pass
  74. # react to stuff
  75. yield from makireacts(client, msg)
  76. # check for commands
  77. if msg.content.startswith(prefix):
  78. cmd = msg.content.split(' ', 1)[0][1:]
  79. if cmd in commands:
  80. yield from commands[cmd](client, msg)
  81. elif cmd in admincommands and msg.author.id in admins:
  82. yield from admincommands[cmd](client, msg)
  83. # MAIN FUNCTION
  84. def main():
  85. logger()
  86. client.run(token)
  87. exit(0)
  88. if __name__ == "__main__":
  89. main()