590 lines
No EOL
36 KiB
Python
590 lines
No EOL
36 KiB
Python
# Modules for the system, these should be pre-installed with Python.
|
|
import json, os, sys, traceback, time, threading, requests, socket, subprocess, nio, asyncio
|
|
from sys import argv
|
|
# Modules for platforms
|
|
import guilded, discord
|
|
global alive
|
|
alive = True
|
|
client = guilded.Client()
|
|
config = {}
|
|
if len(sys.argv) == 2:
|
|
config = json.load(open(sys.argv[1]))["sb_config"]
|
|
else:
|
|
print("SweeBridge takes one argument: config.json")
|
|
sys.exit(1)
|
|
print("Building configuration...")
|
|
targets = {
|
|
"guilded": {
|
|
|
|
},
|
|
"discord": {
|
|
|
|
},
|
|
"libera": {
|
|
|
|
},
|
|
"anthro": {
|
|
|
|
},
|
|
"snoo": {
|
|
|
|
}
|
|
}
|
|
for key, value in config.items():
|
|
if "discord" in value:
|
|
targetid = str(value["discord"]["guild"]) + "_" + str(value["discord"]["channel"])
|
|
targets["discord"][targetid] = key
|
|
if "guilded" in value:
|
|
targetid = value["guilded"]["server"] + "_" + value["guilded"]["channel"]
|
|
targets["guilded"][targetid] = key
|
|
if "libera" in value:
|
|
targets["libera"][value["libera"]["channel"]] = key
|
|
if "anthro" in value:
|
|
targets["anthro"][value["anthro"]["channel"]] = key
|
|
if "snoo" in value:
|
|
targets["snoo"][value["snoo"]["channel"]] = key
|
|
print("Now booting sweeBridge...")
|
|
class bot_irc:
|
|
irc_socket = socket.socket()
|
|
def __init__(self):
|
|
self.irc_socket = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
|
|
def send_irc(self, channel, msg):
|
|
self.irc_socket.send(bytes(f"PRIVMSG {channel} :{msg}\r\n", "UTF-8"))
|
|
def connect_irc(self, server, port, bot_nick, bot_pass, bot_nickpass):
|
|
print("Server connection: " + server)
|
|
self.irc_socket.connect((server, port))
|
|
self.irc_socket.send(bytes(f"USER {bot_nickpass} 0 * :A very experimental bridge\r\nNICK {bot_nick}\r\nPASS {bot_nickpass}:{bot_pass}\r\n", "UTF-8"))
|
|
def response_irc(self):
|
|
r = self.irc_socket.recv(2040).decode("UTF-8")
|
|
if r.find("PING") != -1:
|
|
self.irc_socket.send(bytes("PONG " + r.split()[1] + "\r\n", "UTF-8"))
|
|
return r
|
|
server_irc = "127.0.0.1" # Use a local ZNC...
|
|
port_irc = 5000 # ...Running at port 5000 without SSL
|
|
global libera
|
|
libera = bot_irc()
|
|
libera.connect_irc(
|
|
server_irc, port_irc, os.getenv("LiberaNick"), os.getenv("ZNCPassword"), os.getenv("LiberaUsername")
|
|
)
|
|
userlist = {}
|
|
def hook(url:str, json:dict):
|
|
try:
|
|
return requests.post(url=url, json=json)
|
|
except:
|
|
return False
|
|
def parse_content(text:str, platform:int, target:str):
|
|
# e = [
|
|
# Text for use in IRC,
|
|
# Text for use in Discord,
|
|
# Text for use in Guilded,
|
|
# Text for use in [UNUSED]
|
|
# ]
|
|
# platform =
|
|
# 1 - IRC
|
|
# 2 - Discord
|
|
# 3 - Guilded
|
|
# platform will be the value of the incoming platform, not any outgoing ones.
|
|
e = [text]
|
|
if platform == 1 and "discord" in config[target]: # Parse IRC -> Discord
|
|
try:
|
|
members = {}
|
|
for i in client2.get_guild(config[target]["discord"]["guild"]).members:
|
|
try:
|
|
if not i.bot:
|
|
members[i.display_name.lower()] = i.id
|
|
except:
|
|
print(traceback.format_exc())
|
|
parsed = text.split("d@") # Split by the prefix for discord mention (d@)
|
|
temp = []
|
|
for i in parsed:
|
|
j = i.split(" ")
|
|
print(j)
|
|
broken = False
|
|
for k in range(0,len(j)+1):
|
|
possibleUser = " ".join(j[:k]).lower()
|
|
if possibleUser in members:
|
|
temp.append("<@" + str(members[possibleUser]) + "> ")
|
|
temp.append(" ".join(j[k:]))
|
|
broken = True
|
|
break
|
|
if not broken:
|
|
temp.append(i) # Aknowledge the typo ;3
|
|
e.append("".join(temp))
|
|
except:
|
|
# Just append the text, don't interrupt the parse.
|
|
e.append(text)
|
|
else:
|
|
e.append(text)
|
|
if platform != 3 and "guilded" in config[target]: # Parse IRC -> Guilded:
|
|
# Links workaround for Guilded
|
|
temp = ""
|
|
parsed = text.split(" ")
|
|
for index, i in enumerate(parsed):
|
|
if "http://" in i or "https://" in i:
|
|
parsed[index] = f"[{i}]({i})"
|
|
temp = " ".join(parsed)
|
|
e.append(temp)
|
|
else:
|
|
e.append(text)
|
|
e.append(text)
|
|
return e
|
|
def ircsession(platform:str, sock:bot_irc):
|
|
while True:
|
|
global userlist
|
|
try:
|
|
textt = sock.response_irc().replace("\r", "").split("\n")
|
|
except:
|
|
print(traceback.format_exc())
|
|
break
|
|
for text in textt:
|
|
try:
|
|
if text != "":
|
|
print([text])
|
|
content = ":".join(" ".join(text.split(" ")[1:]).split(":")[1:])
|
|
chan = text.split(" ")[2]
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
except:
|
|
if text != "":
|
|
print("error")
|
|
try:
|
|
if "PRIVMSG" in text:
|
|
# Check if the channel is part of the targets list.
|
|
target = targets[platform][chan] # Define the target for use further in the code.
|
|
channel_irc = chan
|
|
flags_irc = config[target][platform]["flags"] # We don't get the flags out-of-the-box, so get the flags from config.json
|
|
gilwebhook = None
|
|
discwebhook = None
|
|
externalirc1 = None # Libera.Chat
|
|
externalirc2 = None # SnooNet
|
|
externalirc3 = {"channel": config[target]["anthro"]["channel"], "flags": config[target]["anthro"]["flags"]} if "anthro" in config[target] and platform != "anthro" else None # AnthroChat
|
|
if chan in targets[platform]: # the dictionary of targets should contain the channel as the key, and the community identifier as the value.
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"] # Define the webhook for Guilded
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"] # Define the webhook for Discord
|
|
if "libera" in config[target] and platform != "libera": # Define the Libera.Chat channel & its flags if it isn't the current platform used by the IRC thread.
|
|
externalirc1 = {"channel": config[target]["libera"]["channel"], "flags": config[target]["libera"]["flags"]}
|
|
if "snoo" in config[target] and platform != "snoo" : # Define the SnooNet channel & its flags if it isn't the current platform used by the IRC thread.
|
|
externalirc2 = {"channel": config[target]["snoo"]["channel"], "flags": config[target]["snoo"]["flags"]}
|
|
try:
|
|
content2 = parse_content(content, 1, target)
|
|
parse_success = True
|
|
except:
|
|
print(traceback.format_exc())
|
|
parse_success = False
|
|
pass
|
|
irc_nosystem = flags_irc["nosystem"] == "full"
|
|
irc_partial = flags_irc["nosystem"] == "partial"
|
|
discord_enable = discwebhook != None
|
|
guilded_enable = gilwebhook != None
|
|
nickin = False # If the person is currently boarded in the channel (used in QUIT)
|
|
print(text)
|
|
if content[:8] == "\x01ACTION ":
|
|
if guilded_enable: hook(gilwebhook, json={"content": "***" + nick + " " + content[8:-1], "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "***" + nick + " " + content[8:-1], "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
else:
|
|
if guilded_enable: hook(gilwebhook, json={"content":content2[2] if parse_success else content, "username": nick, "avatar_url": "https://api.dicebear.com/9.x/identicon/png?seed=" + nick})
|
|
if discord_enable: hook(discwebhook, json={"content":content2[1] if parse_success else content, "username": nick, "avatar_url": "https://api.dicebear.com/9.x/identicon/png?seed=" + nick})
|
|
if content.split(" ")[0] == "b$guildedusers":
|
|
if guilded_enable:
|
|
liste = ""
|
|
members = client.get_server(config[target]["guilded"]["server"]).members
|
|
for i in members:
|
|
try:
|
|
if not i.bot:
|
|
liste += "[" + i.display_name + "], "
|
|
except:
|
|
print(traceback.format_exc())
|
|
sock.send_irc(channel_irc, liste[:-2])
|
|
hook(gilwebhook, json={"content": "[Everyone here should already know who's in this server lmao]", "username": "SweeBridge", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
if discord_enable: hook(discwebhook, json={"content": liste[:-2], "username": "SweeBridge Guilded", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
else:
|
|
sock.send_irc(channel_irc, "Error: Guilded isn't set up in this community!")
|
|
if discord_enable: hook(discwebhook, json={"content": "Error: Guilded isn't set up in this community!", "username": "SweeBridge Guilded", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
if content.split(" ")[0] == "b$fixnick":
|
|
if guilded_enable:
|
|
sock.irc_socket.send(b"NICK sweeBridge\n")
|
|
sock.send_irc(channel_irc, "Attempted to regain nickname.")
|
|
if guilded_enable: hook(gilwebhook, json={"content": "Attempted to regain nickname.", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "Attempted to regain nickname.", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if content.split(" ")[0] == "b$discordusers":
|
|
if discord_enable:
|
|
liste = ""
|
|
members = client2.get_guild(config[target]["discord"]["guild"]).members
|
|
for i in members:
|
|
try:
|
|
if not i.bot:
|
|
liste += "[" + i.display_name + "], "
|
|
except:
|
|
print(traceback.format_exc())
|
|
sock.send_irc(channel_irc, liste[:-2])
|
|
if guilded_enable: hook(gilwebhook, json={"content": liste[:-2], "username": "SweeBridge Discord", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-discord.png"})
|
|
hook(discwebhook, json={"content": "[Everyone here should already know who's in this server lmao]", "username": "SweeBridge Discord", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-discord.png"})
|
|
else:
|
|
sock.send_irc(channel_irc, "Error: Discord isn't set up in this community!")
|
|
if guilded_enable: hook(gilwebhook, json={"content": "Error: Discord isn't set up in this community!", "username": "SweeBridge Discord", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-discord.png"})
|
|
if content.split(" ")[0] == "b$pull":
|
|
updater = subprocess.run(["git", "pull"], stdout=subprocess.PIPE)
|
|
if updater.stdout.decode().strip() == "Already up to date.":
|
|
sock.send_irc(channel_irc, "[⚠️] SweeBridge is already up-to-date.")
|
|
else:
|
|
sock.send_irc(channel_irc, "[✅] SweeBridge updated, restarting now!")
|
|
global alive
|
|
alive = False
|
|
time.sleep(2)
|
|
sys.exit()
|
|
elif "QUIT" in text:
|
|
for key, value in userlist.items():
|
|
if nick in value:
|
|
target = targets[platform][key]
|
|
guilded_enable = "guilded" in config[target]
|
|
discord_enable = "discord" in config[target]
|
|
gilwebhook = config[target]["guilded"]["webhook"] if guilded_enable else None
|
|
discwebhook = config[target]["discord"]["webhook"] if discord_enable else None
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " Has quit for reason \"" + " ".join(text.split(" ")[2:])[1:] + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " Has quit for reason \"" + " ".join(text.split(" ")[2:])[1:] + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif "JOIN" in text:
|
|
channel_irc = text.split(" ")[2]
|
|
target = targets[platform][channel_irc]
|
|
flags = config[target][platform]["flags"]
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
if flags["nosystem"] != "full":
|
|
if not "sweeBrid" in nick:
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " Has joined " + channel_irc, "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " Has joined " + channel_irc, "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif ("PART" in text and channel_irc in text) and not irc_nosystem:
|
|
channel_irc = text.split(" ")[2]
|
|
target = targets[platform][channel_irc]
|
|
flags = config[target][platform]["flags"]
|
|
if flags["nosystem"] != "full":
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " Has left " + channel_irc + " for reason \"" + content + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " Has left "+channel_irc+" for reason \"" + content + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif "NICK" in text:
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
for channel_irc, nicks in userlist.items():
|
|
for i in nicks:
|
|
if nick == i.replace("@", "").replace("+", "").replace("~", "").replace("&", ""):
|
|
target = targets[platform][channel_irc]
|
|
flags = config[target][platform]["flags"]
|
|
if flags["nosystem"] != "full":
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " Has changed nick to " + text.split(" ")[2][1:], "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " Has changed nick to " + text.split(" ")[2][1:], "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif "MODE" in text:
|
|
mode = text.split(" ")[3]
|
|
user = text.split(" ")[4]
|
|
channel_irc = text.split(" ")[2]
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
target = targets[platform][channel_irc]
|
|
flags = config[target][platform]["flags"]
|
|
if flags["nosystem"] == "disable": # Both 'partial' and 'full' block MODE.
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
if mode.lower() == "+o":
|
|
mode = "gives operator to"
|
|
elif mode.lower() == "-o":
|
|
mode = "removes operator from"
|
|
elif mode.lower() == "+v":
|
|
mode = "gives voice to"
|
|
elif mode.lower() == "-v":
|
|
mode = "removes voice from"
|
|
elif mode.lower() == "+b":
|
|
mode = "adds a ban to hostmask"
|
|
elif mode.lower() == "-b":
|
|
mode = "removes a ban from hostmask"
|
|
else:
|
|
mode = "sets mode " + mode + " on"
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " " + mode + " " + user, "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " " + mode + " " + user, "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif "KICK" in text:
|
|
channel_irc = text.split(" ")[2]
|
|
nick = text.split(" ")[0][1:].split("!")[0]
|
|
target = targets[platform][channel_irc]
|
|
flags = config[target][platform]["flags"]
|
|
if flags["nosystem"] != "full":
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
if guilded_enable: hook(gilwebhook, json={"content": nick + " Kicked " + text.split(" ")[3] + " for reason \"" + ":".join(text.split(":")[2:]) + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
if discord_enable: hook(discwebhook, json={"content": nick + " Kicked " + text.split(" ")[3] + " for reason \"" + ":".join(text.split(":")[2:]) + "\"", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-" + platform + ".png"})
|
|
update(channel_irc, sock)
|
|
elif "353" in text:
|
|
channel_irc = text.split(" ")[4]
|
|
userlist[channel_irc] = text.split(":")[2].split(" ")
|
|
print("Updated userlist for " + channel_irc)
|
|
elif "ERROR :Closing Link:" in text:
|
|
break
|
|
except:
|
|
print(traceback.format_exc())
|
|
def update(channel_irc:str, sock:bot_irc):
|
|
sock.irc_socket.send(bytes("NAMES " + channel_irc + "\n", "UTF-8"))
|
|
irc1 = threading.Thread(target=ircsession, args=["libera", libera], daemon=True)
|
|
# Guilded
|
|
@client.event
|
|
async def on_message(message):
|
|
# Try to get the target
|
|
target = targets["guilded"][message.server.id + "_" + message.channel.id]
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
if "discord" in config[target]:
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
discord_enable = True
|
|
else:
|
|
discwebhook = None
|
|
discord_enable = False
|
|
if "libera" in config[target]:
|
|
libera_channel = config[target]["libera"]["channel"]
|
|
libera_enable = True
|
|
else:
|
|
libera_channel = None
|
|
libera_enable = False
|
|
try:
|
|
pfp = message.author.avatar.url
|
|
except:
|
|
print(traceback.format_exc())
|
|
pfp = None
|
|
if message.author.display_name != "":
|
|
if message.content[:3] == "/me" or message.content[:4] == "b$me":
|
|
await message.delete()
|
|
hook(gilwebhook, json={"content": "***[" + message.author.display_name + "] (Guilded) " + " ".join(message.content.split(" ")[1:]), "username": "SweeBridge Guilded", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "***[" + message.author.display_name + "] (Discord) " + message.content, "username": "SweeBridge Guilded", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
if libera_enable: libera.send_irc(libera_channel, "***[" + message.author.display_name +"] (Guilded) " + message.content.replace("\n","(Newline)"))
|
|
else:
|
|
if pfp != None:
|
|
if discord_enable: hook(discwebhook, json={"content": message.content, "username": message.author.display_name, "avatar_url": pfp})
|
|
else:
|
|
if discord_enable: hook(discwebhook, json={"content": message.content, "username": message.author.display_name, "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-guilded.png"})
|
|
if libera_enable: libera.send_irc(libera_channel, "[" + message.author.display_name +"] (Guilded): " + message.content.replace("\n","(Newline)"))
|
|
if message.content[:13] == "b$liberausers":
|
|
if libera_enable:
|
|
temp = ""
|
|
for i in userlist[libera_channel]:
|
|
if i[0] == "@":
|
|
temp += i[1:] + " (Operator)\n"
|
|
elif i[0] == "+":
|
|
temp += i[1:] + " (Voice)\n"
|
|
else:
|
|
temp += i + "\n"
|
|
hook(gilwebhook, json={"content": "Users in " + libera_channel + " (Libera.Chat):\n" + temp[:-1], "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-libera.png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "Users in " + libera_channel + " (Libera.Chat):\n" + temp[:-1], "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-libera.png"})
|
|
libera.send_irc(libera_channel, "[Everyone here should already know who's in this chan lmao]")
|
|
else:
|
|
hook(gilwebhook, json={"content": "Error: Libera.Chat is not set up in this community!", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-libera.png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "Error: Libera.Chat is not set up in this community!", "username": "SweeBridge IRC", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-libera.png"})
|
|
if message.content[:14] == "b$discordusers":
|
|
if discord_enable:
|
|
liste = ""
|
|
members = client2.get_guild(config[target]["discord"]["guild"]).members
|
|
for i in members:
|
|
try:
|
|
if not i.bot:
|
|
liste += "[" + i.display_name + "], "
|
|
except:
|
|
print(traceback.format_exc())
|
|
if libera_enable: libera.send_irc(libera_channel, liste[:-2])
|
|
hook(gilwebhook, json={"content": liste[:-2], "username": "SweeBridge Discord", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-discord.png"})
|
|
if discord_enable: hook(discwebhook, json={"content": "[Everyone here should already know who's in this server lmao]", "username": "SweeBridge Discord", "avatar_url": "https://git.swee.codes/swee/SweeBridge/raw/branch/main/logos/sweebridge-discord.png"})
|
|
else:
|
|
if libera_enable: libera.send_irc(libera_channel, "Error: Discord is not set up in this community!")
|
|
await message.reply("Error: Discord is not set up in this community!")
|
|
if message.content.split(" ")[0] == "b$pull":
|
|
updater = subprocess.run(["git", "pull"], stdout=subprocess.PIPE)
|
|
if updater.stdout.decode().strip() == "Already up to date.":
|
|
await message.reply("[⚠️] SweeBridge is already up-to-date.")
|
|
else:
|
|
await message.reply("[✅] SweeBridge updated, restarting now!")
|
|
global alive
|
|
alive = False
|
|
time.sleep(2)
|
|
sys.exit()
|
|
if message.content[:13] == "b$chandetails" and not message.author.bot:
|
|
await message.reply("* Server ID: " + message.server.id + "\n* Channel ID: " + message.channel.id)
|
|
class MyClient(discord.Client):
|
|
async def on_ready(self):
|
|
await client2.change_presence(activity=discord.Activity(name="Welcome to the new SweeBridge!", type=discord.ActivityType.watching))
|
|
async def on_message(self, message):
|
|
try:
|
|
pfp = message.author.avatar.url
|
|
except:
|
|
pfp = None
|
|
target = targets["discord"][str(message.guild.id) + "_" + str(message.channel.id)]
|
|
discwebhook = config[target]["discord"]["webhook"]
|
|
if "guilded" in config[target]:
|
|
gilwebhook = config[target]["guilded"]["webhook"]
|
|
guilded_enable = True
|
|
else:
|
|
gilwebhook = None
|
|
guilded_enable = False
|
|
if "libera" in config[target]:
|
|
libera_channel = config[target]["libera"]["channel"]
|
|
libera_enable = True
|
|
else:
|
|
libera_channel = None
|
|
libera_enable = False
|
|
print(f'Message from {message.author} ({message.author.id}) on {message.channel.id}: {message.content}')
|
|
print(str(message.author))
|
|
if str(message.author)[-5:] != "#0000":
|
|
if message.content != None and message.content != "":
|
|
if message.content[:3] == "/me" or message.content[:4] == "b$me":
|
|
await message.delete()
|
|
if guilded_enable: hook(gilwebhook, json={"content": "***[" + message.author.display_name + "] (Guilded) " + message.content[3:]})
|
|
hook(discwebhook, json={"content": "***[" + message.author.display_name + "] (Discord) " + message.content[3:]})
|
|
libera.send_irc(libera_channel, "***[" + message.author.display_name +"] (Discord) " + " ".join(message.content.replace("\n","(Newline)").split(" ")[1:]))
|
|
elif message.content[0] == "_" and libera_enable:
|
|
if guilded_enable: hook(gilwebhook, json={"content": "***[" + message.author.display_name + "] (Guilded) " + message.content[1:-1]})
|
|
libera.send_irc(libera_channel, "***[" + message.author.display_name +"] (Discord) " + message.content.replace("\n","(Newline)")[1:-1])
|
|
else:
|
|
if pfp != None:
|
|
if guilded_enable: hook(gilwebhook, json={"content": message.content, "username": message.author.display_name.replace("_", " "), "avatar_url": pfp})
|
|
else:
|
|
if guilded_enable: hook(gilwebhook, json={"content": message.content, "username": message.author.display_name.replace("_", " ")})
|
|
if libera_enable: libera.send_irc(libera_channel, "[" + message.author.display_name +"] (Discord): " + message.content.replace("\n","(Newline)"))
|
|
if message.content[:13] == "b$liberausers":
|
|
if libera_enable:
|
|
temp = ""
|
|
for i in userlist[libera_channel]:
|
|
if i[0] == "@":
|
|
temp += i[1:] + " (Operator)\n"
|
|
elif i[0] == "+":
|
|
temp += i[1:] + " (Voice)\n"
|
|
else:
|
|
temp += i + "\n"
|
|
hook(discwebhook, json={"content": f"Users in {libera_channel} IRC channel (Libera.Chat):\n" + temp[:-1]})
|
|
if guilded_enable: hook(gilwebhook, json={"content": f"Users in {libera_channel} IRC channel (Libera.Chat):\n" + temp[:-1]})
|
|
if libera_enable: libera.send_irc(libera_channel, "[Everyone here should already know who's in this chan lmao]")
|
|
else:
|
|
await message.reply("Error: IRC is not set up in this community!")
|
|
if guilded_enable: hook(gilwebhook, json={"content": "Error: IRC is not set up in this community!"})
|
|
if message.content[:14] == "b$guildedusers":
|
|
liste = ""
|
|
members = client.get_server(config[target]["guilded"]["server"]).members
|
|
for i in members:
|
|
try:
|
|
if not i.bot:
|
|
liste += "[" + i.display_name + "], "
|
|
except:
|
|
print(traceback.format_exc())
|
|
if libera_enable: libera.send_irc(libera_channel, liste[:-2])
|
|
hook(discwebhook, json={"content": liste[:-2]})
|
|
if guilded_enable: hook(gilwebhook, json={"content": "[Everyone here should already know who's in this server lmao]"})
|
|
if message.content.split(" ")[0] == "b$pull":
|
|
updater = subprocess.run(["git", "pull"], stdout=subprocess.PIPE)
|
|
if updater.stdout.decode().strip() == "Already up to date.":
|
|
await message.reply("[⚠️] SweeBridge is already up-to-date.")
|
|
else:
|
|
await message.reply("[✅] SweeBridge updated, restarting now!")
|
|
global alive
|
|
alive = False
|
|
time.sleep(2)
|
|
sys.exit()
|
|
if len(message.attachments) > 0:
|
|
liste = ""
|
|
guild_liste = ""
|
|
for i in message.attachments:
|
|
try:
|
|
liste += i.url + " ,"
|
|
guild_liste += f"[{i.filename}]({i.url}),\n"
|
|
except:
|
|
liste += "Error getting attachment ,"
|
|
if guilded_enable: hook(gilwebhook, json={"embeds": [{"title": "Message attachments","description": guild_liste[:-2]}], "username": message.author.display_name})
|
|
if libera_enable: libera.send_irc(libera_channel, "[" + message.author.display_name +"] (Discord) Message attachments: " + liste[:-2])
|
|
if message.content[:13] == "b$chandetails":
|
|
await message.reply("* Guild ID: " + str(message.guild.id) + "\n* Channel ID: " + str(message.channel.id))
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
intents.members = True
|
|
client2 = MyClient(intents=intents)
|
|
guild = threading.Thread(target=client.run, args=[os.getenv("GuildedToken")], daemon=True)
|
|
disc = threading.Thread(target=client2.run, args=[os.getenv("DiscordToken")], daemon=True)
|
|
async def matrixSession():
|
|
Mtrix = nio.AsyncClient(os.getenv("MatrixHomeserver"), os.getenv("MatrixUsername"))
|
|
Mtrix.access_token = os.getenv("MatrixToken")
|
|
Mtrix.user_id = os.getenv("MatrixUsername")
|
|
while True:
|
|
try:
|
|
await Mtrix.load_store()
|
|
sync = await Mtrix.sync(30000)
|
|
finally:
|
|
Mtrix.close()
|
|
|
|
|
|
def runMatrix():
|
|
asyncio.run(matrixSession())
|
|
matrix = threading.Thread(target=runMatrix)
|
|
#matrix.start()
|
|
irc1.start()
|
|
guild.start()
|
|
disc.start()
|
|
while alive:
|
|
if not irc1.is_alive():
|
|
time.sleep(5)
|
|
irc1 = threading.Thread(target=ircsession, args=["libera", libera])
|
|
libera.irc_socket.close()
|
|
libera = bot_irc()
|
|
libera.connect_irc("127.0.0.1", "5000", os.getenv("LiberaNick"), os.getenv("ZNCPassword"), os.getenv("LiberaUsername"))
|
|
print("respawning IRC thread...")
|
|
time.sleep(1)
|
|
irc1.start()
|
|
if not guild.is_alive():
|
|
sys.exit()
|
|
if not disc.is_alive():
|
|
sys.exit() |