236 lines
11 KiB
Python
236 lines
11 KiB
Python
#-*- coding: utf-8 -*- ################################################################ SOF
|
|
###########################################################################################
|
|
|
|
???DISCORD???
|
|
|
|
###########################################################################################
|
|
###########################################################################################
|
|
|
|
####################
|
|
if SERVICES_DISCORD:
|
|
####################
|
|
|
|
import asyncio
|
|
import os
|
|
import re
|
|
import sys
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
import discord
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
from tool_colors_plugin import colorform as print
|
|
import irc3
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
from discord import Client
|
|
from discord.ext import commands, tasks
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
from irc3.plugins.command import command
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
from tool_bus_plugin import BUS
|
|
from tool_dims_plugin import dims
|
|
from tool_guds_plugin import guds
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
def getenv(s):
|
|
try:
|
|
s = os.environ[s]
|
|
return s
|
|
except:
|
|
error_type="environmental variable error"
|
|
error_reason=f"exported {s} not found"
|
|
print(f"{error_type}: {error_reason}")
|
|
sys.exit(1)
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
DISCORD__SCHAN=int(getenv('DISCORD__SCHAN'))
|
|
DISCORD__TOKEN=getenv('DISCORD__TOKEN')
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
global client
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
async def _d_bnc_msg(netsrc,usernick,netdest,data):
|
|
netsrc="^{}".format(netsrc)
|
|
bridgedbus=(BUS(netdest))
|
|
await bridgedbus.input(netsrc,usernick,data,True)
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
def d_bnc_msg(netsrc,usernick,netdest,data,):
|
|
ircbot=guds.memory('ircbot')
|
|
asyncio.run_coroutine_threadsafe(_d_bnc_msg(netsrc,usernick,netdest,data),ircbot.loop)
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
def start_discordbot(self):
|
|
print(f'[ discordbot started ]')
|
|
self.discordbot=DISCORDBOT()
|
|
globals()['guds'].memories.append([self.discordbot,'net_discord_plugin:start_discordbot.self'])
|
|
asyncio.set_event_loop(self.loop)
|
|
asyncio.run_coroutine_threadsafe(self.discordbot.client.start(DISCORD__TOKEN),self.loop)
|
|
return self
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
class DISCORDBOT:
|
|
|
|
####################
|
|
if SERVICES_DISCORD:
|
|
####################
|
|
|
|
intents=discord.Intents.default()
|
|
intents.members = True
|
|
intents.messages = True
|
|
intents = discord.Intents.default()
|
|
intents.message_content = True
|
|
client=discord.Client(intents=intents)
|
|
def __init__(self):
|
|
print('[ loaded ]')
|
|
discord.gateway._log=print
|
|
discord.client._log=print
|
|
self.ircbot=guds.memory('ircbot')
|
|
self.dbname="discordchat"
|
|
self.db=self.ircbot.db.getlist(self.dbname)
|
|
if not self.db: self.db=[]
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
@client.event
|
|
async def on_ready():
|
|
print(f'[ discordbot connection made as {globals()["DISCORDBOT"].client.user.name} ]')
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
async def listchannels(self):
|
|
print(f"[ event: scanning channels ] - started")
|
|
discordchannels=list(globals()['DISCORDBOT'].client.get_all_channels())
|
|
if discordchannels[0].__class__.__name__=="CategoryChannel" and discordchannels[0].name=='bridged':
|
|
for channel in discordchannels[0].channels:
|
|
channel_name=f"^{channel.name}"
|
|
result=dims.isdiscordchannel(channel_name)
|
|
if not result:
|
|
dims.__create__('discord',channel_name)
|
|
print(f"[ event: new channel ] - dims.__create__(discord,{channel_name}) - active channel record created")
|
|
globals()['DISCORDBOT'].push(channel.id,channel.name)
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
async def clear():
|
|
discordbot=guds.memory('discordbot')
|
|
client=discordbot.client
|
|
TRAP_FLAG=True
|
|
purge_cycle=0
|
|
while TRAP_FLAG:
|
|
purge_cycle+=1
|
|
channel=client.get_channel(DISCORD__SCHAN)
|
|
await asyncio.sleep(0.1)
|
|
if not channel.last_message==None:
|
|
msg=f'[ purging bridge ] - cycle: {purge_cycle}'
|
|
print(msg)
|
|
await channel.purge()
|
|
else:
|
|
msg=f'[ purging bridge ] - completed'
|
|
print(msg)
|
|
TRAP_FLAG=False
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
def push(id,name):
|
|
class discordchat:
|
|
def __init__(self,id,name):
|
|
self.id=id
|
|
self.name=name
|
|
dc=discordchat(id,name)
|
|
FLAG_FOUND=False
|
|
self=guds.memory('discordbot')
|
|
for entry in self.db:
|
|
if entry[0]==dc.id:
|
|
FLAG_FOUND=True
|
|
break
|
|
if not FLAG_FOUND:
|
|
self.db.append([dc.id,f"^{dc.name}"])
|
|
self.ircbot.db.setlist(self.dbname,self.db)
|
|
print(f'[ new database entry ] > [{dc.id},^{dc.name}]')
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
@client.event
|
|
async def on_message(message):
|
|
discordbot=guds.memory('discordbot')
|
|
client=discordbot.client
|
|
if message.author.name == client.user.name:
|
|
return
|
|
discordbot=guds.memory('discordbot')
|
|
asyncio.run_coroutine_threadsafe(discordbot.listchannels(),discordbot.client.loop)
|
|
channel_name=f"^{message.channel.name}"
|
|
TARGET="^χιϛ"
|
|
USER=message.author.name
|
|
if not message.content.lower().find('maple')==-1:
|
|
ircbot=guds.memory('ircbot')
|
|
asyncio.run_coroutine_threadsafe(ircbot.indirect_maple([USER,TARGET,"#tcpdirect"],message.content),ircbot.loop)
|
|
ircbot=guds.memory('ircbot')
|
|
netschanlist=dims.list_channels_bridged(netsrc=TARGET)
|
|
netschanlist.append(TARGET)
|
|
bridgedbus=(BUS(netschanlist))
|
|
asyncio.run_coroutine_threadsafe(bridgedbus.input(TARGET,USER,message.content,True),ircbot.loop)
|
|
msg=f'[ (d)({message.channel.id})({channel_name}) ] > {str(message.author)}: {message.content}'
|
|
print(msg)
|
|
|
|
###############################################################################
|
|
###############################################################################
|
|
|
|
async def _d_discord_msg(self,target,msg):
|
|
discordbot=guds.memory('discordbot')
|
|
client=discordbot.client
|
|
if [x.id for x in list(client.get_all_channels()) if x.name==target[1:]][0]:
|
|
channel=client.get_channel([x.id for x in list(client.get_all_channels()) if x.name==target[1:]][0])
|
|
await channel.send(msg)
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
def d_discord_cmd(target,cmd,):
|
|
discordbot=guds.memory('discordbot')
|
|
if cmd=="purgebridges":
|
|
asyncio.run_coroutine_threadsafe(discordbot.clear(),discordbot.client.loop)
|
|
|
|
#######################################################################################
|
|
#######################################################################################
|
|
|
|
def d_discord_msg(target,msg,):
|
|
discordbot=guds.memory('discordbot')
|
|
asyncio.run_coroutine_threadsafe(discordbot._d_discord_msg(target,msg),discordbot.client.loop)
|
|
|
|
###########################################################################################
|
|
####################################################################################### EOF
|