public async Task DelCustReact(int id) { if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) { await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false); return; } var success = false; CustomReaction toDelete; using (var uow = DbHandler.UnitOfWork()) { toDelete = uow.CustomReactions.Get(id); if (toDelete == null) //not found { success = false; } else { if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null) { uow.CustomReactions.Remove(toDelete); //todo i can dramatically improve performance of this, if Ids are ordered. _globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray(); success = true; } else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && (long)Context.Guild.Id == toDelete.GuildId) { uow.CustomReactions.Remove(toDelete); GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) => { return(old.Where(cr => cr?.Id != toDelete.Id).ToArray()); }); success = true; } if (success) { await uow.CompleteAsync().ConfigureAwait(false); } } } if (success) { await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithTitle(GetText("deleted")) .WithDescription("#" + toDelete.Id) .AddField(efb => efb.WithName(GetText("trigger")).WithValue(toDelete.Trigger)) .AddField(efb => efb.WithName(GetText("response")).WithValue(toDelete.Response))); } else { await ReplyErrorLocalized("no_found_id").ConfigureAwait(false); } }
public async Task AddCustReact(string key, [Remainder] string message) { var channel = Context.Channel as ITextChannel; if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key)) { return; } key = key.ToLowerInvariant(); if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) { await ReplyErrorLocalized("insuff_perms").ConfigureAwait(false); return; } var cr = new CustomReaction() { GuildId = (long?)channel?.Guild.Id, IsRegex = false, Trigger = key, Response = message, }; using (var uow = DbHandler.UnitOfWork()) { uow.CustomReactions.Add(cr); await uow.CompleteAsync().ConfigureAwait(false); } if (channel == null) { Array.Resize(ref _globalReactions, _globalReactions.Length + 1); _globalReactions[_globalReactions.Length - 1] = cr; } else { GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { cr }, (k, old) => { Array.Resize(ref old, old.Length + 1); old[old.Length - 1] = cr; return(old); }); } await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithTitle(GetText("new_cust_react")) .WithDescription($"#{cr.Id}") .AddField(efb => efb.WithName(GetText("trigger")).WithValue(key)) .AddField(efb => efb.WithName(GetText("response")).WithValue(message)) ).ConfigureAwait(false); }
public async Task AddCustReact(string key, [Remainder] string message) { var channel = Context.Channel as ITextChannel; if (string.IsNullOrWhiteSpace(message) || string.IsNullOrWhiteSpace(key)) { return; } key = key.ToLowerInvariant(); if ((channel == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (channel != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) { try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } return; } var cr = new CustomReaction() { GuildId = channel?.Guild.Id, IsRegex = false, Trigger = key, Response = message, }; using (var uow = DbHandler.UnitOfWork()) { uow.CustomReactions.Add(cr); await uow.CompleteAsync().ConfigureAwait(false); } if (channel == null) { Array.Resize(ref _globalReactions, _globalReactions.Length + 1); _globalReactions[_globalReactions.Length - 1] = cr; } else { var reactions = GuildReactions.AddOrUpdate(Context.Guild.Id, Array.Empty <CustomReaction>(), (k, old) => { Array.Resize(ref old, old.Length + 1); old[old.Length - 1] = cr; return(old); }); } await Context.Channel.EmbedAsync(new EmbedBuilder().WithOkColor() .WithTitle("New Custom Reaction") .WithDescription($"#{cr.Id}") .AddField(efb => efb.WithName("Trigger").WithValue(key)) .AddField(efb => efb.WithName("Response").WithValue(message)) ).ConfigureAwait(false); }
private Task Bot_JoinedGuild(GuildConfig gc) { var _ = Task.Run(() => { using (var uow = _db.UnitOfWork) { var crs = uow.CustomReactions.For(gc.GuildId); GuildReactions.AddOrUpdate(gc.GuildId, crs, (key, old) => crs); } }); return(Task.CompletedTask); }
public async Task DelCustReact(int id) { if ((Context.Guild == null && !NadekoBot.Credentials.IsOwner(Context.User)) || (Context.Guild != null && !((IGuildUser)Context.User).GuildPermissions.Administrator)) { try { await Context.Channel.SendErrorAsync("Insufficient permissions. Requires Bot ownership for global custom reactions, and Administrator for guild custom reactions."); } catch { } return; } var success = false; CustomReaction toDelete; using (var uow = DbHandler.UnitOfWork()) { toDelete = uow.CustomReactions.Get(id); if (toDelete == null) //not found { return; } if ((toDelete.GuildId == null || toDelete.GuildId == 0) && Context.Guild == null) { uow.CustomReactions.Remove(toDelete); //todo i can dramatically improve performance of this, if Ids are ordered. _globalReactions = GlobalReactions.Where(cr => cr?.Id != toDelete.Id).ToArray(); success = true; } else if ((toDelete.GuildId != null && toDelete.GuildId != 0) && Context.Guild.Id == toDelete.GuildId) { uow.CustomReactions.Remove(toDelete); GuildReactions.AddOrUpdate(Context.Guild.Id, new CustomReaction[] { }, (key, old) => { return(old.Where(cr => cr?.Id != toDelete.Id).ToArray()); }); success = true; } if (success) { await uow.CompleteAsync().ConfigureAwait(false); } } if (success) { await Context.Channel.SendConfirmAsync("Deleted custom reaction", toDelete.ToString()).ConfigureAwait(false); } else { await Context.Channel.SendErrorAsync("Failed to find that custom reaction.").ConfigureAwait(false); } }