Ejemplo n.º 1
0
        void HandleChannelUpdateEvent(DiscordApiData data)
        {
            Snowflake          id      = data.GetSnowflake("id").Value;
            DiscordChannelType type    = (DiscordChannelType)data.GetInteger("type");
            Snowflake          guildId = data.GetSnowflake("guild_id").Value;

            DiscordGuildChannel channel = null;

            if (type == DiscordChannelType.GuildText)
            {
                channel = new DiscordGuildTextChannel(http, data, guildId);
            }
            else if (type == DiscordChannelType.GuildVoice)
            {
                channel = new DiscordGuildVoiceChannel(http, data, guildId);
            }
            else if (type == DiscordChannelType.GuildCategory)
            {
                channel = new DiscordGuildCategoryChannel(http, data, guildId);
            }

            if (channel != null)
            {
                cache.GuildChannels[id] = channel;

                OnGuildChannelUpdated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
            else
            {
                log.LogWarning($"Failed to update channel {id} because the type ({type}) doesn't have an implementation!");
            }
        }
Ejemplo n.º 2
0
        private static void ProcessMessage(object sender, MessageEventArgs e)
        {
            if (e.Shard.User.Id == e.Message.Author.Id)
            {
                return;
            }

            ITextChannel            Channel          = (ITextChannel)e.Shard.Cache.Channels.Get(e.Message.ChannelId);
            DiscordMessage          message          = e.Message;
            DiscoreCache            cache            = e.Shard.Cache;
            DiscordGuildTextChannel guildTextChannel = cache.Channels.Get(message.ChannelId) as DiscordGuildTextChannel;
            DiscoreGuildCache       guildCache       = cache.Guilds.Get(guildTextChannel.GuildId);
            DiscordGuildMember      member           = guildCache.Members.Get(e.Message.Author.Id).Value;

            if (Channel.Id == Snowflake.Parse(Settings.Sunbae.ChannelID))
            {
                String[] bannedRoles = new String[] { "Encoders", "Uploaders", "Sunbae", "admins" };

                foreach (DiscordRole role in guildCache.Roles.Values)
                {
                    if (bannedRoles.Contains(role.Name))
                    {
                        continue;
                    }

                    if (role.Name.ToLower() == e.Message.Content.ToLower())
                    {
                        member.AddRole(role.Id);
                        break;
                    }
                }
                e.Message.Delete();
            }
        }
Ejemplo n.º 3
0
        void HandleChannelDeleteEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordDMChannel dm;
                if (cache.DMChannels.TryRemove(id, out MutableDMChannel mutableDM))
                {
                    mutableDM.ClearReferences();

                    dm = mutableDM.ImmutableEntity;
                }
                else
                {
                    dm = new DiscordDMChannel(http, data);
                }

                OnDMChannelRemoved?.Invoke(this, new DMChannelEventArgs(shard, dm));
            }
            else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice ||
                     type == DiscordChannelType.GuildCategory)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                if (type == DiscordChannelType.GuildText)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildTextChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildVoice)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildVoiceChannel(http, data, guildId);
                    }
                }
                else if (type == DiscordChannelType.GuildCategory)
                {
                    if (!cache.GuildChannels.TryRemove(id, out channel))
                    {
                        channel = new DiscordGuildCategoryChannel(http, data, guildId);
                    }
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                OnGuildChannelRemoved?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Ejemplo n.º 4
0
        void HandleChannelCreateEvent(DiscordApiData data)
        {
            Snowflake          id   = data.GetSnowflake("id").Value;
            DiscordChannelType type = (DiscordChannelType)data.GetInteger("type").Value;

            if (type == DiscordChannelType.DirectMessage)
            {
                // DM channel
                DiscordApiData recipientData = data.GetArray("recipients").First();
                Snowflake      recipientId   = recipientData.GetSnowflake("id").Value;

                MutableUser recipient;
                if (!cache.Users.TryGetValue(recipientId, out recipient))
                {
                    recipient = new MutableUser(recipientId, false, http);
                    cache.Users[recipientId] = recipient;
                }

                recipient.Update(recipientData);

                MutableDMChannel mutableDMChannel;
                if (!cache.DMChannels.TryGetValue(id, out mutableDMChannel))
                {
                    mutableDMChannel     = new MutableDMChannel(id, recipient, http);
                    cache.DMChannels[id] = mutableDMChannel;
                }

                OnDMChannelCreated?.Invoke(this, new DMChannelEventArgs(shard, mutableDMChannel.ImmutableEntity));
            }
            else if (type == DiscordChannelType.GuildText || type == DiscordChannelType.GuildVoice)
            {
                // Guild channel
                Snowflake guildId = data.GetSnowflake("guild_id").Value;

                DiscordGuildChannel channel;

                if (type == DiscordChannelType.GuildText)
                {
                    channel = new DiscordGuildTextChannel(http, data, guildId);
                }
                else if (type == DiscordChannelType.GuildVoice)
                {
                    channel = new DiscordGuildVoiceChannel(http, data, guildId);
                }
                else if (type == DiscordChannelType.GuildCategory)
                {
                    channel = new DiscordGuildCategoryChannel(http, data, guildId);
                }
                else
                {
                    throw new NotImplementedException($"Guild channel type \"{type}\" has no implementation!");
                }

                cache.GuildChannels[id] = channel;

                OnGuildChannelCreated?.Invoke(this, new GuildChannelEventArgs(shard, guildId, channel));
            }
        }
Ejemplo n.º 5
0
        void HandleGuildCreateEvent(DiscordApiData data)
        {
            Snowflake guildId = data.GetSnowflake("id").Value;

            bool wasUnavailable = !cache.IsGuildAvailable(guildId);

            // Update guild
            MutableGuild mutableGuild;

            if (!cache.Guilds.TryGetValue(guildId, out mutableGuild))
            {
                mutableGuild          = new MutableGuild(guildId, http);
                cache.Guilds[guildId] = mutableGuild;
            }

            mutableGuild.Update(data);

            // Ensure the cache guildId list contains this guild (it uses a hashset so don't worry about duplicates).
            cache.AddGuildId(guildId);

            // GUILD_CREATE specifics
            // Update metadata
            cache.GuildMetadata[guildId] = new DiscordGuildMetadata(data);

            // Deserialize members
            cache.GuildMembers.Clear(guildId);
            IList <DiscordApiData> membersArray = data.GetArray("members");

            for (int i = 0; i < membersArray.Count; i++)
            {
                DiscordApiData memberData = membersArray[i];

                DiscordApiData userData = memberData.Get("user");
                Snowflake      userId   = userData.GetSnowflake("id").Value;

                MutableUser user;
                if (!cache.Users.TryGetValue(userId, out user))
                {
                    user = new MutableUser(userId, false, http);
                    cache.Users[userId] = user;
                }

                user.Update(userData);

                MutableGuildMember member;
                if (!cache.GuildMembers.TryGetValue(guildId, userId, out member))
                {
                    member = new MutableGuildMember(user, guildId, http);
                    cache.GuildMembers[guildId, userId] = member;
                }

                member.Update(memberData);
            }

            // Deserialize channels
            cache.ClearGuildChannels(guildId);
            IList <DiscordApiData> channelsArray = data.GetArray("channels");

            for (int i = 0; i < channelsArray.Count; i++)
            {
                DiscordApiData     channelData = channelsArray[i];
                DiscordChannelType channelType = (DiscordChannelType)channelData.GetInteger("type");

                DiscordGuildChannel channel = null;
                if (channelType == DiscordChannelType.GuildText)
                {
                    channel = new DiscordGuildTextChannel(http, channelData, guildId);
                }
                else if (channelType == DiscordChannelType.GuildVoice)
                {
                    channel = new DiscordGuildVoiceChannel(http, channelData, guildId);
                }
                else if (channelType == DiscordChannelType.GuildCategory)
                {
                    channel = new DiscordGuildCategoryChannel(http, channelData, guildId);
                }

                if (channel != null)
                {
                    cache.AddGuildChannel(channel);
                }
            }

            // Deserialize voice states
            cache.GuildVoiceStates.Clear(guildId);
            IList <DiscordApiData> voiceStatesArray = data.GetArray("voice_states");

            for (int i = 0; i < voiceStatesArray.Count; i++)
            {
                DiscordVoiceState state = new DiscordVoiceState(guildId, voiceStatesArray[i]);
                UpdateMemberVoiceState(state);
            }

            // Deserialize presences
            cache.GuildPresences.Clear(guildId);
            IList <DiscordApiData> presencesArray = data.GetArray("presences");

            for (int i = 0; i < presencesArray.Count; i++)
            {
                // Presence's in GUILD_CREATE do not contain full user objects,
                // so don't attempt to update them here.

                DiscordApiData presenceData = presencesArray[i];
                Snowflake      userId       = presenceData.LocateSnowflake("user.id").Value;

                cache.GuildPresences[guildId, userId] = new DiscordUserPresence(userId, presenceData);
            }

            // Mark the guild as available
            cache.SetGuildAvailability(guildId, true);

            // Fire event
            if (wasUnavailable)
            {
                OnGuildAvailable?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity));
            }
            else
            {
                OnGuildCreated?.Invoke(this, new GuildEventArgs(shard, mutableGuild.ImmutableEntity));
            }
        }
Ejemplo n.º 6
0
        private async void ExecuteCommand(KeyValuePair <string, Tuple <BaseModule, ILog, MethodInfo> > Command, MessageEventArgs e, string @params)
        {
            try {
                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("ExecutingCommand", Resources.Culture), Command.Key, string.IsNullOrEmpty(@params) ? Resources.ResourceManager.GetString("NoParams", Resources.Culture) : @params));
                Stopwatch st = new Stopwatch();
                st.Start();
                MethodInfo       method     = Command.Value.Item3;
                CommandAttribute attr       = method.GetCustomAttribute <CommandAttribute>();
                List <object>    parameters = new List <object>();
                foreach (ParameterInfo info in method.GetParameters())
                {
                    Type t = info.ParameterType;
                    if (parameters.Exists(p => p.GetType().Equals(t)))
                    {
                        Command.Value.Item2.Error(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingDuplicateParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        return;
                    }
                    DiscordVoiceConnection connection = null;
                    if (attr.NeedsVoice)
                    {
                        DiscordGuildTextChannel channel = shard.Cache.GetGuildTextChannel(e.Message.ChannelId);
                        if (channel == null)
                        {
                            channel = await client.GetChannel <DiscordGuildTextChannel>(e.Message.ChannelId).ConfigureAwait(true);

                            log.Debug(Resources.ResourceManager.GetString("HadToLoadGuild", Resources.Culture));
                        }
                        DiscordVoiceState voiceState = shard.Cache.GetVoiceState(channel.GuildId, e.Message.Author.Id);
                        if (voiceState.ChannelId.HasValue)
                        {
                            connection = shard.Voice.CreateOrGetConnection(channel.GuildId);
                            if (connection.IsValid && !connection.IsConnected && !connection.IsConnecting)
                            {
                                await connection.ConnectAsync(voiceState.ChannelId.Value, startDeaf : true).ConfigureAwait(true);

                                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("ConnectedToVoiceChannel", Resources.Culture), voiceState.ChannelId.Value, Guilds.FirstOrDefault(g => g.Id == channel.GuildId)));
                            }
                        }
                        else
                        {
                            return;
                        }
                    }
                    switch (t.Name)
                    {
                    case "ICoreHandler": {
                        if (!attr.NeedsHandler)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        parameters.Add(this);
                        break;
                    }

                    case "Message": {
                        if (!attr.NeedsMessage)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        Message message = new Message(e.Message.Id, e.Message.ChannelId.Id, e.Message.Author.Id);
                        if (attr.NeedsVoice)
                        {
                            message.HasSound += Message_HasSound;
                        }
                        parameters.Add(message);
                        break;
                    }

                    case "String": {
                        if (!attr.HasParams)
                        {
                            Command.Value.Item2.Warn(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnflaggedParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        }
                        parameters.Add(@params);
                        break;
                    }

                    default: {
                        Command.Value.Item2.Error(string.Format(Resources.Culture, Resources.ResourceManager.GetString("UsingUnknownParam", Resources.Culture), info.Name, info.ParameterType.Name));
                        return;
                    }
                    }
                }
                await Task.Run(() => {
                    try {
                        if (Command.Value.Item3.IsStatic)
                        {
                            Command.Value.Item3.Invoke(null, parameters.ToArray());
                        }
                        else
                        {
                            Command.Value.Item3.Invoke(Command.Value.Item1, parameters.ToArray());
                        }
                    } catch (InvalidOperationException ex) {
                        Command.Value.Item2.Error(ex.InnerException.ToString());
                    }
                }).ConfigureAwait(true);

                st.Stop();
                log.Debug(string.Format(Resources.Culture, Resources.ResourceManager.GetString("RanCommand", Resources.Culture), Command.Key, st.Elapsed.ToString("s\\.f", Resources.Culture)));
            } catch (Exception ex) {
                log.Error(ex);
                throw;
            }
        }