Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import discord
- import asyncio
- import time
- import json
- client = discord.Client()
- staff_channel_id = "224239361779040274"
- allothers_channel_id = "225068884959559680"
- allchannels_id = "224582137934905346"
- udp_ip = "127.0.0.1"
- udp_port = 5000
- _debug = False
- channel_list = {}
- class MyDiscordClientdiscord.Client():
- @asyncio.coroutine
- def on_ready(self):
- print('Logged in as ', self.user.name, self.user.id)
- # get registered gamechat channels...
- chans = self.get_all_channels()
- for chan in chans:
- if chan.name.startswith("srv_"):
- srv_code = chan.name[-3:]
- channel_list[srv_code] = chan.id
- if _debug: print(srv_code, chan.id)
- def write_message(self, cmd, extension):
- file = "command_" + str(int(time.time() * 1000)) + "." + extension
- with open ("/home/steam/sharedcontent/data/shared/ulxcmd/"+file, "w") as myfile:
- myfile.write(cmd )
- # We are receiving a message from Discord.
- @asyncio.coroutine
- def on_message(self, message):
- if (message.author.name != "THABBot"): # Don't consider messages from THABBot itself
- # Helpful tips from THABBot
- msgLower = message.content.lower()
- if msgLower.startswith('why'):
- yield from self.send_message(message.channel, 'Ask someone who cares.')
- if msgLower.startswith('how do i'):
- if "points" in msgLower:
- 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')
- else:
- yield from self.send_message(message.channel, 'I have absolutely no idea.')
- for mention in message.mentions:
- # Did someone mention THABBot? If so, send a snarky response.
- if mention.name == "THABBot":
- yield from self.send_message(message.channel, 'Here`s some bleach, ' + message.author.display_name + ". You know what to do." )
- if _debug: print(message.timestamp.strftime("%Y-%m-%d %H:%M:%S") + " - (#" + message.channel.name + ") " + message.author.display_name + ": " + message.content)
- # relay functionality here
- if message.channel.id == staff_channel_id: # Did we say something on staff channel? Then gasay it to all servers
- cmd = "ulx asay [@Discord] " + message.author.display_name + ": " + message.content
- self.write_message(cmd, "txt")
- elif message.channel.name.startswith("srv_"): # did someone say something on a server chat chan? Relay it.
- srv_code = message.channel.name[-3:]
- # was it a command?
- if (message.content.startswith("!")):
- if message.author.id == "224309318697091074":
- cmd = message.content[1:]
- self.write_message(cmd, srv_code)
- else:
- # Assume this is just chat
- cmd = "ulx tsay [@Discord#" + srv_code + "] " + message.author.display_name + ": " + message.content
- self.write_message(cmd, srv_code)
- cmd = "say AdminHide: [@Discord#" + srv_code + "] " + message.author.display_name + ": " + message.content
- self.write_message(cmd, srv_code)
- class MyUDPServer(asyncio.DatagramProtocol):
- def connection_made(self, transport):
- print('start', transport)
- self.transport = transport
- # We are receiving a transmission from globalcommand.py
- def datagram_received(self, data, addr):
- payload = data.decode('ISO-8859-1')
- if _debug: print('Data received:', addr, payload)
- data = json.loads(payload)
- msg = data["msg"]
- # completely ignore anything coming from globalcommand that looks like something originating from Discord.
- # ie, don't get stuck in a feedback loop. This should probably be stopped in globalcommand itself.
- #if "[@Discord]" in msg:
- # return
- # a global asay? Broadcast it to the staff channel
- if msg.startswith("ulx asay "):
- msg_send = msg[9:] # strip ulx asay
- asyncio.async(client.send_message(client.get_channel(staff_channel_id), msg_send ))
- # a global botasay?, post that to the staff channel too
- elif msg.startswith("ulx botasay "):
- msg_send = msg[12:] # strip ulx asay
- asyncio.async(client.send_message(client.get_channel(staff_channel_id), msg_send ))
- # a chat message on a specific, registered server? Post it to the appropriate server channel
- elif data["server"] in channel_list:
- msg_send = "[@" + data["serverName"] + "] " + data["from"] + " : " + msg
- asyncio.async(client.send_message(client.get_channel(channel_list[data["server"]]), msg_send ))
- asyncio.async(client.send_message(client.get_channel(allchannels_id), msg_send ))
- # a chat message on a channel we don't have registered yet? Post it to the catch-all channel.
- elif data["server"] !="":
- msg_send = "[@" + data["serverName"] + "] " + data["from"] + " : " + msg
- asyncio.async(client.send_message(client.get_channel(allothers_channel_id), msg_send ))
- asyncio.async(client.send_message(client.get_channel(allchannels_id), msg_send ))
- else:
- if _debug: print(" - Ignored! - ")
- def error_received(self, exc):
- print('error_received: ', exc)
- def connection_lost(self, exc):
- print('connection_lost: ', exc)
- loop = asyncio.get_event_loop()
- coro = loop.create_datagram_endpoint(MyUDPServer, local_addr=(udp_ip, udp_port))
- server_udp = loop.run_until_complete(coro)
- #loop.run_forever()
- Client().run('NjkzNjUxMzE4ODE2MTEyNzcw.XoAifA.2supTTRayw8YgNtulYD_ePz4Ouc')
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement