Ejemplo n.º 1
0
        private void MapGuildMessage(MessageDescriptor message, GuildMapper mapper)
        {
            if (!mapper.MappedGuilds.TryGetValue(message.guild_id.Value, out GuildDescriptor guild))
            {
                throw new MappingException("Guild ID contained in message not previously mapped.", message.guild_id.Value, message);
            }
            message.Guild = guild;

            ChannelDescriptor channel = guild.Channels.FirstOrDefault(c => c.Id == message.channel_id);

            message.Channel = channel ?? throw new MappingException("Channel ID contained in message not previously mapped", message.channel_id, message, guild);

            MemberDescriptor member = guild.Members.FirstOrDefault(m => m.User.Id == message.author.id);

            message.Sender = member ?? throw new MappingException("Author ID contained in message not previously mapped", message.author.id, message, guild);

            message.MentionedRoles = guild.Roles.Join(inner: message.mention_roles,
                                                      outerKeySelector: rD => rD.Id,
                                                      innerKeySelector: id => id,
                                                      resultSelector: (rD, id) => rD).ToList();

            message.MentionedUsers = guild.Members.Join(inner: message.mentions,
                                                        outerKeySelector: mD => mD.User.Id,
                                                        innerKeySelector: uO => uO.id,
                                                        resultSelector: (mD, uO) => mD).ToList();
        }
Ejemplo n.º 2
0
 internal void Map(MessageDescriptor message, GuildMapper guildMapper, PrivateChannelMapper privateChannelMapper)
 {
     if (message.guild_id.HasValue)
     {
         MapGuildMessage(message, guildMapper);
     }
     else
     {
         MapPrivateMessage(message, privateChannelMapper);
     }
 }
Ejemplo n.º 3
0
        /// <summary>
        /// Create a new Mapper instance. This will add Mapper's serializers to the provided <see cref="Gateway"/>'s
        /// <see cref="GatewayEvents"/> instance.
        /// </summary>
        /// <param name="gateway"></param>
        public Mapper(Gateway gateway)
        {
            _events        = new Events(this);
            _guildMapper   = new GuildMapper();
            _messageMapper = new MessageMapper();

            gateway.Events.AddConverter(new ChannelDeserializer());
            gateway.Events.AddConverter(new UserDeserializer());
            gateway.Events.AddConverter(new MemberDeserializer());
            gateway.Events.AddConverter(new EmojiDeserializer());
            gateway.Events.AddConverter(new RoleDeserializer());
            gateway.Events.AddConverter(new PermissionDeserializer());
            gateway.Events.AddConverter(new MemberActivityDeserializer());
            gateway.Events.AddConverter(new MemberActivityTimestampDeserializer());
            gateway.Events.AddConverter(new MessageDeserializer());

            gateway.Events.AddEventCallback <GuildDescriptor>(Discord.Enums.EventType.GUILD_CREATE, _events.Guild_Create);
            gateway.Events.AddEventCallback <GuildDescriptor>(Discord.Enums.EventType.GUILD_UPDATE, _events.Guild_Update);
            gateway.Events.AddEventCallback <GuildDescriptor>(Discord.Enums.EventType.GUILD_DELETE, _events.Guild_Delete);

            gateway.Events.AddEventCallback <MessageDescriptor>(Discord.Enums.EventType.MESSAGE_CREATE, _events.Message_Create);
        }