1
Fork 0
mirror of https://github.com/RYGhub/royalnet.git synced 2024-11-27 13:34:28 +00:00

Add /bridge command

This commit is contained in:
Steffo 2018-06-14 00:10:57 +02:00
parent 5552c92817
commit e98964eb1b
2 changed files with 55 additions and 9 deletions

View file

@ -243,18 +243,32 @@ async def update_users_pipe(users_connection):
await client.wait_until_ready() await client.wait_until_ready()
while True: while True:
msg = await loop.run_in_executor(executor, users_connection.recv) msg = await loop.run_in_executor(executor, users_connection.recv)
if msg == "/cv": if msg == "get cv":
discord_members = list(client.get_server(config["Discord"]["server_id"]).members) discord_members = list(client.get_server(config["Discord"]["server_id"]).members)
users_connection.send(discord_members) users_connection.send(discord_members)
elif msg.startswith("!"):
data = msg.split(" ")
if data[0] not in commands:
users_connection.send("error")
continue
await commands[data[0]](channel=client.get_channel(config["Discord"]["main_channel"]),
author=None,
params=data[1:] if len(data) > 1 else [])
users_connection.send("success")
def command(func): def command(func):
"""Decorator. Runs the function as a Discord command.""" """Decorator. Runs the function as a Discord command."""
async def new_func(channel: discord.Channel, author: discord.Member, params: typing.List[str], *args, **kwargs): async def new_func(channel: discord.Channel, author: discord.Member, params: typing.List[str], *args, **kwargs):
if author is not None:
sentry.user_context({ sentry.user_context({
"discord_id": author.id, "discord_id": author.id,
"username": f"{author.name}#{author.discriminator}" "username": f"{author.name}#{author.discriminator}"
}) })
else:
sentry.user_context({
"source": "Telegram"
})
try: try:
result = await func(channel=channel, author=author, params=params, *args, **kwargs) result = await func(channel=channel, author=author, params=params, *args, **kwargs)
except Exception: except Exception:
@ -313,6 +327,8 @@ async def cmd_ping(channel: discord.Channel, author: discord.Member, params: typ
@command @command
async def cmd_cv(channel: discord.Channel, author: discord.Member, params: typing.List[str]): async def cmd_cv(channel: discord.Channel, author: discord.Member, params: typing.List[str]):
if author is None:
await client.send_message(channel, "⚠ Questo comando richiede un autore.")
if author.voice.voice_channel is None: if author.voice.voice_channel is None:
await client.send_message(channel, "⚠ Non sei in nessun canale!") await client.send_message(channel, "⚠ Non sei in nessun canale!")
return return
@ -510,6 +526,21 @@ async def queue_play_next_video():
del voice_queue[0] del voice_queue[0]
commands = {
"!ping": cmd_ping,
"!cv": cmd_cv,
"!play": cmd_play,
"!p": cmd_play,
"!skip": cmd_skip,
"!s": cmd_skip,
"!remove": cmd_remove,
"!queue": cmd_queue,
"!q": cmd_queue,
"!shuffle": cmd_shuffle,
"!clear": cmd_clear
}
def process(users_connection=None): def process(users_connection=None):
print("Discordbot starting...") print("Discordbot starting...")
if users_connection is not None: if users_connection is not None:

View file

@ -64,12 +64,12 @@ def cmd_register(bot: Bot, update: Update):
def cmd_discord(bot: Bot, update: Update): def cmd_discord(bot: Bot, update: Update):
if discord_connection is None: if discord_connection is None:
bot.send_message(update.message.chat.id, "⚠ Il bot non è sincronizzato con Discord al momento.") bot.send_message(update.message.chat.id, "⚠ Il bot non è collegato a Discord al momento.")
return return
discord_connection.send("/cv") discord_connection.send("get cv")
server_members = discord_connection.recv() server_members = discord_connection.recv()
channels = {0:None} channels = {0: None}
members_in_channels = {0:[]} members_in_channels = {0: []}
message = "" message = ""
# Find all the channels # Find all the channels
for member in server_members: for member in server_members:
@ -404,6 +404,20 @@ def cmd_profile(bot: Bot, update: Update):
parse_mode="Markdown") parse_mode="Markdown")
def cmd_bridge(bot: Bot, update: Update):
try:
data = update.message.text.split(" ", 1)[1]
except IndexError:
bot.send_message(update.message.chat.id, "⚠ Non hai specificato un comando!\n"
"Sintassi corretta: `/bridge <comando> <argomenti>`")
discord_connection.send(f"!{data}")
result = discord_connection.recv()
if result == "error":
bot.send_message(update.message.chat.id, "⚠ Esecuzione del comando fallita.")
if result == "success":
bot.send_message(update.message.chat.id, "⏩ Comando eseguito su Discord.")
def process(arg_discord_connection): def process(arg_discord_connection):
print("Telegrambot starting...") print("Telegrambot starting...")
if arg_discord_connection is not None: if arg_discord_connection is not None:
@ -425,6 +439,7 @@ def process(arg_discord_connection):
u.dispatcher.add_handler(CommandHandler("eat", cmd_eat)) u.dispatcher.add_handler(CommandHandler("eat", cmd_eat))
u.dispatcher.add_handler(CommandHandler("ship", cmd_ship)) u.dispatcher.add_handler(CommandHandler("ship", cmd_ship))
u.dispatcher.add_handler(CommandHandler("profile", cmd_profile)) u.dispatcher.add_handler(CommandHandler("profile", cmd_profile))
u.dispatcher.add_handler(CommandHandler("bridge", cmd_bridge))
u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query)) u.dispatcher.add_handler(CallbackQueryHandler(on_callback_query))
u.bot.send_message(config["Telegram"]["main_group"], u.bot.send_message(config["Telegram"]["main_group"],
f" Royal Bot avviato e pronto a ricevere comandi!\n" f" Royal Bot avviato e pronto a ricevere comandi!\n"