Titan/discordbot/titanembeds/socketio/socketiointerface.py

73 lines
3.8 KiB
Python
Raw Normal View History

2017-08-20 21:56:54 +02:00
import socketio
from titanembeds.utils import get_message_author, get_message_mentions, get_roles_list, get_attachments_list, get_embeds_list, get_formatted_message, get_formatted_user, get_formatted_emojis, get_formatted_guild, get_formatted_channel, get_formatted_role
2018-03-23 00:07:52 +01:00
import discord
2017-08-20 21:56:54 +02:00
class SocketIOInterface:
def __init__(self, bot, redis_uri):
self.io = socketio.AsyncRedisManager(redis_uri, write_only=True, channel='flask-socketio')
self.bot = bot
async def on_message(self, message):
if message.guild:
msg = get_formatted_message(message)
await self.io.emit('MESSAGE_CREATE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway')
async def on_message_delete(self, message):
if message.guild:
msg = get_formatted_message(message)
await self.io.emit('MESSAGE_DELETE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway')
async def on_message_update(self, message):
if message.guild:
msg = get_formatted_message(message)
await self.io.emit('MESSAGE_UPDATE', data=msg, room=str("CHANNEL_"+str(message.channel.id)), namespace='/gateway')
async def on_guild_member_add(self, member):
user = get_formatted_user(member)
await self.io.emit('GUILD_MEMBER_ADD', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway')
async def on_guild_member_remove(self, member):
user = get_formatted_user(member)
await self.io.emit('GUILD_MEMBER_REMOVE', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway')
async def on_guild_member_update(self, member):
user = get_formatted_user(member)
await self.io.emit('GUILD_MEMBER_UPDATE', data=user, room=str("GUILD_"+str(member.guild.id)), namespace='/gateway')
async def on_guild_emojis_update(self, emojis):
emotes = get_formatted_emojis(emojis)
await self.io.emit('GUILD_EMOJIS_UPDATE', data=emotes, room=str("GUILD_"+str(emojis[0].guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_guild_update(self, guild):
guildobj = get_formatted_guild(guild)
await self.io.emit('GUILD_UPDATE', data=guildobj, room=str("GUILD_"+str(guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_channel_delete(self, channel):
if str(channel.type) != "text":
return
chan = get_formatted_channel(channel)
await self.io.emit('CHANNEL_DELETE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_channel_create(self, channel):
if str(channel.type) != "text":
return
chan = get_formatted_channel(channel)
await self.io.emit('CHANNEL_CREATE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_channel_update(self, channel):
2018-03-23 00:07:52 +01:00
if not isinstance(channel, discord.channel.TextChannel) and not isinstance(channel, discord.channel.CategoryChannel):
2017-08-25 08:37:14 +02:00
return
chan = get_formatted_channel(channel)
await self.io.emit('CHANNEL_UPDATE', data=chan, room=str("GUILD_"+str(channel.guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_guild_role_create(self, role):
rol = get_formatted_role(role)
await self.io.emit('GUILD_ROLE_CREATE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_guild_role_update(self, role):
rol = get_formatted_role(role)
await self.io.emit('GUILD_ROLE_UPDATE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway')
2017-08-25 08:37:14 +02:00
async def on_guild_role_delete(self, role):
rol = get_formatted_role(role)
await self.io.emit('GUILD_ROLE_DELETE', data=rol, room=str("GUILD_"+str(role.guild.id)), namespace='/gateway')