예제 #1
0
        public bool RemoveAutoCommand(int index, out AutoCommand cmd)
        {
            using (var uow = _db.GetDbContext())
            {
                cmd = uow._context.AutoCommands
                      .AsNoTracking()
                      .Where(x => x.Interval >= 5)
                      .Skip(index)
                      .FirstOrDefault();

                if (cmd != null)
                {
                    uow._context.Remove(cmd);
                    if (_autoCommands.TryGetValue(cmd.GuildId, out var autos))
                    {
                        if (autos.TryRemove(cmd.Id, out var timer))
                        {
                            timer.Change(Timeout.Infinite, Timeout.Infinite);
                        }
                    }
                    uow.SaveChanges();
                    return(true);
                }
            }

            return(false);
        }
예제 #2
0
        private async Task ExecuteCommand(AutoCommand cmd)
        {
            try
            {
                if (cmd.GuildId is null)
                {
                    return;
                }

                var guildShard = (int)((cmd.GuildId.Value >> 22) % (ulong)_creds.TotalShards);
                if (guildShard != _client.ShardId)
                {
                    return;
                }
                var prefix = _cmdHandler.GetPrefix(cmd.GuildId);
                //if someone already has .die as their startup command, ignore it
                if (cmd.CommandText.StartsWith(prefix + "die", StringComparison.InvariantCulture))
                {
                    return;
                }
                await _cmdHandler.ExecuteExternal(cmd.GuildId, cmd.ChannelId, cmd.CommandText).ConfigureAwait(false);
            }
            catch (Exception ex)
            {
                Log.Warning(ex, "Error in SelfService ExecuteCommand");
            }
        }
예제 #3
0
 private Timer TimerFromAutoCommand(AutoCommand x)
 {
     return(new Timer(async(obj) => await ExecuteCommand((AutoCommand)obj).ConfigureAwait(false),
                      x,
                      x.Interval * 1000,
                      x.Interval * 1000));
 }
예제 #4
0
            public async Task AutoCommandAdd(int interval, [Leftover] string cmdText)
            {
                if (cmdText.StartsWith(Prefix + "die", StringComparison.InvariantCulture))
                {
                    return;
                }

                if (interval < 5)
                {
                    return;
                }

                var guser = (IGuildUser)ctx.User;
                var cmd   = new AutoCommand()
                {
                    CommandText      = cmdText,
                    ChannelId        = ctx.Channel.Id,
                    ChannelName      = ctx.Channel.Name,
                    GuildId          = ctx.Guild?.Id,
                    GuildName        = ctx.Guild?.Name,
                    VoiceChannelId   = guser.VoiceChannel?.Id,
                    VoiceChannelName = guser.VoiceChannel?.Name,
                    Interval         = interval,
                };

                _service.AddNewAutoCommand(cmd);

                await ReplyConfirmLocalizedAsync("autocmd_add", Format.Code(Format.Sanitize(cmdText)), cmd.Interval).ConfigureAwait(false);
            }
예제 #5
0
            public async Task StartupCommandAdd([Leftover] string cmdText)
            {
                if (cmdText.StartsWith(Prefix + "die", StringComparison.InvariantCulture))
                {
                    return;
                }

                var guser = (IGuildUser)ctx.User;
                var cmd   = new AutoCommand()
                {
                    CommandText      = cmdText,
                    ChannelId        = ctx.Channel.Id,
                    ChannelName      = ctx.Channel.Name,
                    GuildId          = ctx.Guild?.Id,
                    GuildName        = ctx.Guild?.Name,
                    VoiceChannelId   = guser.VoiceChannel?.Id,
                    VoiceChannelName = guser.VoiceChannel?.Name,
                    Interval         = 0,
                };

                _service.AddNewAutoCommand(cmd);

                await ctx.Channel.EmbedAsync(new EmbedBuilder().WithOkColor()
                                             .WithTitle(GetText("scadd"))
                                             .AddField(efb => efb.WithName(GetText("server"))
                                                       .WithValue(cmd.GuildId == null ? $"-" : $"{cmd.GuildName}/{cmd.GuildId}").WithIsInline(true))
                                             .AddField(efb => efb.WithName(GetText("channel"))
                                                       .WithValue($"{cmd.ChannelName}/{cmd.ChannelId}").WithIsInline(true))
                                             .AddField(efb => efb.WithName(GetText("command_text"))
                                                       .WithValue(cmdText).WithIsInline(false))).ConfigureAwait(false);
            }
예제 #6
0
 public void VoteEnd()
 {
     //Make sure it's all good for next round
     _command       = null;
     VoteStatus     = Status.voteStandby;
     voteInProgress = null;
     _voteReg.Clear();
 }
예제 #7
0
 protected void AddAutoCommand(EventKind eventKind, string pattern, string command)
 {
     var autoCommand = new AutoCommand(
         AutoCommandGroup.Default,
         eventKind,
         command,
         pattern);
     VimData.AutoCommands = VimData.AutoCommands.Concat(new[] { autoCommand }).ToFSharpList();
 }
예제 #8
0
        protected void AddAutoCommand(EventKind eventKind, string pattern, string command)
        {
            var autoCommand = new AutoCommand(
                AutoCommandGroup.Default,
                eventKind,
                command,
                pattern);

            VimData.AutoCommands = VimData.AutoCommands.Concat(new[] { autoCommand }).ToFSharpList();
        }
예제 #9
0
        public static void AddAutoCommand(this IVimData vimData, EventKind eventKind, string pattern, string command)
        {
            var autoCommand = new AutoCommand(
                AutoCommandGroup.Default,
                eventKind,
                command,
                pattern);

            vimData.AutoCommands = vimData.AutoCommands.Concat(new[] { autoCommand }).ToFSharpList();
        }
예제 #10
0
        public void AddNewAutoCommand(AutoCommand cmd)
        {
            using (var uow = _db.GetDbContext())
            {
                uow._context.AutoCommands.Add(cmd);
                uow.SaveChanges();
            }

            if (cmd.Interval >= 5)
            {
                var autos = _autoCommands.GetOrAdd(cmd.GuildId, new ConcurrentDictionary <int, Timer>());
                autos.AddOrUpdate(cmd.Id, key => TimerFromAutoCommand(cmd), (key, old) =>
                {
                    old.Change(Timeout.Infinite, Timeout.Infinite);
                    return(TimerFromAutoCommand(cmd));
                });
            }
        }
예제 #11
0
        public bool RemoveStartupCommand(int index, out AutoCommand cmd)
        {
            using (var uow = _db.GetDbContext())
            {
                cmd = uow._context.AutoCommands
                      .AsNoTracking()
                      .Where(x => x.Interval == 0)
                      .Skip(index)
                      .FirstOrDefault();

                if (cmd != null)
                {
                    uow._context.Remove(cmd);
                    uow.SaveChanges();
                    return(true);
                }
            }

            return(false);
        }
예제 #12
0
        public void Vote(string name)
        {
            if (Context.Player == null)
            {
                return;
            }

            if (VoteStatus == Status.voteInProgress)
            {
                Context.Respond(
                    $"vote for {voteInProgress} is currently active. Use !yes to vote and !no to retract vote");
                return;
            }

            _command = EssentialsPlugin.Instance.Config.AutoCommands.FirstOrDefault(c => c.Name.Equals(name));

            if (_command == null || _command.CommandTrigger != Trigger.Vote)
            {
                Context.Respond($"Couldn't find any votable command with the name {name}");
                _command = null;
                return;
            }


            // Rexxar's spam blocker. Timing is random as f**k and unique to each player.
            var steamid = Context.Player.SteamUserId;

            if (_voteCooldown.TryGetValue(steamid, out var activeCooldown))
            {
                var difference = activeCooldown - DateTime.Now;
                if (difference.TotalSeconds > 0)
                {
                    Context.Respond(
                        $"Cooldown active. You can use this command again in {difference.Minutes:N0} minutes : {difference.Seconds:N0} seconds");
                    return;
                }

                _voteCooldown[steamid] = DateTime.Now.AddMinutes(_cooldown);
            }

            else
            {
                _voteCooldown.Add(steamid, DateTime.Now.AddMinutes(_cooldown));
            }

            var _voteDuration = TimeSpan.Parse(_command.Interval);

            // voting status
            voteInProgress = name;
            VoteStatus     = Status.voteInProgress;
            VoteYes();
            var sb = new StringBuilder();

            sb.AppendLine($"Voting started for {name} by {Context.Player.DisplayName}.");
            sb.AppendLine("Use !yes to vote and !no to retract your vote");
            ModCommunication.SendMessageToClients(new NotificationMessage(sb.ToString(), 15000, "Blue"));
            //vote countdown
            Task.Run(() =>
            {
                var countdown = VoteCountdown(_voteDuration).GetEnumerator();
                while (countdown.MoveNext())
                {
                    Thread.Sleep(1000);
                }
            });

            lastVoteName = voteInProgress;
        }
예제 #13
0
 public HistoryManager()
 {
     _undoCommand = new AutoCommand(UndoCommand_Execute, () => CanUndo);
     _redoCommand = new AutoCommand(RedoCommand_Execute, () => CanRedo);
 }