A Python bot that bridges a MUSH to a Discord channel
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.

mushcord.py 3.7KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. import discord
  2. import telnetlib as tn
  3. from time import sleep
  4. import sys, os
  5. client = discord.Client()
  6. class Mushcord:
  7. VERSION = '0.1.3'
  8. def __init__(self):
  9. with open('config.txt', 'r+') as file:
  10. options = file.readlines()
  11. config = {}
  12. for option in options:
  13. o = option.split('=')
  14. config[o[0]] = config[o[1]]
  15. self.TOKEN = config['TOKEN']
  16. self.server = config['SERVER']
  17. self.port = int(config['PORT'])
  18. self.username = config['USERNAME']
  19. self.password = config['PASSWORD']
  20. @client.event
  21. async def on_message(self, message):
  22. if message.author == client.user:
  23. return
  24. if message.content.startswith('!hello'):
  25. msg = 'Hello {0.author.mention}'.format(message)
  26. await client.send_message(message.channel, msg)
  27. if message.content.startswith('!say'):
  28. author = message.author.nick or str(message.author).split('#')[0]
  29. content = " ".join(message.content.split(" ")[1:])
  30. msg = bytes(":| {0} says, \"{1}\".\n".format(author, content), "UTF-8")
  31. mc.t.write(msg)
  32. if message.content.startswith('!"'):
  33. author = message.author.nick or str(message.author).split('#')[0]
  34. content = message.content[2:]
  35. msg = bytes(":| {0} says, \"{1}\".\n".format(author, content), "UTF-8")
  36. mc.t.write(msg)
  37. if message.content.startswith('!emote'):
  38. author = message.author.nick or str(message.author).split('#')[0]
  39. content = " ".join(message.content.split(" ")[1:])
  40. msg = bytes(":| {0} {1}\n".format(author, content), "UTF-8")
  41. mc.t.write(msg)
  42. if message.content.startswith('!:'):
  43. author = message.author.nick or str(message.author).split('#')[0]
  44. content = message.content[2:]
  45. msg = bytes(":| {0} {1}\n".format(author, content), "UTF-8")
  46. mc.t.write(msg)
  47. if message.content.startswith('!>:'):
  48. author = message.author.nick or str(message.author).split('#')[0]
  49. content = message.content[3:]
  50. msg = bytes(">:| {0} {1}\n".format(author, content), "UTF-8")
  51. mc.t.write(msg)
  52. if message.content.startswith('!>') and not message.content.startswith('!>:'):
  53. author = message.author.nick or str(message.author).split('#')[0]
  54. content = message.content[2:]
  55. msg = bytes(">:| {0} says, \"{1}\".\n".format(author, content), "UTF-8")
  56. mc.t.write(msg)
  57. @client.event
  58. async def on_ready(self):
  59. print('Logged in as')
  60. print(client.user.name)
  61. print(client.user.id)
  62. print("--------")
  63. def start(self):
  64. self.t = tn.Telnet(self.server,self.port)
  65. self.t.read_until(b"\"news\"")
  66. self.t.write(bytes("connect {0} {1}\n".format(self.username, self.password),"UTF-8"))
  67. client.run(self.TOKEN)
  68. def listen(self):
  69. while True:
  70. try:
  71. g = b''
  72. g = self.t.read_until(b'\r\n', 1)
  73. if g != b'':
  74. try:
  75. g = g.decode(encoding='iso-8859-1').strip()
  76. except UnicodeDecodeError:
  77. try:
  78. g = g.decode(errors="replace").strip()
  79. except:
  80. g = g.decode(errors="ignore").strip()
  81. client.send_message(message.channel, g)
  82. g = b''
  83. except KeyboardInterrupt:
  84. break
  85. except EOFError:
  86. break
  87. except:
  88. continue
  89. if __name__ == "__main__":
  90. mc = Mushcord()
  91. mc.start()
  92. mc.listen()