Пример #1
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();
            }
        }
Пример #2
0
        /// <summary>
        /// Gets a list of members in a guild.
        /// This method is paged, and cannot always return every member at once.
        /// </summary>
        /// <param name="guildId">The ID of the guild.</param>
        /// <param name="limit">Max number of members to return (1-1000).</param>
        /// <param name="after">The highest user ID in the previous page.</param>
        /// <exception cref="DiscordHttpApiException"></exception>
        public async Task <IReadOnlyList <DiscordGuildMember> > ListGuildMembers(Snowflake guildId,
                                                                                 int?limit = null, Snowflake?after = null)
        {
            UrlParametersBuilder urlParams = new UrlParametersBuilder();

            urlParams["limit"] = limit?.ToString() ?? null;
            urlParams["after"] = after?.Id.ToString() ?? null;

            DiscordApiData data = await rest.Get($"guilds/{guildId}/members{urlParams.ToQueryString()}",
                                                 $"guilds/{guildId}/members").ConfigureAwait(false);

            DiscordGuildMember[] members = new DiscordGuildMember[data.Values.Count];
            for (int i = 0; i < members.Length; i++)
            {
                members[i] = new DiscordGuildMember(this, data.Values[i], guildId);
            }

            return(members);
        }
Пример #3
0
        void HandleGuildMembersChunkEvent(DiscordApiData data)
        {
            Snowflake guildId = data.GetSnowflake("guild_id").Value;

            // Get every member and ensure they are cached
            IList <DiscordApiData> membersData = data.GetArray("members");

            DiscordGuildMember[] members = new DiscordGuildMember[membersData.Count];
            for (int i = 0; i < membersData.Count; i++)
            {
                DiscordApiData memberData = membersData[i];

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

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

                mutableUser.Update(userData);

                // Get or create member
                MutableGuildMember mutableMember;
                if (!cache.GuildMembers.TryGetValue(guildId, userId, out mutableMember))
                {
                    mutableMember = new MutableGuildMember(mutableUser, guildId, http);
                    mutableMember.Update(memberData);

                    cache.GuildMembers[guildId, userId] = mutableMember;
                }

                members[i] = mutableMember.ImmutableEntity;
            }

            // Fire event
            OnGuildMembersChunk?.Invoke(this, new GuildMemberChunkEventArgs(shard, guildId, members));
        }
Пример #4
0
        /// <summary>
        /// Using the cache, this will attempt to verify that the current bot can
        /// join the selected voice channel. If the cache does not have the required details,
        /// this will NOT throw an exception (the handshake timeout will handle invalid permission anyway).
        /// </summary>
        /// <exception cref="DiscordPermissionException"></exception>
        /// <exception cref="InvalidOperationException"></exception>
        void AssertUserCanJoin(Snowflake guildId, Snowflake voiceChannelId)
        {
            DiscordGuildMember       member       = cache.GetGuildMember(guildId, Shard.UserId.Value);
            DiscordGuild             guild        = cache.GetGuild(guildId);
            DiscordGuildVoiceChannel voiceChannel = cache.GetGuildVoiceChannel(voiceChannelId);

            if (member != null && guild != null && voiceChannel != null)
            {
                // Check if the user has permission to connect.
                DiscordPermissionHelper.AssertPermission(DiscordPermission.Connect, member, guild, voiceChannel);

                // Check if the voice channel has room
                bool channelHasRoom = false;
                if (voiceChannel.UserLimit == 0)
                {
                    channelHasRoom = true;
                }
                else if (DiscordPermissionHelper.HasPermission(DiscordPermission.Administrator, member, guild, voiceChannel))
                {
                    channelHasRoom = true;
                }
                else
                {
                    IReadOnlyList <Snowflake> usersInChannel = Shard.Voice.GetUsersInVoiceChannel(voiceChannelId);
                    if (usersInChannel.Count < voiceChannel.UserLimit)
                    {
                        channelHasRoom = true;
                    }
                }

                if (!channelHasRoom)
                {
                    throw new InvalidOperationException("The voice channel is full, and the current bot " +
                                                        "does not have the administrator permission.");
                }
            }
        }
Пример #5
0
 internal PresenceEventArgs(Shard shard, Snowflake guildId, DiscordGuildMember member, DiscordUserPresence presence)
     : base(shard, guildId, member)
 {
     Presence = presence;
 }
Пример #6
0
 internal GuildMemberEventArgs(Shard shard, Snowflake guildId, DiscordGuildMember member)
     : base(shard)
 {
     GuildId = guildId;
     Member  = member;
 }