The KISS Twitch bot
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.
 
 

111 lines
2.5 KiB

  1. #!/usr/bin/env python3
  2. import irc3
  3. import json
  4. from os import environ
  5. TWITCH_IRC_SERVER = "irc.chat.twitch.tv"
  6. TWITCH_IRC_PORT = 6697
  7. class Command:
  8. name: str
  9. message: str
  10. def __init__(self, name: str, message: str):
  11. self.name = name
  12. self.message = message
  13. @classmethod
  14. def from_dict(cls, params: dict):
  15. return Command(params['name'], params['message'])
  16. class Config:
  17. channel: str
  18. nickname: str
  19. token: str
  20. commands: [Command]
  21. def __init__(self, channel: str, nickname: str, token: str, commands: [Command] = None):
  22. self.nickname = nickname
  23. self.channel = channel
  24. self.token = token
  25. self.commands = commands if commands is not None else []
  26. @classmethod
  27. def from_dict(cls, params: dict, token: str):
  28. commands = []
  29. for command in params['commands']:
  30. commands.append(Command.from_dict(command))
  31. return Config(
  32. params['channel'],
  33. params['nickname'],
  34. token,
  35. commands
  36. )
  37. @irc3.plugin
  38. class TwitchBot:
  39. def __init__(self, bot):
  40. self.config = get_config()
  41. self.bot = bot
  42. self.log = self.bot.log
  43. def connection_made(self):
  44. print('connected')
  45. def server_ready(self):
  46. print('ready')
  47. def connection_lost(self):
  48. print('connection lost')
  49. @irc3.event(irc3.rfc.PRIVMSG)
  50. def on_msg(self, target, mask, data, event):
  51. author_name = mask.split('!')[0]
  52. for command in self.config.commands:
  53. print(command.name)
  54. print('%s ' % data)
  55. if ('%s ' % data).startswith('!%s ' % command.name):
  56. self.bot.privmsg(target, command.message)
  57. @irc3.event(irc3.rfc.JOIN)
  58. def on_join(self, mask, channel, **kw):
  59. print('JOINED')
  60. print(mask)
  61. print(channel)
  62. print(kw)
  63. def get_config():
  64. with open('config.json', 'r') as config_file:
  65. token = environ['TWITCH_TOKEN']
  66. return Config.from_dict(json.loads(config_file.read()), token)
  67. def main() -> int:
  68. config = get_config()
  69. bot = irc3.IrcBot.from_config({
  70. 'nick': config.nickname,
  71. 'password': config.token,
  72. 'autojoins': ['%s' % config.channel],
  73. 'host': TWITCH_IRC_SERVER,
  74. 'port': TWITCH_IRC_PORT,
  75. 'ssl': True,
  76. 'includes': [__name__]
  77. })
  78. bot.run(forever=True)
  79. return 0
  80. if __name__ == '__main__':
  81. exit(main())