Advertisement
rjtwins

python ss13<-->discord bot

Sep 21st, 2016
294
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.55 KB | None | 0 0
  1. import discord, socket, struct, time, asyncio, os, re, atexit, sys, signal
  2.  
  3. from sys import argv
  4. from os import environ
  5. ### config
  6. server_id = '204308700477915136' #server id goes here
  7. channel_id = '207995528779137024' #ahelp channel id goes here
  8. #server IP and Port
  9. ip = '127.0.0.1'
  10. port = 3333
  11. #Pid file for is running checks
  12. pid = str(os.getpid())
  13. pidfile = "discord_bot.pid"
  14. ###
  15. client = discord.Client()
  16.  
  17. #The following two defs are to insure a clean exit and pidfile deletion.
  18. def signal_handler(signal, frame):
  19.         exit_handler()
  20.         sys.exit(0)
  21. signal.signal(signal.SIGINT, signal_handler)
  22.  
  23. @atexit.register
  24. def exit_handler():
  25.         if len(argv) < 3 and os.path.isfile(pidfile):
  26.                 os.unlink(pidfile)
  27.  
  28. # send the ahelp as soon as the client is ready to be used
  29. @client.event
  30. async def on_ready():
  31.         if(len(argv) < 3):
  32.                 return
  33.         message = ''
  34.         if argv[2] == '1':
  35.                 message = '**AHELP:** _' + argv[1] + '_```' + argv[3] + '```'
  36.         else:
  37.                 message = '**PM:** _' + argv[1] + '_ -> _' + argv[2] + '_```' + argv[3] + '```'
  38.         server = client.get_server(server_id)
  39.         ahelp_channel = server.get_channel(channel_id)
  40.         await client.send_message(ahelp_channel, message)
  41.         await client.close()
  42.  
  43. #When a message is sent to the right channel on discord run this.
  44. #Message will be sent along to be exported to the byond server.
  45. @client.event
  46. async def on_message(message):
  47.     if(len(argv) < 3):
  48.         server = client.get_server(server_id)
  49.         if message.channel != server.get_channel(channel_id):
  50.                 return
  51.         if not message.content.startswith('!'):
  52.                 return
  53.         mod = str(message.author)
  54.         pm = message.content
  55.         pm_list = re.split('\s+', pm)
  56.         ckey = pm_list[0]
  57.         message = pm.replace(ckey,"")
  58.         ckey = ckey.replace("!","")
  59.         message = message.replace('\n',"")
  60.         message = message[1:]
  61.         mod = re.sub('[^A-Za-z0-9]+', '', mod)
  62.         ckey = re.sub('[^A-Za-z0-9]+', '', ckey)
  63.         message = re.sub('[^A-Za-z0-9]+', '', message)
  64.         await byond_export('adminmsg'+'&target='+ckey+'&admin='+mod+'&text='+message)
  65.  
  66. #Mimic byond world export to call world/topic() with given string
  67. async def byond_export(string):
  68.         packet_id = b'\x83'
  69.         try:
  70.                 sock = socket.create_connection((ip, port))
  71.         except socket.error:
  72.                 server = client.get_server(server_id)
  73.                 ahelp_channel = server.get_channel(channel_id)
  74.                 await client.send_message(ahelp_channel, "WARNING: Failed to connect to IP:"+ip+":"+str(port))
  75.                 return
  76.         packet = struct.pack('>xcH5x', packet_id, len(string)+6) + bytes(string, encoding='ascii') + b'\x00'
  77.         sock.send(packet)
  78.         data = sock.recv(512)
  79.         sock.close()
  80.  
  81. #If we are running on loop tell the program we are running on loop
  82. if(len(argv) < 3) and not (os.path.isfile(pidfile)):
  83.         file = open(pidfile, 'w')
  84.         file.write(pid)
  85.         file.close()
  86.         client.run(environ['DISCORDTOKEN'])
  87.  
  88. #Else we are not running on loop so tell it to stop when its done.
  89. elif len(argv) >= 3:
  90.     # not using client.run() because this way we can close the connection immediately after sending the ahelp as opposed to having the bot run on the server continually
  91.     try:
  92.         client.loop.run_until_complete(client.start(environ['DISCORDTOKEN']))
  93.     finally:
  94.         client.loop.run_until_complete(client.logout())
  95.         client.loop.close()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement