The KISS Twitch bot
Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.

pirms 3 gadiem
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110
  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())