mirror of
https://github.com/RYGhub/royalnet.git
synced 2024-11-27 13:34:28 +00:00
CV!
This commit is contained in:
parent
c1436ffa05
commit
a908990771
3 changed files with 103 additions and 28 deletions
12
bots.py
Normal file
12
bots.py
Normal file
|
@ -0,0 +1,12 @@
|
|||
import multiprocessing
|
||||
import telegrambot
|
||||
import discordbot
|
||||
|
||||
discord_users_pipe = multiprocessing.Pipe()
|
||||
|
||||
discord = multiprocessing.Process(target=discordbot.process, args=(discord_users_pipe[0],))
|
||||
telegram = multiprocessing.Process(target=telegrambot.process, args=(discord_users_pipe[1],))
|
||||
|
||||
if __name__ == "__main__":
|
||||
discord.start()
|
||||
telegram.start()
|
|
@ -6,6 +6,7 @@ import errors
|
|||
|
||||
# Init the event loop
|
||||
import asyncio
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
|
||||
# Init the config reader
|
||||
|
@ -13,30 +14,44 @@ import configparser
|
|||
config = configparser.ConfigParser()
|
||||
config.read("config.ini")
|
||||
|
||||
# Open a new postgres session
|
||||
session = db.Session()
|
||||
|
||||
# Init the discord bot
|
||||
client = discord.Client()
|
||||
|
||||
@client.event
|
||||
async def on_message(message: discord.Message):
|
||||
if message.content.startswith("!register"):
|
||||
try:
|
||||
username = message.content.split(" ", 1)[1]
|
||||
except IndexError:
|
||||
await client.send_message(message.channel, "⚠️ Non hai specificato un username!")
|
||||
return
|
||||
try:
|
||||
d = db.Discord.create(session,
|
||||
royal_username=username,
|
||||
discord_user=message.author)
|
||||
except errors.AlreadyExistingError:
|
||||
await client.send_message(message.channel, "⚠ Il tuo account Discord è già collegato a un account RYG o l'account RYG che hai specificato è già collegato a un account Discord.")
|
||||
return
|
||||
session.add(d)
|
||||
session.commit()
|
||||
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
|
||||
# Open a new postgres session
|
||||
session = db.Session()
|
||||
try:
|
||||
if message.content.startswith("!register"):
|
||||
try:
|
||||
username = message.content.split(" ", 1)[1]
|
||||
except IndexError:
|
||||
await client.send_message(message.channel, "⚠️ Non hai specificato un username!")
|
||||
return
|
||||
try:
|
||||
d = db.Discord.create(session,
|
||||
royal_username=username,
|
||||
discord_user=message.author)
|
||||
except errors.AlreadyExistingError:
|
||||
await client.send_message(message.channel, "⚠ Il tuo account Discord è già collegato a un account RYG o l'account RYG che hai specificato è già collegato a un account Discord.")
|
||||
return
|
||||
session.add(d)
|
||||
session.commit()
|
||||
await client.send_message(message.channel, "✅ Sincronizzazione completata!")
|
||||
finally:
|
||||
session.close()
|
||||
|
||||
client.run(config["Discord"]["bot_token"])
|
||||
session.close()
|
||||
|
||||
async def update_users_pipe(users_connection):
|
||||
await client.wait_until_ready()
|
||||
while True:
|
||||
msg = await loop.run_in_executor(None, users_connection.recv)
|
||||
if msg == "/cv":
|
||||
discord_members = list(client.get_server(config["Discord"]["server_id"]).members)
|
||||
users_connection.send(discord_members)
|
||||
|
||||
|
||||
def process(users_connection):
|
||||
print("Discordbot starting...")
|
||||
loop.create_task(update_users_pipe(users_connection))
|
||||
client.run(config["Discord"]["bot_token"])
|
|
@ -2,12 +2,15 @@ import db
|
|||
import errors
|
||||
from telegram import Bot, Update, Message
|
||||
from telegram.ext import Updater, CommandHandler
|
||||
from discord import Status as DiscordStatus
|
||||
|
||||
# Init the config reader
|
||||
import configparser
|
||||
config = configparser.ConfigParser()
|
||||
config.read("config.ini")
|
||||
|
||||
users_connection = None
|
||||
|
||||
def cmd_register(bot: Bot, update: Update):
|
||||
session = db.Session()
|
||||
try:
|
||||
|
@ -27,10 +30,55 @@ def cmd_register(bot: Bot, update: Update):
|
|||
bot.send_message(update.message.chat.id, "✅ Sincronizzazione completata!")
|
||||
session.close()
|
||||
|
||||
u = Updater(config["Telegram"]["bot_token"])
|
||||
u.dispatcher.add_handler(CommandHandler("register", cmd_register))
|
||||
u.start_polling()
|
||||
try:
|
||||
u.idle()
|
||||
except KeyboardInterrupt:
|
||||
pass
|
||||
|
||||
def cmd_discord(bot: Bot, update: Update):
|
||||
users_connection.send("/cv")
|
||||
server_members = users_connection.recv()
|
||||
message = ""
|
||||
for member in server_members:
|
||||
if member.status == DiscordStatus.offline and member.voice.voice_channel is None:
|
||||
continue
|
||||
if member.bot:
|
||||
continue
|
||||
# Online status emoji
|
||||
if member.status == DiscordStatus.online:
|
||||
message += "🔵 "
|
||||
elif member.status == DiscordStatus.idle:
|
||||
message += "⚫️ "
|
||||
elif member.status == DiscordStatus.dnd:
|
||||
message += "🔴 "
|
||||
elif member.status == DiscordStatus.offline:
|
||||
message += "⚪️ "
|
||||
# Nickname
|
||||
if member.nick is not None:
|
||||
message += member.nick
|
||||
else:
|
||||
message += member.name
|
||||
# Voice
|
||||
if member.voice.voice_channel is not None:
|
||||
# Voice status
|
||||
if member.voice.self_deaf:
|
||||
message += f" | 🔇 {member.voice.voice_channel.name}"
|
||||
elif member.voice.self_mute:
|
||||
message += f" | 🔈 {member.voice.voice_channel.name}"
|
||||
else:
|
||||
message += f" | 🔊 {member.voice.voice_channel.name}"
|
||||
# Game or stream
|
||||
if member.game is not None:
|
||||
if member.game.type == 0:
|
||||
message += f" | 🎮 {member.game.name}"
|
||||
elif member.game.type == 1:
|
||||
message += f" | 📡 [{member.game.name}]({member.game.url})"
|
||||
message += "\n"
|
||||
bot.send_message(update.message.chat.id, message, disable_web_page_preview=True, parse_mode="Markdown")
|
||||
|
||||
|
||||
def process(discord_users_connection):
|
||||
print("Telegrambot starting...")
|
||||
global users_connection
|
||||
users_connection = discord_users_connection
|
||||
u = Updater(config["Telegram"]["bot_token"])
|
||||
u.dispatcher.add_handler(CommandHandler("register", cmd_register))
|
||||
u.dispatcher.add_handler(CommandHandler("discord", cmd_discord))
|
||||
u.start_polling()
|
||||
u.idle()
|
Loading…
Reference in a new issue