private Task Bot_JoinedGuild(GuildConfig gc)
 {
     GuildConfigsCache.AddOrUpdate(gc.GuildId,
                                   GreetSettings.Create(gc),
                                   delegate { return(GreetSettings.Create(gc)); });
     return(Task.CompletedTask);
 }
        private async Task ByeUsers(GreetSettings conf, ITextChannel channel, IEnumerable <IUser> users)
        {
            if (!users.Any())
            {
                return;
            }

            var rep = new ReplacementBuilder()
                      .WithChannel(channel)
                      .WithClient(_client)
                      .WithServer(_client, (SocketGuild)channel.Guild)
                      .WithManyUsers(users)
                      .Build();

            if (CREmbed.TryParse(conf.ChannelByeMessageText, out var embedData))
            {
                rep.Replace(embedData);
                try
                {
                    var toDelete = await channel.EmbedAsync(embedData).ConfigureAwait(false);

                    if (conf.AutoDeleteByeMessagesTimer > 0)
                    {
                        toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer);
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning(ex, "Error embeding bye message");
                }
            }
            else
            {
                var msg = rep.Replace(conf.ChannelByeMessageText);
                if (string.IsNullOrWhiteSpace(msg))
                {
                    return;
                }
                try
                {
                    var toDelete = await channel.SendMessageAsync(msg.SanitizeMentions()).ConfigureAwait(false);

                    if (conf.AutoDeleteByeMessagesTimer > 0)
                    {
                        toDelete.DeleteAfter(conf.AutoDeleteByeMessagesTimer);
                    }
                }
                catch (Exception ex)
                {
                    Log.Warning(ex, "Error sending bye message");
                }
            }
        }
        public async Task <bool> SetGreetDm(ulong guildId, bool?value = null)
        {
            bool enabled;

            using (var uow = _db.UnitOfWork)
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                enabled = conf.SendDmGreetMessage = value ?? !conf.SendDmGreetMessage;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                await uow.CompleteAsync();
            }
            return(enabled);
        }
        public GreetSettings GetOrAddSettingsForGuild(ulong guildId)
        {
            if (GuildConfigsCache.TryGetValue(guildId, out var settings) &&
                settings != null)
            {
                return(settings);
            }

            using (var uow = _db.UnitOfWork)
            {
                var gc = uow.GuildConfigs.ForId(guildId, set => set);
                settings = GreetSettings.Create(gc);
            }

            GuildConfigsCache.TryAdd(guildId, settings);
            return(settings);
        }
        public async Task <bool> SetBye(ulong guildId, ulong channelId, bool?value = null)
        {
            bool enabled;

            using (var uow = _db.UnitOfWork)
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                enabled = conf.SendChannelByeMessage = value ?? !conf.SendChannelByeMessage;
                conf.ByeMessageChannelId = channelId;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                await uow.CompleteAsync().ConfigureAwait(false);
            }
            return(enabled);
        }
Beispiel #6
0
        public async Task <bool> SetGreet(ulong guildId, ulong channelId, bool?value = null)
        {
            bool enabled;

            using (var uow = _db.GetDbContext())
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                enabled = conf.SendChannelGreetMessage = value ?? !conf.SendChannelGreetMessage;
                conf.GreetMessageChannelId = channelId;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                await uow.SaveChangesAsync();
            }
            return(enabled);
        }
        public async Task SetGreetDel(ulong id, int timer)
        {
            if (timer < 0 || timer > 600)
            {
                return;
            }

            using (var uow = _db.UnitOfWork)
            {
                var conf = uow.GuildConfigs.ForId(id, set => set);
                conf.AutoDeleteGreetMessagesTimer = timer;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(id, toAdd, (key, old) => toAdd);

                await uow.CompleteAsync();
            }
        }
Beispiel #8
0
        public async Task SetByeDel(ulong guildId, int timer)
        {
            if (timer < 0 || timer > 600)
            {
                return;
            }

            using (var uow = _db.GetDbContext())
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                conf.AutoDeleteByeMessagesTimer = timer;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                await uow.SaveChangesAsync();
            }
        }
        public async Task <bool> SetSettings(ulong guildId, GreetSettings settings)
        {
            if (settings.AutoDeleteByeMessagesTimer > 600 ||
                settings.AutoDeleteByeMessagesTimer < 0 ||
                settings.AutoDeleteGreetMessagesTimer > 600 ||
                settings.AutoDeleteGreetMessagesTimer < 0)
            {
                return(false);
            }

            using (var uow = _db.UnitOfWork)
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                conf.DmGreetMessageText      = settings.DmGreetMessageText?.SanitizeMentions();
                conf.ChannelGreetMessageText = settings.ChannelGreetMessageText?.SanitizeMentions();
                conf.ChannelByeMessageText   = settings.ChannelByeMessageText?.SanitizeMentions();

                conf.AutoDeleteGreetMessagesTimer = settings.AutoDeleteGreetMessagesTimer;
                conf.AutoDeleteGreetMessages      = settings.AutoDeleteGreetMessagesTimer > 0;

                conf.AutoDeleteByeMessagesTimer = settings.AutoDeleteByeMessagesTimer;
                conf.AutoDeleteByeMessages      = settings.AutoDeleteByeMessagesTimer > 0;

                conf.GreetMessageChannelId = settings.GreetMessageChannelId;
                conf.ByeMessageChannelId   = settings.ByeMessageChannelId;

                conf.SendChannelGreetMessage = settings.SendChannelGreetMessage;
                conf.SendChannelByeMessage   = settings.SendChannelByeMessage;

                await uow.CompleteAsync();

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);
            }

            return(true);
        }
        public bool SetByeMessage(ulong guildId, ref string message)
        {
            message = message?.SanitizeMentions();

            if (string.IsNullOrWhiteSpace(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            bool byeMsgEnabled;

            using (var uow = _db.UnitOfWork)
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                conf.ChannelByeMessageText = message;
                byeMsgEnabled = conf.SendChannelByeMessage;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                uow.Complete();
            }
            return(byeMsgEnabled);
        }
Beispiel #11
0
        public bool SetGreetDmMessage(ulong guildId, ref string message)
        {
            message = message?.SanitizeMentions();

            if (string.IsNullOrWhiteSpace(message))
            {
                throw new ArgumentNullException(nameof(message));
            }

            bool greetMsgEnabled;

            using (var uow = _db.GetDbContext())
            {
                var conf = uow.GuildConfigs.ForId(guildId, set => set);
                conf.DmGreetMessageText = message;
                greetMsgEnabled         = conf.SendDmGreetMessage;

                var toAdd = GreetSettings.Create(conf);
                GuildConfigsCache.AddOrUpdate(guildId, toAdd, (key, old) => toAdd);

                uow.SaveChanges();
            }
            return(greetMsgEnabled);
        }
        private async Task <bool> GreetDmUser(GreetSettings conf, IDMChannel channel, IGuildUser user)
        {
            var rep = new ReplacementBuilder()
                      .WithDefault(user, channel, (SocketGuild)user.Guild, _client)
                      .Build();

            if (CREmbed.TryParse(conf.DmGreetMessageText, out var embedData))
            {
                rep.Replace(embedData);
                try
                {
                    await channel.EmbedAsync(embedData).ConfigureAwait(false);
                }
                catch
                {
                    return(false);
                }
            }
            else
            {
                var msg = rep.Replace(conf.DmGreetMessageText);
                if (!string.IsNullOrWhiteSpace(msg))
                {
                    try
                    {
                        await channel.SendConfirmAsync(msg).ConfigureAwait(false);
                    }
                    catch
                    {
                        return(false);
                    }
                }
            }

            return(true);
        }
 private Task GreetUsers(GreetSettings conf, ITextChannel channel, IGuildUser user)
 => GreetUsers(conf, channel, new[] { user });
 private Task ByeUsers(GreetSettings conf, ITextChannel channel, IUser user)
 => ByeUsers(conf, channel, new[] { user });