Esempio n. 1
0
        /// <summary>
        /// Removes a binding of a specific badword.
        /// </summary>
        /// <param name="global">global == true means across the entire guild, else restricted to the channel</param>
        /// <param name="context">context of the command</param>
        /// <param name="badWord">word to be unbanned</param>
        /// <returns></returns>
        public async Task <BindingStatus> RemoveBinding(bool global, ICommandContext context, string badWord)
        {
            using (var db = new BadWordContext(dbOptions))
            {
                if (global)
                {
                    var badWordDbEntry = new ServerBadWord
                    {
                        Entry    = badWord,
                        ServerId = context.Guild.Id,
                    };

                    if (!await db.BadWordServerBindings.AnyAsync(b => b.ServerId == context.Guild.Id && b.BadWords.Any(e => e == badWordDbEntry)))
                    {
                        return(BindingStatus.NotExisting);
                    }

                    foreach (var server in db.BadWordServerBindings.Include(b => b.BadWords).Where(b => b.ServerId == context.Guild.Id))
                    {
                        db.BadWordServerBindings.Update(server);
                        server.BadWords.Remove(badWordDbEntry);
                    }

                    await db.SaveChangesAsync();

                    return(BindingStatus.Removed);
                }
                else
                {
                    var badWordDbEntry = new ChannelBadWord
                    {
                        Entry     = badWord,
                        ServerId  = context.Guild.Id,
                        ChannelId = context.Channel.Id,
                    };

                    if (!await db.BadWordChannelBindings.AnyAsync(b => b.ChannelId == context.Guild.Id && b.BadWords.Any(e => e == badWordDbEntry)))
                    {
                        return(BindingStatus.NotExisting);
                    }

                    foreach (var channel in db.BadWordChannelBindings.Include(b => b.BadWords).Where(b => b.ChannelId == context.Channel.Id))
                    {
                        db.BadWordChannelBindings.Update(channel);
                        channel.BadWords.Remove(badWordDbEntry);
                    }

                    await db.SaveChangesAsync();

                    return(BindingStatus.Removed);
                }
            }
        }
Esempio n. 2
0
        /// <summary>
        /// Adds a binding of a specific badword.
        /// </summary>
        /// <param name="global">global == true means across the entire guild, else restricted to the channel</param>
        /// <param name="context">context of the command</param>
        /// <param name="badWord">word to be banned</param>
        /// <returns></returns>
        public async Task <BindingStatus> AddBinding(bool global, ICommandContext context, string badWord)
        {
            using (var db = new BadWordContext(dbOptions))
            {
                if (global)
                {
                    var badWordDbEntry = new ServerBadWord
                    {
                        Entry    = badWord,
                        ServerId = context.Guild.Id,
                    };

                    if (await db.BadWordServerBindings.AnyAsync(b => b.ServerId == context.Guild.Id && b.BadWords.Any(e => e == badWordDbEntry)))
                    {
                        return(BindingStatus.AlreadyExists);
                    }

                    if (!await db.BadWordServerBindings.AnyAsync(b => b.ServerId == context.Guild.Id))
                    {
                        var serverDbEntry = new BadWordServerBinding
                        {
                            ServerId = context.Guild.Id,
                            BadWords = new List <ServerBadWord>
                            {
                                badWordDbEntry,
                            },
                        };
                        await db.BadWordServerBindings.AddAsync(serverDbEntry);
                    }
                    else
                    {
                        foreach (var server in db.BadWordServerBindings.Include(b => b.BadWords).Where(b => b.ServerId == context.Guild.Id))
                        {
                            db.BadWordServerBindings.Update(server);
                            server.BadWords.Add(badWordDbEntry);
                        }
                    }

                    await db.SaveChangesAsync();

                    return(BindingStatus.Added);
                }
                else
                {
                    var badWordDbEntry = new ChannelBadWord
                    {
                        Entry     = badWord,
                        ChannelId = context.Channel.Id,
                        ServerId  = context.Guild.Id,
                    };

                    if (await db.BadWordChannelBindings.AnyAsync(b => b.ChannelId == context.Guild.Id && b.BadWords.Any(e => e == badWordDbEntry)))
                    {
                        return(BindingStatus.AlreadyExists);
                    }

                    if (!await db.BadWordChannelBindings.AnyAsync(b => b.ChannelId == context.Guild.Id))
                    {
                        var serverDbEntry = new BadWordChannelBinding
                        {
                            ChannelId = context.Guild.Id,
                            ServerId  = context.Guild.Id,
                            BadWords  = new List <ChannelBadWord>
                            {
                                badWordDbEntry,
                            },
                        };
                        await db.BadWordChannelBindings.AddAsync(serverDbEntry);
                    }
                    else
                    {
                        foreach (var channel in db.BadWordChannelBindings.Include(b => b.BadWords).Where(b => b.ChannelId == context.Channel.Id))
                        {
                            db.BadWordChannelBindings.Update(channel);
                            channel.BadWords.Add(badWordDbEntry);
                        }
                    }

                    await db.SaveChangesAsync();

                    return(BindingStatus.Added);
                }
            }
        }