Пример #1
0
        private async Task QueryAndPrintInfoAsync(CommandContext ctx, DatabaseSwatServer server)
        {
            SwatServerInfo info = await SwatServerInfo.QueryIPAsync(server.IP, server.QueryPort);

            if (info is null)
            {
                await this.InformFailureAsync(ctx, "No reply from server.");

                return;
            }

            DiscordMessage msg = await ctx.RespondAsync(embed : info.ToDiscordEmbed(this.ModuleColor));

            await msg.CreateReactionAsync(StaticDiscordEmoji.Information);

            await Task.Delay(250);

            InteractivityResult <MessageReactionAddEventArgs> rctx = await ctx.Client.GetInteractivity().WaitForEventArgsAsync <MessageReactionAddEventArgs>(e => e.Message == msg && e.Emoji == StaticDiscordEmoji.Information);

            if (!rctx.TimedOut)
            {
                SwatServerInfo completeInfo = await SwatServerInfo.QueryIPAsync(server.IP, server.QueryPort, complete : true);

                if (completeInfo is null)
                {
                    await this.InformFailureAsync(ctx, "No reply from server.");

                    return;
                }
                await msg.ModifyAsync(embed : completeInfo.ToDiscordEmbed(this.ModuleColor));
            }
        }
            public async Task AddAsync(CommandContext ctx,
                                       [Description("Name.")] string name,
                                       [Description("IP.")] CustomIPFormat ip,
                                       [Description("Query port")] int queryport = 10481)
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new InvalidCommandUsageException("Invalid name.");
                }

                if (queryport <= 0 || queryport > 65535)
                {
                    throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
                }

                using (DatabaseContext db = this.Database.CreateContext()) {
                    var newServer = DatabaseSwatServer.FromIP(ip.Content, queryport, name);
                    if (db.SwatServers.Any(s => s.Name == name || (s.IP == newServer.IP && s.JoinPort == newServer.JoinPort && s.QueryPort == newServer.QueryPort)))
                    {
                        throw new CommandFailedException("A server with such name/IP is already listed!");
                    }
                    db.SwatServers.Add(newServer);
                    await db.SaveChangesAsync();
                }

                await this.InformAsync(ctx, "Server added. You can now query it using the name provided.", important : false);
            }
Пример #3
0
        public Task QueryAsync(CommandContext ctx,
                               [Description("Server IP.")] CustomIPFormat ip,
                               [Description("Query port")] int queryport = 10481)
        {
            if (queryport <= 0 || queryport > 65535)
            {
                throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
            }

            var server = DatabaseSwatServer.FromIP(ip.Content, queryport);

            return(this.QueryAndPrintInfoAsync(ctx, server));
        }
Пример #4
0
        public async Task StartCheckAsync(CommandContext ctx,
                                          [Description("IP.")] CustomIPFormat ip,
                                          [Description("Query port")] int queryport = 10481)
        {
            if (queryport <= 0 || queryport > 65535)
            {
                throw new InvalidCommandUsageException("Port range invalid (must be in range [1, 65535])!");
            }

            if (SwatSpaceCheckService.IsListening(ctx.Channel))
            {
                throw new CommandFailedException("Already checking space in this channel!");
            }

            var server = DatabaseSwatServer.FromIP(ip.Content, queryport);

            SwatSpaceCheckService.AddListener(server, ctx.Channel);

            await this.InformAsync(ctx, $"Starting space listening on {server.IP}:{server.JoinPort}... Use command {Formatter.Bold("swat stopcheck")} to stop the check.", important : false);
        }
            public async Task DeleteAsync(CommandContext ctx,
                                          [Description("Name.")] string name)
            {
                if (string.IsNullOrWhiteSpace(name))
                {
                    throw new InvalidCommandUsageException("Name missing.");
                }
                name = name.ToLowerInvariant();

                using (DatabaseContext db = this.Database.CreateContext()) {
                    DatabaseSwatServer server = db.SwatServers.SingleOrDefault(s => s.Name == name);
                    if (!(server is null))
                    {
                        db.SwatServers.Remove(server);
                        await db.SaveChangesAsync();
                    }
                }

                await this.InformAsync(ctx, "Server successfully removed.", important : false);
            }
Пример #6
0
        public static void AddListener(DatabaseSwatServer server, DiscordChannel channel)
        {
            if (_listeners.Count > 10 || _listeners.Any(kvp => kvp.Value.Count > 10))
            {
                throw new Exception("Maximum amount of simultanous checks reached. Please try again later.");
            }

            lock (_lock) {
                _listeners.AddOrUpdate(
                    server,
                    new ConcurrentHashSet <DiscordChannelInfo> {
                    new DiscordChannelInfo(channel)
                },
                    (k, v) => {
                    v.Add(new DiscordChannelInfo(channel));
                    return(v);
                }
                    );
            }

            Start();
        }