Added .boost, .boostmsg and .boostdel commands which allow you to have customizable messages when someone boosts your server, with auto-deletion support

- Updated response embed colors in greet commands
- Updated .greetmsg and .byemsg command help to match the new .boost command help
This commit is contained in:
Kwoth
2021-09-12 20:44:19 +02:00
parent a27ea7a0aa
commit 8ee1160a00
13 changed files with 2929 additions and 21 deletions

View File

@@ -48,8 +48,51 @@ namespace NadekoBot.Services
bot.JoinedGuild += Bot_JoinedGuild;
_client.LeftGuild += _client_LeftGuild;
_client.GuildMemberUpdated += ClientOnGuildMemberUpdated;
}
private Task ClientOnGuildMemberUpdated(SocketGuildUser oldUser, SocketGuildUser newUser)
{
if (oldUser is { PremiumSince: null } && newUser is { PremiumSince: not null })
{
var conf = GetOrAddSettingsForGuild(newUser.Guild.Id);
if (!conf.SendBoostMessage) return Task.CompletedTask;
_ = Task.Run(TriggerBoostMessage(conf, newUser));
}
return Task.CompletedTask;
}
private Func<Task> TriggerBoostMessage(GreetSettings conf, SocketGuildUser user) => async () =>
{
var channel = user.Guild.GetTextChannel(conf.BoostMessageChannelId);
if (channel is null)
return;
if (string.IsNullOrWhiteSpace(conf.BoostMessage))
return;
var toSend = SmartText.CreateFrom(conf.BoostMessage);
var rep = new ReplacementBuilder()
.WithDefault(user, channel, user.Guild, _client)
.Build();
try
{
var toDelete = await channel.SendAsync(rep.Replace(toSend));
if (conf.BoostMessageDeleteAfter > 0)
{
toDelete.DeleteAfter(conf.BoostMessageDeleteAfter);
}
}
catch (Exception ex)
{
Log.Error(ex, "Error sending boost message.");
}
};
private Task _client_LeftGuild(SocketGuild arg)
{
GuildConfigsCache.TryRemove(arg.Id, out _);
@@ -524,6 +567,48 @@ namespace NadekoBot.Services
await uow.SaveChangesAsync();
}
}
public bool SetBoostMessage(ulong guildId, ref string message)
{
message = message?.SanitizeMentions();
using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set);
conf.BoostMessage = message;
var toAdd = GreetSettings.Create(conf);
GuildConfigsCache.AddOrUpdate(guildId, toAdd,(_, _) => toAdd);
uow.SaveChanges();
return conf.SendBoostMessage;
}
public async Task SetBoostDel(ulong guildId, int timer)
{
if (timer < 0 || timer > 600)
throw new ArgumentOutOfRangeException(nameof(timer));
using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set);
conf.BoostMessageDeleteAfter = timer;
var toAdd = GreetSettings.Create(conf);
GuildConfigsCache.AddOrUpdate(guildId, toAdd,(_, _) => toAdd);
await uow.SaveChangesAsync();
}
public async Task<bool> ToggleBoost(ulong guildId, ulong channelId)
{
using var uow = _db.GetDbContext();
var conf = uow.GuildConfigsForId(guildId, set => set);
conf.SendBoostMessage = !conf.SendBoostMessage;
await uow.SaveChangesAsync();
var toAdd = GreetSettings.Create(conf);
GuildConfigsCache.AddOrUpdate(guildId, toAdd,(_, _) => toAdd);
return conf.SendBoostMessage;
}
}
public class GreetSettings
@@ -542,6 +627,11 @@ namespace NadekoBot.Services
public bool SendChannelByeMessage { get; set; }
public string ChannelByeMessageText { get; set; }
public bool SendBoostMessage { get; set; }
public string BoostMessage { get; set; }
public int BoostMessageDeleteAfter { get; set; }
public ulong BoostMessageChannelId { get; set; }
public static GreetSettings Create(GuildConfig g) => new GreetSettings()
{
@@ -555,6 +645,11 @@ namespace NadekoBot.Services
ChannelGreetMessageText = g.ChannelGreetMessageText,
SendChannelByeMessage = g.SendChannelByeMessage,
ChannelByeMessageText = g.ChannelByeMessageText,
SendBoostMessage = g.SendBoostMessage,
BoostMessage = g.BoostMessage,
BoostMessageDeleteAfter = g.BoostMessageDeleteAfter,
BoostMessageChannelId = g.BoostMessageChannelId
};
}
}

View File

@@ -12,8 +12,6 @@ using NadekoBot.Modules.Administration;
namespace NadekoBot.Services
{
// todo future use guild locale more in the code (from guild settings) (for dates, currency, etc?)
// todo future maybe Write a sourcegen for response strings
// and use const/static fields (maybe even typed to enforce correct number of arguments)
public class Localization : ILocalization, INService
{
private readonly BotConfigService _bss;