Skip to content

Commit

Permalink
include oldminutes in /server minutes
Browse files Browse the repository at this point in the history
  • Loading branch information
adamgauthier committed Dec 5, 2023
1 parent d9f481a commit adf54ba
Show file tree
Hide file tree
Showing 11 changed files with 58 additions and 197 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -7,19 +7,8 @@
namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

[Name("Activity 📚")]
public class ActivityModule : TaylorBotModule
public class ActivityModule(ICommandRunner commandRunner, ServerMessagesSlashCommand serverMessagesCommand, ServerMinutesSlashCommand serverMinutesCommand) : TaylorBotModule
{
private readonly ICommandRunner _commandRunner;
private readonly ServerMessagesSlashCommand _serverMessagesCommand;
private readonly ServerMinutesSlashCommand _serverMinutesCommand;

public ActivityModule(ICommandRunner commandRunner, ServerMessagesSlashCommand serverMessagesCommand, ServerMinutesSlashCommand serverMinutesCommand)
{
_commandRunner = commandRunner;
_serverMessagesCommand = serverMessagesCommand;
_serverMinutesCommand = serverMinutesCommand;
}

[Command("messages")]
[Summary("Show the message count of a server member")]
public async Task<RuntimeResult> MessagesAsync(
Expand All @@ -33,8 +22,8 @@ public async Task<RuntimeResult> MessagesAsync(
await user.GetTrackedUserAsync();

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(
_serverMessagesCommand.Messages(u, isLegacyCommand: true),
var result = await commandRunner.RunAsync(
serverMessagesCommand.Messages(u, isLegacyCommand: true),
context
);

Expand All @@ -58,7 +47,7 @@ Please use it instead! 😊
"""))));

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(command, context);
var result = await commandRunner.RunAsync(command, context);

return new TaylorBotResult(result, context);
}
Expand All @@ -76,8 +65,8 @@ public async Task<RuntimeResult> MinutesAsync(
await user.GetTrackedUserAsync();

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(
_serverMinutesCommand.Minutes(u, isLegacyCommand: true),
var result = await commandRunner.RunAsync(
serverMinutesCommand.Minutes(u, isLegacyCommand: true),
context
);

Expand All @@ -101,7 +90,7 @@ Please use it instead! 😊
"""))));

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(command, context);
var result = await commandRunner.RunAsync(command, context);

return new TaylorBotResult(result, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,8 @@
namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

[Name("Joined 🚪")]
public class JoinedModule : TaylorBotModule
public class JoinedModule(ICommandRunner commandRunner, ServerJoinedSlashCommand serverJoinedCommand) : TaylorBotModule
{
private readonly ICommandRunner _commandRunner;
private readonly ServerJoinedSlashCommand _serverJoinedCommand;

public JoinedModule(ICommandRunner commandRunner, ServerJoinedSlashCommand serverJoinedCommand)
{
_commandRunner = commandRunner;
_serverJoinedCommand = serverJoinedCommand;
}

[Command("joined")]
[Summary("Show the first recorded joined date of a server member")]
public async Task<RuntimeResult> JoinedAsync(
Expand All @@ -31,8 +22,8 @@ public async Task<RuntimeResult> JoinedAsync(
await user.GetTrackedUserAsync();

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(
_serverJoinedCommand.Joined(u, isLegacyCommand: true),
var result = await commandRunner.RunAsync(
serverJoinedCommand.Joined(u, isLegacyCommand: true),
context
);

Expand All @@ -56,7 +47,7 @@ Please use it instead! 😊
"""))));

var context = DiscordNetContextMapper.MapToRunContext(Context);
var result = await _commandRunner.RunAsync(command, context);
var result = await commandRunner.RunAsync(command, context);

return new TaylorBotResult(result, context);
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,27 +23,21 @@ public interface IServerActivityRepository
Task<IList<MessageLeaderboardEntry>> GetMessageLeaderboardAsync(IGuild guild);
Task<int> GetMinutesAsync(IGuildUser guildUser);
Task<IList<MinuteLeaderboardEntry>> GetMinuteLeaderboardAsync(IGuild guild);
Task<int?> GetOldMinutesAsync(IUser user);
}

public class ServerMessagesSlashCommand : ISlashCommand<ServerMessagesSlashCommand.Options>
public class ServerMessagesSlashCommand(IServerActivityRepository serverActivityRepository) : ISlashCommand<ServerMessagesSlashCommand.Options>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server messages");

public record Options(ParsedUserOrAuthor user);

private readonly IServerActivityRepository _serverActivityRepository;

public ServerMessagesSlashCommand(IServerActivityRepository serverActivityRepository)
{
_serverActivityRepository = serverActivityRepository;
}

public Command Messages(IUser user, bool isLegacyCommand) => new(
new("messages"),
async () =>
{
var guildUser = (IGuildUser)user;
var messages = await _serverActivityRepository.GetMessagesAsync(guildUser);
var messages = await serverActivityRepository.GetMessagesAsync(guildUser);
var wordAverage = (double)messages.word_count / messages.message_count;
var embed = new EmbedBuilder()
Expand Down Expand Up @@ -75,25 +69,28 @@ public ValueTask<Command> GetCommandAsync(RunContext context, Options options)
}
}

public class ServerMinutesSlashCommand : ISlashCommand<ServerMinutesSlashCommand.Options>
public class ServerMinutesSlashCommand(IServerActivityRepository serverActivityRepository) : ISlashCommand<ServerMinutesSlashCommand.Options>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server minutes");

public record Options(ParsedUserOrAuthor user);

private readonly IServerActivityRepository _serverActivityRepository;

public ServerMinutesSlashCommand(IServerActivityRepository serverActivityRepository)
{
_serverActivityRepository = serverActivityRepository;
}

public Command Minutes(IUser user, bool isLegacyCommand) => new(
new("minutes"),
async () =>
{
var guildUser = (IGuildUser)user;
var minutes = await _serverActivityRepository.GetMinutesAsync(guildUser);
var minutes = await serverActivityRepository.GetMinutesAsync(guildUser);
var bottomText = "Check out </server leaderboard:1137547317549998130> to see the most active server members! 📃";
if (guildUser.GuildId == 115332333745340416)
{
var oldMinutes = await serverActivityRepository.GetOldMinutesAsync(guildUser);
if (oldMinutes != null)
{
bottomText = $"Before **December 25th 2015**, minutes used to be counted based on online status. Under that system, this user had {"minute".ToQuantity(oldMinutes.Value, TaylorBotFormats.BoldReadable)}. 🧓";
}
}
var embed = new EmbedBuilder()
.WithColor(TaylorBotColors.SuccessColor)
Expand All @@ -102,7 +99,7 @@ public ServerMinutesSlashCommand(IServerActivityRepository serverActivityReposit
{guildUser.Mention} has been active for {"minute".ToQuantity(minutes, TaylorBotFormats.BoldReadable)} in this server. ⏳
This is roughly equivalent to **{TimeSpan.FromMinutes(minutes).Humanize(maxUnit: TimeUnit.Month, culture: TaylorBotCulture.Culture)}** of activity.
Check out </server leaderboard:1137547317549998130> to see the most active server members! 📃
{bottomText}
""");
if (isLegacyCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,19 +11,12 @@

namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

public class ServerLeaderboardSlashCommand : ISlashCommand<ServerLeaderboardSlashCommand.Options>
public class ServerLeaderboardSlashCommand(IServerActivityRepository serverMessageRepository) : ISlashCommand<ServerLeaderboardSlashCommand.Options>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server leaderboard");

public record Options(ParsedString @for);

private readonly IServerActivityRepository _serverMessageRepository;

public ServerLeaderboardSlashCommand(IServerActivityRepository serverMessageRepository)
{
_serverMessageRepository = serverMessageRepository;
}

private record LeaderboardData(string Title, List<string> Pages);

public ValueTask<Command> GetCommandAsync(RunContext context, Options options)
Expand Down Expand Up @@ -63,7 +56,7 @@ private async ValueTask<LeaderboardData> GetLeaderboardData(Options options, IGu
{
case "messages":
{
var leaderboard = await _serverMessageRepository.GetMessageLeaderboardAsync(guild);
var leaderboard = await serverMessageRepository.GetMessageLeaderboardAsync(guild);

var pages = leaderboard.Chunk(15).Select(entries => string.Join('\n', entries.Select(entry =>
$"{entry.rank}. {entry.username.MdUserLink(entry.user_id)}: **~{"message".ToQuantity(entry.message_count, $"{TaylorBotFormats.Readable}**")}"
Expand All @@ -74,7 +67,7 @@ private async ValueTask<LeaderboardData> GetLeaderboardData(Options options, IGu

case "minutes":
{
var leaderboard = await _serverMessageRepository.GetMinuteLeaderboardAsync(guild);
var leaderboard = await serverMessageRepository.GetMinuteLeaderboardAsync(guild);

var pages = leaderboard.Chunk(15).Select(entries => string.Join('\n', entries.Select(entry =>
$"{entry.rank}. {entry.username.MdUserLink(entry.user_id)}: {"minute".ToQuantity(entry.minute_count, TaylorBotFormats.BoldReadable)}"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,17 +9,10 @@

namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

public class ServerNamesSlashCommand : ISlashCommand<NoOptions>
public class ServerNamesSlashCommand(IGuildNamesRepository guildNamesRepository) : ISlashCommand<NoOptions>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server names");

private readonly IGuildNamesRepository _guildNamesRepository;

public ServerNamesSlashCommand(IGuildNamesRepository guildNamesRepository)
{
_guildNamesRepository = guildNamesRepository;
}

public ValueTask<Command> GetCommandAsync(RunContext context, NoOptions options)
{
return new(new Command(
Expand All @@ -28,7 +21,7 @@ public ValueTask<Command> GetCommandAsync(RunContext context, NoOptions options)
{
var guild = context.Guild!;
var guildNames = await _guildNamesRepository.GetHistoryAsync(guild, 75);
var guildNames = await guildNamesRepository.GetHistoryAsync(guild, 75);
var guildNamesAsLines = guildNames.Select(n => $"{n.ChangedAt:MMMM dd, yyyy}: {n.GuildName}");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,26 +9,19 @@

namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

public class ServerPopulationSlashCommand : ISlashCommand<NoOptions>
public class ServerPopulationSlashCommand(IServerStatsRepository serverStatsRepository) : ISlashCommand<NoOptions>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server population");

private readonly IServerStatsRepository _serverStatsRepository;

public ServerPopulationSlashCommand(IServerStatsRepository serverStatsRepository)
{
_serverStatsRepository = serverStatsRepository;
}

public ValueTask<Command> GetCommandAsync(RunContext context, NoOptions options)
{
return new(new Command(
new(Info.Name),
async () =>
{
var guild = context.Guild!;
var ageStats = await _serverStatsRepository.GetAgeStatsInGuildAsync(guild);
GenderStats genderStats = await _serverStatsRepository.GetGenderStatsInGuildAsync(guild);
var ageStats = await serverStatsRepository.GetAgeStatsInGuildAsync(guild);
GenderStats genderStats = await serverStatsRepository.GetGenderStatsInGuildAsync(guild);
string FormatPercent(long count) => ((decimal)count / genderStats.TotalCount).ToString("0.0%", CultureInfo.InvariantCulture);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,25 +12,18 @@

namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Commands;

public class ServerTimelineSlashCommand : ISlashCommand<NoOptions>
public class ServerTimelineSlashCommand(IServerJoinedRepository serverJoinedRepository) : ISlashCommand<NoOptions>
{
public ISlashCommandInfo Info => new MessageCommandInfo("server timeline");

private readonly IServerJoinedRepository _serverJoinedRepository;

public ServerTimelineSlashCommand(IServerJoinedRepository serverJoinedRepository)
{
_serverJoinedRepository = serverJoinedRepository;
}

public ValueTask<Command> GetCommandAsync(RunContext context, NoOptions options)
{
return new(new Command(
new(Info.Name),
async () =>
{
var guild = context.Guild!;
var timeline = await _serverJoinedRepository.GetTimelineAsync(guild);
var timeline = await serverJoinedRepository.GetTimelineAsync(guild);
var pages = timeline.Chunk(15).Select(entries => string.Join('\n', entries.Select(entry =>
$"{((DateTimeOffset)entry.first_joined_at).FormatLongDate()}: {entry.username.MdUserLink(entry.user_id)} is {((int)entry.rank).Ordinalize(TaylorBotCulture.Culture)} to join"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,11 @@

namespace TaylorBot.Net.Commands.Discord.Program.Modules.Server.Infrastructure;

public class ServerActivityPostgresRepository : IServerActivityRepository
public class ServerActivityPostgresRepository(PostgresConnectionFactory postgresConnectionFactory) : IServerActivityRepository
{
private readonly PostgresConnectionFactory _postgresConnectionFactory;

public ServerActivityPostgresRepository(PostgresConnectionFactory postgresConnectionFactory)
{
_postgresConnectionFactory = postgresConnectionFactory;
}

public async Task<ServerMessages> GetMessagesAsync(IGuildUser guildUser)
{
await using var connection = _postgresConnectionFactory.CreateConnection();
await using var connection = postgresConnectionFactory.CreateConnection();

return await connection.QuerySingleAsync<ServerMessages>(
"""
Expand All @@ -35,7 +28,7 @@ FROM guilds.guild_members

public async Task<IList<MessageLeaderboardEntry>> GetMessageLeaderboardAsync(IGuild guild)
{
await using var connection = _postgresConnectionFactory.CreateConnection();
await using var connection = postgresConnectionFactory.CreateConnection();

var entries = await connection.QueryAsync<MessageLeaderboardEntry>(
"""
Expand All @@ -55,7 +48,7 @@ public async Task<IList<MessageLeaderboardEntry>> GetMessageLeaderboardAsync(IGu

public async Task<int> GetMinutesAsync(IGuildUser guildUser)
{
await using var connection = _postgresConnectionFactory.CreateConnection();
await using var connection = postgresConnectionFactory.CreateConnection();

return await connection.QuerySingleAsync<int>(
"""
Expand All @@ -74,7 +67,7 @@ FROM guilds.guild_members

public async Task<IList<MinuteLeaderboardEntry>> GetMinuteLeaderboardAsync(IGuild guild)
{
await using var connection = _postgresConnectionFactory.CreateConnection();
await using var connection = postgresConnectionFactory.CreateConnection();

var entries = await connection.QueryAsync<MinuteLeaderboardEntry>(
"""
Expand All @@ -91,4 +84,20 @@ public async Task<IList<MinuteLeaderboardEntry>> GetMinuteLeaderboardAsync(IGuil

return entries.ToList();
}

public async Task<int?> GetOldMinutesAsync(IUser user)
{
await using var connection = postgresConnectionFactory.CreateConnection();

return await connection.QuerySingleOrDefaultAsync<int?>(
"""
SELECT integer_value FROM attributes.integer_attributes
WHERE attribute_id = 'oldminutes' AND user_id = @UserId;
""",
new
{
UserId = $"{user.Id}",
}
);
}
}
27 changes: 0 additions & 27 deletions src/taylorbot-typescript/attributes/user/OldMinutes.ts

This file was deleted.

Loading

0 comments on commit adf54ba

Please sign in to comment.