From 2605351f5cf1aa91be4d5071c03f42988940f93d Mon Sep 17 00:00:00 2001 From: Toastie Date: Sat, 17 Aug 2024 22:46:31 +1200 Subject: [PATCH] .setgame renamed to .setactivity and now supports custom activities (with no playing in front of the text) --- .../PlayingRotate/PlayingRotateService.cs | 2 +- .../Administration/Self/SelfCommands.cs | 23 +++-- .../Administration/Self/SelfService.cs | 99 ++++++++++--------- src/EllieBot/data/aliases.yml | 3 +- .../data/strings/commands/commands.en-US.yml | 10 +- .../strings/responses/responses.en-US.json | 2 +- 6 files changed, 80 insertions(+), 59 deletions(-) diff --git a/src/EllieBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs b/src/EllieBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs index edf6843..0c9afff 100644 --- a/src/EllieBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs +++ b/src/EllieBot/Modules/Administration/PlayingRotate/PlayingRotateService.cs @@ -58,7 +58,7 @@ public sealed class PlayingRotateService : IEService, IReadyExecutor : rotatingStatuses[index++]; var statusText = await _repService.ReplaceAsync(playingStatus.Status, new (client: _client)); - await _selfService.SetGameAsync(statusText, (ActivityType)playingStatus.Type); + await _selfService.SetActivityAsync(statusText, (ActivityType)playingStatus.Type); } catch (Exception ex) { diff --git a/src/EllieBot/Modules/Administration/Self/SelfCommands.cs b/src/EllieBot/Modules/Administration/Self/SelfCommands.cs index b8842cd..57f5765 100644 --- a/src/EllieBot/Modules/Administration/Self/SelfCommands.cs +++ b/src/EllieBot/Modules/Administration/Self/SelfCommands.cs @@ -459,7 +459,7 @@ public partial class Administration await Response().Confirm(strs.bot_name(Format.Bold(newName))).SendAsync(); } - + [Cmd] [OwnerOnly] public async Task SetStatus([Leftover] SettableUserStatus status) @@ -491,14 +491,25 @@ public partial class Administration [Cmd] [OwnerOnly] - public async Task SetGame(ActivityType type, [Leftover] string game = null) + public async Task SetActivity(ActivityType? type, [Leftover] string game = null) { // var rep = new ReplacementBuilder().WithDefault(Context).Build(); var repCtx = new ReplacementContext(ctx); - await _service.SetGameAsync(game is null ? game : await repSvc.ReplaceAsync(game, repCtx), type); + await _service.SetActivityAsync(game is null ? game : await repSvc.ReplaceAsync(game, repCtx), type); - await Response().Confirm(strs.set_game).SendAsync(); + await Response().Confirm(strs.set_activity).SendAsync(); + } + + [Cmd] + [OwnerOnly] + public Task SetActivity([Leftover] string game = null) + => SetActivity(null, game); + + public class SetActivityOptions + { + public ActivityType? Type { get; set; } + public string Game { get; set; } } [Cmd] @@ -541,11 +552,11 @@ public partial class Administration return; } - + var repCtx = new ReplacementContext(ctx); text = await repSvc.ReplaceAsync(text, repCtx); await Response().Channel(ch).Text(text).SendAsync(); - + await ctx.OkAsync(); } diff --git a/src/EllieBot/Modules/Administration/Self/SelfService.cs b/src/EllieBot/Modules/Administration/Self/SelfService.cs index 82eb68c..bbed3f7 100644 --- a/src/EllieBot/Modules/Administration/Self/SelfService.cs +++ b/src/EllieBot/Modules/Administration/Self/SelfService.cs @@ -85,13 +85,14 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService { await using var uow = _db.GetDbContext(); - autoCommands = uow.Set().AsNoTracking() - .Where(x => x.Interval >= 5) - .AsEnumerable() - .GroupBy(x => x.GuildId) - .ToDictionary(x => x.Key, - y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent()) - .ToConcurrent(); + autoCommands = uow.Set() + .AsNoTracking() + .Where(x => x.Interval >= 5) + .AsEnumerable() + .GroupBy(x => x.GuildId) + .ToDictionary(x => x.Key, + y => y.ToDictionary(x => x.Id, TimerFromAutoCommand).ToConcurrent()) + .ToConcurrent(); var startupCommands = uow.Set().AsNoTracking().Where(x => x.Interval == 0); foreach (var cmd in startupCommands) @@ -170,18 +171,18 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService private async Task LoadOwnerChannels() { var channels = await _creds.OwnerIds.Select(id => - { - var user = _client.GetUser(id); - if (user is null) - return Task.FromResult(null); + { + var user = _client.GetUser(id); + if (user is null) + return Task.FromResult(null); - return user.CreateDMChannelAsync(); - }) - .WhenAll(); + return user.CreateDMChannelAsync(); + }) + .WhenAll(); ownerChannels = channels.Where(x => x is not null) - .ToDictionary(x => x.Recipient.Id, x => x) - .ToImmutableDictionary(); + .ToDictionary(x => x.Recipient.Id, x => x) + .ToImmutableDictionary(); if (!ownerChannels.Any()) { @@ -400,7 +401,10 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService { try { - await _client.SetGameAsync(data.Name, data.Link, data.Type); + if (data.Type is { } activityType) + await _client.SetGameAsync(data.Name, data.Link, activityType); + else + await _client.SetCustomStatusAsync(data.Name); } catch (Exception ex) { @@ -408,7 +412,7 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService } }); - public Task SetGameAsync(string game, ActivityType type) + public Task SetActivityAsync(string game, ActivityType? type) => _pubSub.Pub(_activitySetKey, new() { @@ -430,10 +434,10 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService { public string Name { get; init; } public string Link { get; init; } - public ActivityType Type { get; init; } + public ActivityType? Type { get; init; } } - - + + /// /// Adds the specified to the database. If a database user with placeholder name /// and discriminator is present in , their name and discriminator get updated accordingly. @@ -443,41 +447,46 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService /// A tuple with the amount of new users added and old users updated. public async Task<(long UsersAdded, long UsersUpdated)> RefreshUsersAsync(List users) { - await using var ctx = _db.GetDbContext(); + await using var ctx = _db.GetDbContext(); var presentDbUsers = await ctx.GetTable() - .Select(x => new { x.UserId, x.Username, x.Discriminator }) - .Where(x => users.Select(y => y.Id).Contains(x.UserId)) - .ToArrayAsyncEF(); + .Select(x => new + { + x.UserId, + x.Username, + x.Discriminator + }) + .Where(x => users.Select(y => y.Id).Contains(x.UserId)) + .ToArrayAsyncEF(); var usersToAdd = users - .Where(x => !presentDbUsers.Select(x => x.UserId).Contains(x.Id)) - .Select(x => new DiscordUser() - { - UserId = x.Id, - AvatarId = x.AvatarId, - Username = x.Username, - Discriminator = x.Discriminator - }); + .Where(x => !presentDbUsers.Select(x => x.UserId).Contains(x.Id)) + .Select(x => new DiscordUser() + { + UserId = x.Id, + AvatarId = x.AvatarId, + Username = x.Username, + Discriminator = x.Discriminator + }); var added = (await ctx.BulkCopyAsync(usersToAdd)).RowsCopied; var toUpdateUserIds = presentDbUsers - .Where(x => x.Username == "Unknown" && x.Discriminator == "????") - .Select(x => x.UserId) - .ToArray(); + .Where(x => x.Username == "Unknown" && x.Discriminator == "????") + .Select(x => x.UserId) + .ToArray(); foreach (var user in users.Where(x => toUpdateUserIds.Contains(x.Id))) { await ctx.GetTable() - .Where(x => x.UserId == user.Id) - .UpdateAsync(x => new DiscordUser() - { - Username = user.Username, - Discriminator = user.Discriminator, + .Where(x => x.UserId == user.Id) + .UpdateAsync(x => new DiscordUser() + { + Username = user.Username, + Discriminator = user.Discriminator, - // .award tends to set AvatarId and DateAdded to NULL, so account for that. - AvatarId = user.AvatarId, - DateAdded = x.DateAdded ?? DateTime.UtcNow - }); + // .award tends to set AvatarId and DateAdded to NULL, so account for that. + AvatarId = user.AvatarId, + DateAdded = x.DateAdded ?? DateTime.UtcNow + }); } return (added, toUpdateUserIds.Length); diff --git a/src/EllieBot/data/aliases.yml b/src/EllieBot/data/aliases.yml index 4752882..b6f5924 100644 --- a/src/EllieBot/data/aliases.yml +++ b/src/EllieBot/data/aliases.yml @@ -208,7 +208,8 @@ setavatar: - setav setbanner: - setbanner -setgame: +setactivity: + - setactivity - setgame send: - send diff --git a/src/EllieBot/data/strings/commands/commands.en-US.yml b/src/EllieBot/data/strings/commands/commands.en-US.yml index bd5f4a4..3f96439 100644 --- a/src/EllieBot/data/strings/commands/commands.en-US.yml +++ b/src/EllieBot/data/strings/commands/commands.en-US.yml @@ -813,12 +813,12 @@ setbanner: params: - img: desc: "The URL of the image file to be displayed as the bot's banner." -setgame: - desc: Sets the bots game status to either Playing, Listening, or Watching. +setactivity: + desc: Sets the bots game status to a Custom, Playing, Listening, or Watching status. ex: - - Playing with snakes. - - Watching anime. - - Listening music. + - Just chilling + - Playing with canaries. + - Listening music params: - type: desc: "The activity type determines whether the bot is engaged in a game, listening to audio, or watching a video." diff --git a/src/EllieBot/data/strings/responses/responses.en-US.json b/src/EllieBot/data/strings/responses/responses.en-US.json index fe495bb..31ee64f 100644 --- a/src/EllieBot/data/strings/responses/responses.en-US.json +++ b/src/EllieBot/data/strings/responses/responses.en-US.json @@ -195,7 +195,7 @@ "srvr_banner_too_large": "Specified image is too large! Maximum size is 8MB.", "srvr_banner_invalid_url": "Specified url is not valid. Make sure you're specifying a direct image url.", "set_channel_name": "New channel name set.", - "set_game": "New game set!", + "set_activity": "New activity set!", "set_stream": "New stream set!", "set_topic": "New channel topic set.", "shard_reconnecting": "Shard {0} reconnecting.",