Advertisement
Guest User

Untitled

a guest
Mar 29th, 2020
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 5.44 KB | None | 0 0
  1. import discord
  2. import asyncio
  3. import time
  4. import json
  5. client = discord.Client()
  6.  
  7. staff_channel_id = "224239361779040274"
  8. allothers_channel_id = "225068884959559680"
  9. allchannels_id = "224582137934905346"
  10.  
  11. udp_ip = "127.0.0.1"
  12. udp_port = 5000
  13. _debug = False
  14.  
  15. channel_list = {}
  16.  
  17. class MyDiscordClient(discord.Client):
  18.  
  19.     @asyncio.coroutine
  20.     def on_ready(self):
  21.         print('Logged in as ', self.user.name, self.user.id)
  22.        
  23.         # get registered gamechat channels...
  24.         chans = self.get_all_channels()
  25.         for chan in chans:
  26.             if chan.name.startswith("srv_"):
  27.                 srv_code = chan.name[-3:]
  28.                 channel_list[srv_code] = chan.id
  29.                 if _debug: print(srv_code, chan.id)
  30.    
  31.    
  32.     def write_message(self, cmd, extension):
  33.         file = "command_" + str(int(time.time() * 1000)) + "." + extension
  34.         with open ("/home/steam/sharedcontent/data/shared/ulxcmd/"+file, "w") as myfile:
  35.             myfile.write(cmd )
  36.    
  37.    
  38.     # We are receiving a message from Discord.
  39.     @asyncio.coroutine
  40.     def on_message(self, message):
  41.        
  42.         if (message.author.name != "THABBot"):  # Don't consider messages from THABBot itself
  43.        
  44.             # Helpful tips from THABBot
  45.             msgLower = message.content.lower()
  46.             if msgLower.startswith('why'):
  47.                 yield from self.send_message(message.channel, 'Ask someone who cares.')
  48.             if msgLower.startswith('how do i'):
  49.                 if "points" in msgLower:
  50.                     yield from self.send_message(message.channel, 'You earn points by spending time in-game on each THAB server. There are also opportunities to win points, including a regularly-scheduled lottery in every Pointshop-enabled gamemmode and gamemmode-specific perks like Slots in Elevator')
  51.                 else:
  52.                     yield from self.send_message(message.channel, 'I have absolutely no idea.')
  53.        
  54.        
  55.        
  56.             for mention in message.mentions:
  57.                 # Did someone mention THABBot? If so, send a snarky response.
  58.                 if mention.name == "THABBot":  
  59.                     yield from self.send_message(message.channel, 'Here`s some bleach, ' + message.author.display_name + ". You know what to do." )
  60.  
  61.             if _debug: print(message.timestamp.strftime("%Y-%m-%d %H:%M:%S") + " - (#" + message.channel.name + ") " + message.author.display_name + ": " + message.content)
  62.            
  63.            
  64.             # relay functionality here
  65.             if message.channel.id == staff_channel_id:  # Did we say something on staff channel? Then gasay it to all servers
  66.                 cmd = "ulx asay [@Discord] " + message.author.display_name  + ": " + message.content
  67.                 self.write_message(cmd, "txt")
  68.             elif message.channel.name.startswith("srv_"): # did someone say something on a server chat chan? Relay it.
  69.                 srv_code = message.channel.name[-3:]
  70.                
  71.                 # was it a command?
  72.                 if (message.content.startswith("!")):
  73.                     if message.author.id == "224309318697091074":
  74.                         cmd = message.content[1:]
  75.                         self.write_message(cmd, srv_code)
  76.                 else:
  77.                     # Assume this is just chat
  78.                     cmd = "ulx tsay [@Discord#" + srv_code + "] " + message.author.display_name  + ": " + message.content
  79.                     self.write_message(cmd, srv_code)
  80.                     cmd = "say AdminHide: [@Discord#" + srv_code + "] "  + message.author.display_name  + ": " + message.content
  81.                     self.write_message(cmd, srv_code)
  82.                
  83.                
  84.  
  85. class MyUDPServer(asyncio.DatagramProtocol):
  86.  
  87.     def connection_made(self, transport):
  88.         print('start', transport)
  89.         self.transport = transport
  90.  
  91.     # We are receiving a transmission from globalcommand.py
  92.     def datagram_received(self, data, addr):
  93.        
  94.         payload = data.decode('ISO-8859-1')
  95.         if _debug: print('Data received:', addr, payload)
  96.         data = json.loads(payload)
  97.         msg = data["msg"]
  98.  
  99.         # completely ignore anything coming from globalcommand that looks like something originating from Discord.
  100.         # ie, don't get stuck in a feedback loop.  This should probably be stopped in globalcommand itself.
  101.         #if "[@Discord]" in msg:
  102.         #   return
  103.        
  104.         # a global asay? Broadcast it to the staff channel
  105.         if msg.startswith("ulx asay "):
  106.             msg_send = msg[9:]                  # strip ulx asay
  107.             asyncio.async(client.send_message(client.get_channel(staff_channel_id), msg_send ))
  108.            
  109.         # a global botasay?, post that to the staff channel too
  110.         elif msg.startswith("ulx botasay "):       
  111.             msg_send = msg[12:]                     # strip ulx asay
  112.             asyncio.async(client.send_message(client.get_channel(staff_channel_id), msg_send ))
  113.            
  114.         # a chat message on a specific, registered server? Post it to the appropriate server channel
  115.         elif data["server"] in channel_list:
  116.             msg_send = "[@" + data["serverName"] + "] " + data["from"] + " : " + msg
  117.             asyncio.async(client.send_message(client.get_channel(channel_list[data["server"]]), msg_send ))
  118.             asyncio.async(client.send_message(client.get_channel(allchannels_id), msg_send ))
  119.            
  120.         # a chat message on a channel we don't have registered yet? Post it to the catch-all channel.
  121.         elif data["server"] !="":
  122.             msg_send = "[@" + data["serverName"] + "] " + data["from"] + " : " + msg
  123.             asyncio.async(client.send_message(client.get_channel(allothers_channel_id), msg_send ))
  124.             asyncio.async(client.send_message(client.get_channel(allchannels_id), msg_send ))
  125.            
  126.         else:
  127.             if _debug: print(" - Ignored! - ")
  128.            
  129.     def error_received(self, exc):
  130.         print('error_received: ', exc)
  131.  
  132.     def connection_lost(self, exc):
  133.         print('connection_lost: ', exc)
  134.    
  135.  
  136.  
  137. loop = asyncio.get_event_loop()
  138. coro = loop.create_datagram_endpoint(MyUDPServer, local_addr=(udp_ip, udp_port))
  139. server_udp = loop.run_until_complete(coro)
  140.  
  141. #loop.run_forever()
  142.  
  143.  
  144. client.run('NjkzNjUxMzE4ODE2MTEyNzcw.XoAxGg.MeuL6JXCFwr8sIHrHA_Ce4Ho-JM')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement