Example #1
0
        /// <summary>
        /// Broadcast role change to all relevant users
        /// </summary>
        /// <param name="role">Discord role</param>
        /// <param name="eventName">Event name</param>
        /// <returns>Completed task</returns>
        public static Task BroadcastRoleChange(DW.SocketRole role, string eventName)
        {
            IEnumerable <WebSocket> targets = role.Guild.Users.ToList().Select(user =>
            {
                foreach (KeyValuePair <WebSocket, DW.SocketUser> socket in CommandHandler.sockets)
                {
                    if (socket.Value.Id == user.Id)
                    {
                        return(socket.Key);
                    }
                }

                return(null);
            }).Where(socket => socket != null);

            foreach (WebSocket socket in targets)
            {
                try
                {
                    socket.Send(eventName, new Wrappers.Role(role));
                }
                catch (Exception ex)
                {
                    ex.ToString();
                }
            }

            return(Task.CompletedTask);
        }
Example #2
0
        /// <summary>
        /// Run roles command
        /// </summary>
        /// <param name="socket">Web socket</param>
        /// <param name="user">Discord user who invoked command</param>
        /// <param name="parameters">Command parameters</param>
        /// <returns>True on success</returns>
        public bool Run(WebSocket socket, DW.SocketUser user, string[] parameters)
        {
            if (parameters.Length == 0 || string.IsNullOrWhiteSpace(parameters[0]) || string.IsNullOrWhiteSpace(parameters[1]))
            {
                // Not enough parameters
                return(false);
            }

            ulong roleId  = 0;
            ulong guildId = 0;

            if (!ulong.TryParse(parameters[0], out guildId) || !ulong.TryParse(parameters[1], out roleId))
            {
                // NO guild specified
                return(false);
            }

            DW.SocketGuild guild = user.MutualGuilds.FirstOrDefault(mutualGuild => mutualGuild.Id == guildId);

            if (guild == null)
            {
                // User is not in this guild
                return(false);
            }

            DW.SocketRole foundRole = guild.GetUser(user.Id).Roles.FirstOrDefault(role => role.Id == roleId);

            if (foundRole != null)
            {
                socket.Send("role", new Wrappers.Role(foundRole));
                return(true);
            }

            return(false);
        }
Example #3
0
        /// <summary>
        /// Save role settings to DB
        /// </summary>
        /// <param name="guild">Discord guild containing role</param>
        /// <returns>True on success modify/insert</returns>
        public bool Save(DW.SocketGuild guild = null)
        {
            if (!string.IsNullOrWhiteSpace(this.Path))
            {
                this.Path = "/" + string.Join(
                    '/',
                    this.Path.Trim().Split(new char[] { '/' }, StringSplitOptions.RemoveEmptyEntries)
                    .Select(folder => Regex.Replace(folder, "^[0-9A-Za-z ]+$", string.Empty).Trim())
                    .Where(folder => !string.IsNullOrWhiteSpace(folder)));

                if (string.IsNullOrWhiteSpace(this.Path) || !this.Path.StartsWith('/'))
                {
                    this.Path = "/";
                }
            }

            Role.UpdateDatabase(ulong.Parse(this.Identifier), this.Path, ulong.Parse(this.GuildIdentifier));

            if (guild != null)
            {
                if (this.Identifier == "0" || string.IsNullOrWhiteSpace(this.Identifier))
                {
                    // Create role
                    DNET.Color?color = null;

                    if (this.Color != null && this.Color.Count == 3)
                    {
                        color = new DNET.Color(this.Color[0], this.Color[1], this.Color[2]);
                    }

                    Task.Run(() => guild.CreateRoleAsync(this.Name, this.Permissions, color));
                    return(true);
                }
                else
                {
                    // Modify role
                    DW.SocketRole role = guild.GetRole(ulong.Parse(this.Identifier));

                    if (role != null)
                    {
                        Task.Run(() => role.ModifyAsync(changed =>
                        {
                            changed.Name        = this.Name;
                            changed.Permissions = this.Permissions;

                            if (this.Color != null && this.Color.Count == 3)
                            {
                                changed.Color = new DNET.Optional <DNET.Color>(new DNET.Color(this.Color[0], this.Color[1], this.Color[2]));
                            }
                        }));

                        return(true);
                    }
                }
            }

            return(false);
        }
Example #4
0
        /// <summary>
        /// Run role-remove command
        /// </summary>
        /// <param name="socket">Web socket</param>
        /// <param name="user">Discord user who invoked command</param>
        /// <param name="parameters">Command parameters</param>
        /// <returns>True on success</returns>
        public bool Run(WebSocket socket, DW.SocketUser user, string[] parameters)
        {
            ulong guildId = 0;
            ulong roleId  = 0;

            if (parameters.Length > 1 ||
                string.IsNullOrWhiteSpace(parameters[0]) ||
                string.IsNullOrWhiteSpace(parameters[1]) ||
                ulong.TryParse(parameters[0], out guildId) ||
                ulong.TryParse(parameters[1], out roleId) ||
                guildId == 0 ||
                roleId == 0)
            {
                // Not enough parameters
                return(false);
            }

            DW.SocketGuild foundGuild = user.MutualGuilds.FirstOrDefault(guild => guild.Id == guildId);

            if (foundGuild != null)
            {
                DW.SocketGuildUser foundUser = foundGuild.GetUser(user.Id);

                if (foundUser != null && foundUser.Roles.Any(userRole => userRole.Permissions.Administrator))
                {
                    DW.SocketRole role = foundGuild.GetRole(roleId);

                    if (role != null)
                    {
                        Task.Run(() => role.DeleteAsync());
                        return(true);
                    }
                }
            }

            return(false);
        }
Example #5
0
        /// <summary>
        /// Initializes a new instance of the <see cref="Role" /> class.
        /// </summary>
        /// <param name="role">Discord role</param>
        public Role(DW.SocketRole role)
        {
            this.GuildIdentifier = role.Guild.Id.ToString();
            this.Identifier      = role.Id.ToString();
            this.Name            = role.Name;
            this.Color           = new List <int> {
                role.Color.R, role.Color.G, role.Color.B
            };
            this.Permissions = role.Permissions;

            SqlDataReader reader = DatabaseAccess.Read("SELECT * FROM RoleFolders WHERE Id=" + this.Identifier.ToString());

            if (reader == null)
            {
                this.Path = "/";
            }
            else
            {
                reader.NextResult();
                string fromDb = reader.RecordToString(1);

                this.Path = string.IsNullOrWhiteSpace(fromDb) ? "/" : fromDb;
            }
        }
Example #6
0
 /// <summary>
 /// Constructs a new <see cref="SocketRoleAbstraction"/> around an existing <see cref="WebSocket.SocketRole"/>.
 /// </summary>
 /// <param name="socketRole">The value to use for <see cref="WebSocket.SocketRole"/>.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="socketRole"/>.</exception>
 public SocketRoleAbstraction(SocketRole socketRole)
 {
     SocketRole = socketRole ?? throw new ArgumentNullException(nameof(socketRole));
 }
Example #7
0
 /// <summary>
 /// Converts an existing <see cref="SocketRole"/> to an abstracted <see cref="ISocketRole"/> value.
 /// </summary>
 /// <param name="socketRole">The existing <see cref="SocketRole"/> to be abstracted.</param>
 /// <exception cref="ArgumentNullException">Throws for <paramref name="socketRole"/>.</exception>
 /// <returns>An <see cref="ISocketRole"/> that abstracts <paramref name="socketRole"/>.</returns>
 public static ISocketRole Abstract(this SocketRole socketRole)
 => new SocketRoleAbstraction(socketRole);
Example #8
0
 /// <inheritdoc />
 public Task ModifyAsync(Action <RoleProperties> func, RequestOptions options = null)
 => SocketRole.ModifyAsync(func, options);
Example #9
0
 /// <inheritdoc />
 public Task DeleteAsync(RequestOptions options = null)
 => SocketRole.DeleteAsync(options);
Example #10
0
 /// <inheritdoc />
 public int CompareTo(IRole other)
 => SocketRole.CompareTo(other);