.setgame renamed to .setactivity and now supports custom activities (with no playing in front of the text)

This commit is contained in:
Toastie 2024-08-17 22:46:31 +12:00
parent 05c03248c4
commit 2605351f5c
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
6 changed files with 80 additions and 59 deletions

View file

@ -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)
{

View file

@ -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();
}

View file

@ -85,13 +85,14 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService
{
await using var uow = _db.GetDbContext();
autoCommands = uow.Set<AutoCommand>().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<AutoCommand>()
.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<AutoCommand>().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<IDMChannel>(null);
{
var user = _client.GetUser(id);
if (user is null)
return Task.FromResult<IDMChannel>(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; }
}
/// <summary>
/// Adds the specified <paramref name="users"/> to the database. If a database user with placeholder name
/// and discriminator is present in <paramref name="users"/>, their name and discriminator get updated accordingly.
@ -443,41 +447,46 @@ public sealed class SelfService : IExecNoCommand, IReadyExecutor, IEService
/// <returns>A tuple with the amount of new users added and old users updated.</returns>
public async Task<(long UsersAdded, long UsersUpdated)> RefreshUsersAsync(List<IUser> users)
{
await using var ctx = _db.GetDbContext();
await using var ctx = _db.GetDbContext();
var presentDbUsers = await ctx.GetTable<DiscordUser>()
.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<DiscordUser>()
.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);

View file

@ -208,7 +208,8 @@ setavatar:
- setav
setbanner:
- setbanner
setgame:
setactivity:
- setactivity
- setgame
send:
- send

View file

@ -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."

View file

@ -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.",