/// <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); }
/// <summary> /// Run role-set 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; if (parameters.Length > 1 || string.IsNullOrWhiteSpace(parameters[0]) || string.IsNullOrWhiteSpace(parameters[1]) || ulong.TryParse(parameters[0], out guildId) || guildId == 0) { // Not enough parameters return(false); } // reconstruct botched JSON :D string json = string.Join('/', parameters.Skip(1).ToArray()); Wrappers.Role role = JSON.JsonConvert.DeserializeObject <Wrappers.Role>(json); DW.SocketGuild foundGuild = user.MutualGuilds.FirstOrDefault(guild => guild.Id == guildId); if (role != null && foundGuild != null) { DW.SocketGuildUser foundUser = foundGuild.GetUser(user.Id); if (foundUser != null && foundUser.Roles.Any(userRole => userRole.Permissions.Administrator)) { role.Save(foundGuild); return(true); } } return(false); }
/// <summary> /// Broadcast guild change to all relevan users /// </summary> /// <param name="guild">Discord guild</param> /// <param name="eventName">Event name</param> /// <returns>Completed task</returns> public static Task BroadcastGuildChange(DW.SocketGuild guild, string eventName) { if (eventName == "guildLeft") { DatabaseAccess.Command("DELETE * FROM `RoleFolders` WHERE `Guild`=`" + guild.Id + "`"); } IEnumerable <Tuple <WebSocket, bool> > targets = guild.Users.ToList().Select(user => { foreach (KeyValuePair <WebSocket, DW.SocketUser> socket in CommandHandler.sockets) { if (socket.Value.Id == user.Id) { return(new Tuple <WebSocket, bool>(socket.Key, user.Roles.FirstOrDefault(role => role.Permissions.Administrator) != null)); } } return(null); }).Where(socket => socket != null); foreach (Tuple <WebSocket, bool> socket in targets) { try { socket.Item1.Send(eventName, new Wrappers.Guild(guild, socket.Item2)); } catch (Exception ex) { ex.ToString(); } } return(Task.CompletedTask); }
internal void Update(DiscordSocketClient client, ClientState state, WebSocket.SocketGuild guild, IMessageChannel channel, InteractionCreateModel model) { Type = model.Type; Id = model.Id; if (model.Message.IsSpecified) { MessageId = model.Message.Value.Id; } ChannelId = channel.Id; Channel = channel; if (guild != null) { Guild = guild; Member = guild.GetUser(model.Member.Value.User.Id) ?? guild.AddOrUpdateUser(model.Member.Value.User); User = Member as SocketUser; } else { User = SocketGlobalUser.Create(client, state, model.User.Value); } Data = new InteractionData { Id = model.Id, Name = model.Data.Name.IsSpecified ? model.Data.Name.Value : null, Token = model.Token, Choices = model.Data.Options == null ? new InteractionChoice[0] : model.Data.Options.Select(x => new InteractionChoice { Name = x.Name, Value = x.Value, Choices = GetChoices(x) }).ToArray(), ComponentType = model.Data.ComponentType, CustomId = model.Data.CustomId.IsSpecified ? model.Data.CustomId.Value : null }; }
/// <summary> /// Run role-set-path 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); } // recostruct path string path = "/" + string.Join('/', parameters.Skip(2).Select(folder => Regex.Replace(folder, "^[0-9A-Za-z ]+$", string.Empty).Trim()).Where(folder => !string.IsNullOrWhiteSpace(folder))); 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) && foundUser.Roles.FirstOrDefault(role => role.Id == roleId) != null) { Wrappers.Role.UpdateDatabase(roleId, path, guildId); return(true); } } return(false); }
/// <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); }
/// <summary> /// Initializes a new instance of the <see cref="Guild" /> class. /// </summary> /// <param name="guild">Discord guild</param> /// <param name="admin">Is user administrator for this guild?</param> public Guild(DW.SocketGuild guild, bool admin) { this.Identifier = guild.Id.ToString(); this.Icon = guild.IconUrl; this.IconIdentifier = guild.IconId; this.MemberCount = guild.MemberCount; this.Name = guild.Name; this.IsAdministrator = admin; }
internal static ActionResult RemoveGuild(Discord.WebSocket.SocketGuild guild) { var result = new ActionResult(); _guilds.Remove(_guilds.Find(g => g.Id == guild.Id)); DataStorage.DeleteFolder(GuildsFolder + "/" + guild.Id + ".json"); Utilities.Log("GuildsData.RemoveGuild", guild.Name + "Removed from GuildDataSystem."); return(result); }
public static GuildConfig FindOrCreateGuildConfig(Discord.WebSocket.SocketGuild guild) { GuildConfig GuildConfig = _guilds.Find(g => g.Id == guild.Id); if (GuildConfig.Equals(null)) { InitializeGuild(guild); GuildConfig = _guilds.Find(g => g.Id == guild.Id); } Utilities.Log("FindOrCreateGuildConfig", $"Guild found with id {guild.Id} [{GuildConfig.Popularity}]", Discord.LogSeverity.Debug); return(GuildConfig); }
internal static ActionResult InitializeGuild(Discord.WebSocket.SocketGuild guild) { var result = new ActionResult(); var guildconfig = new GuildConfig() { Prefix = "!", Id = guild.Id, TimeCreated = DateTime.Now.ToString() }; _guilds.Add(guildconfig); DataStorage.StoreObject(guildconfig, $"{GuildsFolder}/{guildconfig.Id}/config.json", _guildGlobalSettings.UseNiceFormatting); Utilities.Log("GuildsData.InitializeGuild", guild.Name + "Initialized in GuildDataSystem."); return(result); }
/// <summary> /// Run roles-list 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])) { // Not enough parameters return(false); } ulong guildId = 0; if (!ulong.TryParse(parameters[0], out guildId)) { // 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); } ulong userId = 0; if (parameters.Length > 1 && !string.IsNullOrWhiteSpace(parameters[1]) && ulong.TryParse(parameters[1], out userId)) { // List roles for specific user socket.Send("rolesList", guild.GetUser(userId).Roles.Select(role => new Wrappers.Role(role))); return(true); } // List all roles in guild socket.Send("rolesList", guild.Roles.Select(role => new Wrappers.Role(role))); return(true); }
/// <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); }
/// <inheritdoc /> public ISocketGuildUser GetUser(ulong id) => SocketGuild.GetUser(id) ?.Abstract();
/// <summary> /// Converts an existing <see cref="SocketGuild"/> to an abstracted <see cref="ISocketGuild"/> value. /// </summary> /// <param name="socketGuild">The existing <see cref="SocketGuild"/> to be abstracted.</param> /// <exception cref="ArgumentNullException">Throws for <paramref name="socketGuild"/>.</exception> /// <returns>An <see cref="ISocketGuild"/> that abstracts <paramref name="socketGuild"/>.</returns> public static ISocketGuild Abstract(this SocketGuild socketGuild) => new SocketGuildAbstraction(socketGuild);
/// <inheritdoc /> public async Task <IGuildEmote> GetEmoteAsync(ulong id, RequestOptions options = null) => (await SocketGuild.GetEmoteAsync(id, options)) ?.Abstract();
/// <inheritdoc /> public ISocketRole GetRole(ulong id) => SocketGuild.GetRole(id) ?.Abstract();
/// <inheritdoc cref="SocketGuild.ToString" /> public override string ToString() => SocketGuild.ToString();
/// <inheritdoc /> public async Task <IBan> GetBanAsync(IUser user, RequestOptions options = null) => await SocketGuild.GetBanAsync(user, options);
/// <inheritdoc /> public async Task <IRestInviteMetadata> GetVanityInviteAsync(RequestOptions options = null) => RestInviteMetadataAbstractionExtensions.Abstract( await SocketGuild.GetVanityInviteAsync(options));
/// <inheritdoc /> public ISocketVoiceChannel GetVoiceChannel(ulong id) => SocketGuild.GetVoiceChannel(id) ?.Abstract();
/// <inheritdoc /> public async Task <IGuildEmote> ModifyEmoteAsync(IGuildEmote emote, Action <EmoteProperties> func, RequestOptions options = null) => (await SocketGuild.ModifyEmoteAsync(emote.Unabstract(), func, options)) .Abstract();
/// <inheritdoc /> public Task ModifyEmbedAsync(Action <GuildEmbedProperties> func, RequestOptions options = null) => SocketGuild.ModifyEmbedAsync(func, options);
/// <inheritdoc /> public Task LeaveAsync(RequestOptions options = null) => SocketGuild.LeaveAsync(options);
/// <inheritdoc /> public async Task <IReadOnlyCollection <IRestWebhook> > GetWebhooksAsync(RequestOptions options = null) => (await SocketGuild.GetWebhooksAsync(options)) .Select(RestWebhookAbstractionExtensions.Abstract) .ToArray();
/// <inheritdoc /> public async Task <IReadOnlyCollection <IBan> > GetBansAsync(RequestOptions options = null) => await SocketGuild.GetBansAsync(options);
/// <inheritdoc /> public Task RemoveBanAsync(ulong userId, RequestOptions options = null) => SocketGuild.RemoveBanAsync(userId, options);
/// <inheritdoc /> public Task RemoveBanAsync(IUser user, RequestOptions options = null) => SocketGuild.RemoveBanAsync(user, options);
/// <inheritdoc /> public Task ReorderRolesAsync(IEnumerable <ReorderRoleProperties> args, RequestOptions options = null) => SocketGuild.ReorderRolesAsync(args, options);
/// <inheritdoc /> public Task <int> PruneUsersAsync(int days = 30, bool simulate = false, RequestOptions options = null) => SocketGuild.PruneUsersAsync(days, simulate, options);
/// <inheritdoc /> public async Task <IReadOnlyCollection <IRestInviteMetadata> > GetInvitesAsync(RequestOptions options = null) => (await SocketGuild.GetInvitesAsync(options)) .Select(RestInviteMetadataAbstractionExtensions.Abstract) .ToArray();