From b8065e2065ddc3e00123ad28b47e0ff9dede6ec3 Mon Sep 17 00:00:00 2001 From: Ske Date: Thu, 25 Apr 2019 18:50:07 +0200 Subject: [PATCH] bot: periodically update game status --- PluralKit/Bot/Bot.cs | 13 +++++++++++-- PluralKit/Bot/Commands/SystemCommands.cs | 4 ++-- 2 files changed, 13 insertions(+), 4 deletions(-) diff --git a/PluralKit/Bot/Bot.cs b/PluralKit/Bot/Bot.cs index 2a43b4d9..463077a9 100644 --- a/PluralKit/Bot/Bot.cs +++ b/PluralKit/Bot/Bot.cs @@ -2,6 +2,7 @@ using System; using System.Data; using System.Linq; using System.Reflection; +using System.Threading; using System.Threading.Tasks; using Dapper; using Discord; @@ -75,6 +76,7 @@ namespace PluralKit.Bot private CommandService _commands; private IDbConnection _connection; private ProxyService _proxy; + private Timer _updateTimer; public Bot(IServiceProvider services, IDiscordClient client, CommandService commands, IDbConnection connection, ProxyService proxy) { @@ -98,11 +100,18 @@ namespace PluralKit.Bot _client.MessageDeleted += _proxy.HandleMessageDeletedAsync; } - private Task Ready() + private async Task UpdatePeriodic() { + // Method called every 60 seconds + await _client.SetGameAsync($"pk;help | in {_client.Guilds.Count} servers"); + } + + private async Task Ready() + { + _updateTimer = new Timer((_) => Task.Run(this.UpdatePeriodic), null, 0, 60*1000); + Console.WriteLine($"Shard #{_client.ShardId} connected to {_client.Guilds.Sum(g => g.Channels.Count)} channels in {_client.Guilds.Count} guilds."); Console.WriteLine($"PluralKit started as {_client.CurrentUser.Username}#{_client.CurrentUser.Discriminator} ({_client.CurrentUser.Id})."); - return Task.CompletedTask; } private async Task CommandExecuted(Optional cmd, ICommandContext ctx, IResult _result) diff --git a/PluralKit/Bot/Commands/SystemCommands.cs b/PluralKit/Bot/Commands/SystemCommands.cs index 7ef9de53..cfe72dd9 100644 --- a/PluralKit/Bot/Commands/SystemCommands.cs +++ b/PluralKit/Bot/Commands/SystemCommands.cs @@ -21,7 +21,7 @@ namespace PluralKit.Bot.Commands if (system == null) system = Context.SenderSystem; if (system == null) return NO_SYSTEM_ERROR; - await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateEmbed(system)); + await Context.Channel.SendMessageAsync(embed: await EmbedService.CreateSystemEmbed(system)); return PKResult.Success(); } @@ -33,7 +33,7 @@ namespace PluralKit.Bot.Commands var system = await Systems.Create(systemName); await Systems.Link(system, Context.User.Id); - + await ReplyAsync("Your system has been created. Type `pk;system` to view it, and type `pk;help` for more information about commands you can use now."); return PKResult.Success(); }