예제 #1
0
        public async Task <bool> AddBinding(IGuild guild, IMessageChannel channel, bool move = false)
        {
            using (var db = new JoinPartServerContext(dbOptions))
            {
                var binding = new JoinPartServerBinding
                {
                    ServerId  = guild.Id,
                    ChannelId = channel.Id,
                };

                var bindingExists = db.JoinPartServerBindings.Any(b => b.ServerId == binding.ServerId);

                if (bindingExists && !move)
                {
                    return(false);
                }
                if (bindingExists)
                {
                    db.JoinPartServerBindings.Update(binding);
                }
                else
                {
                    db.JoinPartServerBindings.Add(binding);
                }

                await db.SaveChangesAsync();

                return(true);
            }
        }
예제 #2
0
        public async Task <bool> RemoveBinding(IEntity <ulong> binding)
        {
            using (var db = new JoinPartServerContext(dbOptions))
            {
                var entities = db.JoinPartServerBindings.Where(b => b.ServerId == binding.Id);

                if (!entities.Any())
                {
                    return(false);
                }

                db.JoinPartServerBindings.RemoveRange(entities);
                await db.SaveChangesAsync();

                return(true);
            }
        }
예제 #3
0
        private async Task <IMessageChannel> GetGreetingChannel(IGuild guild)
        {
            using (var db = new JoinPartServerContext(dbOptions))
            {
                var server = await db.JoinPartServerBindings.FirstOrDefaultAsync(srv => srv.ServerId == guild.Id);

                if (server == default(JoinPartServerBinding))
                {
                    return(null);
                }
                var greetingChannel = client.GetChannel(server.ChannelId);

                if (greetingChannel is IMessageChannel msgChannel)
                {
                    return(msgChannel);
                }
                db.JoinPartServerBindings.Remove(server);

                await db.SaveChangesAsync();

                return(null);
            }
        }