bot: periodically update game status

This commit is contained in:
Ske 2019-04-25 18:50:07 +02:00
parent 21b16667df
commit b8065e2065
2 changed files with 13 additions and 4 deletions

View File

@ -2,6 +2,7 @@ using System;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
using System.Threading;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper; using Dapper;
using Discord; using Discord;
@ -75,6 +76,7 @@ namespace PluralKit.Bot
private CommandService _commands; private CommandService _commands;
private IDbConnection _connection; private IDbConnection _connection;
private ProxyService _proxy; private ProxyService _proxy;
private Timer _updateTimer;
public Bot(IServiceProvider services, IDiscordClient client, CommandService commands, IDbConnection connection, ProxyService proxy) public Bot(IServiceProvider services, IDiscordClient client, CommandService commands, IDbConnection connection, ProxyService proxy)
{ {
@ -98,11 +100,18 @@ namespace PluralKit.Bot
_client.MessageDeleted += _proxy.HandleMessageDeletedAsync; _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($"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})."); Console.WriteLine($"PluralKit started as {_client.CurrentUser.Username}#{_client.CurrentUser.Discriminator} ({_client.CurrentUser.Id}).");
return Task.CompletedTask;
} }
private async Task CommandExecuted(Optional<CommandInfo> cmd, ICommandContext ctx, IResult _result) private async Task CommandExecuted(Optional<CommandInfo> cmd, ICommandContext ctx, IResult _result)

View File

@ -21,7 +21,7 @@ namespace PluralKit.Bot.Commands
if (system == null) system = Context.SenderSystem; if (system == null) system = Context.SenderSystem;
if (system == null) return NO_SYSTEM_ERROR; 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(); return PKResult.Success();
} }
@ -33,7 +33,7 @@ namespace PluralKit.Bot.Commands
var system = await Systems.Create(systemName); var system = await Systems.Create(systemName);
await Systems.Link(system, Context.User.Id); 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."); 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(); return PKResult.Success();
} }