Stagger greet dms

This commit is contained in:
Kwoth
2022-01-06 22:41:49 +01:00
parent 352ec9a562
commit 73de20b615
4 changed files with 48 additions and 15 deletions

View File

@@ -214,8 +214,7 @@ public partial class Administration
{
user ??= (IGuildUser)ctx.User;
var channel = await user.CreateDMChannelAsync();
var success = await _service.GreetDmTest(channel, user);
var success = await _service.GreetDmTest(user);
if (success)
await ctx.OkAsync();
else

View File

@@ -1,9 +1,11 @@
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db;
using NadekoBot.Services.Database.Models;
using System.Threading.Channels;
namespace NadekoBot.Services;
public class GreetService : INService
public class GreetService : INService, IReadyExecutor
{
public bool GroupGreets
=> _bss.Data.GroupGreets;
@@ -38,6 +40,17 @@ public class GreetService : INService
_client.GuildMemberUpdated += ClientOnGuildMemberUpdated;
}
public async Task OnReadyAsync()
{
while (true)
{
var (conf, user, compl) = await _greetDmQueue.Reader.ReadAsync();
var res = await GreetDmUserInternal(conf, user);
compl.TrySetResult(res);
await Task.Delay(2000);
}
}
private Task ClientOnGuildMemberUpdated(Cacheable<SocketGuildUser, ulong> optOldUser, SocketGuildUser newUser)
{
// if user is a new booster
@@ -209,14 +222,33 @@ public class GreetService : INService
}
}
private async Task<bool> GreetDmUser(GreetSettings conf, IDMChannel channel, IGuildUser user)
private readonly Channel<(GreetSettings, IGuildUser, TaskCompletionSource<bool>)> _greetDmQueue =
Channel.CreateBounded<(GreetSettings, IGuildUser, TaskCompletionSource<bool>)>(new BoundedChannelOptions(60)
{
// The limit of 60 users should be only hit when there's a raid. In that case
// probably the best thing to do is to drop newest (raiding) users
FullMode = BoundedChannelFullMode.DropNewest
});
private async Task<bool> GreetDmUser(GreetSettings conf, IGuildUser user)
{
var completionSource = new TaskCompletionSource<bool>(TaskCreationOptions.RunContinuationsAsynchronously);
await _greetDmQueue.Writer.WriteAsync((conf, user, completionSource));
return await completionSource.Task;
}
private async Task<bool> GreetDmUserInternal(GreetSettings conf, IGuildUser user)
{
var rep = new ReplacementBuilder().WithDefault(user, channel, (SocketGuild)user.Guild, _client).Build();
var text = SmartText.CreateFrom(conf.DmGreetMessageText);
text = rep.Replace(text);
try
{
var rep = new ReplacementBuilder()
.WithUser(user)
.WithServer(_client, (SocketGuild)user.Guild)
.Build();
var text = SmartText.CreateFrom(conf.DmGreetMessageText);
text = rep.Replace(text);
if (text is SmartPlainText pt)
{
text = new SmartEmbedText() { PlainText = pt.Text };
@@ -227,7 +259,7 @@ public class GreetService : INService
Text = $"This message was sent from {user.Guild} server.", IconUrl = user.Guild.IconUrl
};
await channel.SendAsync(text);
await user.SendAsync(text);
}
catch
{
@@ -277,9 +309,7 @@ public class GreetService : INService
if (conf.SendDmGreetMessage)
{
var channel = await user.CreateDMChannelAsync();
if (channel is not null) await GreetDmUser(conf, channel, user);
await GreetDmUser(conf, user);
}
}
catch
@@ -551,10 +581,10 @@ public class GreetService : INService
return GreetUsers(conf, channel, user);
}
public Task<bool> GreetDmTest(IDMChannel channel, IGuildUser user)
public Task<bool> GreetDmTest(IGuildUser user)
{
var conf = GetOrAddSettingsForGuild(user.GuildId);
return GreetDmUser(conf, channel, user);
return GreetDmUser(conf, user);
}
#endregion