More restructuring

This commit is contained in:
Kwoth
2023-03-26 14:44:25 +02:00
parent 01f70f0a24
commit 308ba36b2e
319 changed files with 681 additions and 218 deletions

View File

@@ -1,137 +0,0 @@
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Db;
using NadekoBot.Modules.Utility.Services;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class CommandMapCommands : NadekoModule<AliasService>
{
private readonly DbService _db;
private readonly DiscordSocketClient _client;
public CommandMapCommands(DbService db, DiscordSocketClient client)
{
_db = db;
_client = client;
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
public async Task AliasesClear()
{
var count = _service.ClearAliases(ctx.Guild.Id);
await ReplyConfirmLocalizedAsync(strs.aliases_cleared(count));
}
[Cmd]
[UserPerm(GuildPerm.Administrator)]
[RequireContext(ContextType.Guild)]
public async Task Alias(string trigger, [Leftover] string mapping = null)
{
if (string.IsNullOrWhiteSpace(trigger))
return;
trigger = trigger.Trim().ToLowerInvariant();
if (string.IsNullOrWhiteSpace(mapping))
{
if (!_service.AliasMaps.TryGetValue(ctx.Guild.Id, out var maps) || !maps.TryRemove(trigger, out _))
{
await ReplyErrorLocalizedAsync(strs.alias_remove_fail(Format.Code(trigger)));
return;
}
await using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var tr = config.CommandAliases.FirstOrDefault(x => x.Trigger == trigger);
if (tr is not null)
uow.Set<CommandAlias>().Remove(tr);
uow.SaveChanges();
}
await ReplyConfirmLocalizedAsync(strs.alias_removed(Format.Code(trigger)));
return;
}
_service.AliasMaps.AddOrUpdate(ctx.Guild.Id,
_ =>
{
using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
config.CommandAliases.Add(new()
{
Mapping = mapping,
Trigger = trigger
});
uow.SaveChanges();
}
return new(new Dictionary<string, string>
{
{ trigger.Trim().ToLowerInvariant(), mapping.ToLowerInvariant() }
});
},
(_, map) =>
{
using (var uow = _db.GetDbContext())
{
var config = uow.GuildConfigsForId(ctx.Guild.Id, set => set.Include(x => x.CommandAliases));
var toAdd = new CommandAlias
{
Mapping = mapping,
Trigger = trigger
};
var toRemove = config.CommandAliases.Where(x => x.Trigger == trigger).ToArray();
if (toRemove.Any())
uow.RemoveRange(toRemove);
config.CommandAliases.Add(toAdd);
uow.SaveChanges();
}
map.AddOrUpdate(trigger, mapping, (_, _) => mapping);
return map;
});
await ReplyConfirmLocalizedAsync(strs.alias_added(Format.Code(trigger), Format.Code(mapping)));
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task AliasList(int page = 1)
{
page -= 1;
if (page < 0)
return;
if (!_service.AliasMaps.TryGetValue(ctx.Guild.Id, out var maps) || !maps.Any())
{
await ReplyErrorLocalizedAsync(strs.aliases_none);
return;
}
var arr = maps.ToArray();
await ctx.SendPaginatedConfirmAsync(page,
curPage =>
{
return _eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.alias_list))
.WithDescription(string.Join("\n",
arr.Skip(curPage * 10).Take(10).Select(x => $"`{x.Key}` => `{x.Value}`")));
},
arr.Length,
10);
}
}
}

View File

@@ -1,95 +0,0 @@
#nullable disable
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility.Services;
public class AliasService : IInputTransformer, INService
{
public ConcurrentDictionary<ulong, ConcurrentDictionary<string, string>> AliasMaps { get; } = new();
private readonly IEmbedBuilderService _eb;
private readonly DbService _db;
public AliasService(DiscordSocketClient client, DbService db, IEmbedBuilderService eb)
{
_eb = eb;
using var uow = db.GetDbContext();
var guildIds = client.Guilds.Select(x => x.Id).ToList();
var configs = uow.Set<GuildConfig>()
.Include(gc => gc.CommandAliases)
.Where(x => guildIds.Contains(x.GuildId))
.ToList();
AliasMaps = new(configs.ToDictionary(x => x.GuildId,
x => new ConcurrentDictionary<string, string>(x.CommandAliases.DistinctBy(ca => ca.Trigger)
.ToDictionary(ca => ca.Trigger, ca => ca.Mapping),
StringComparer.OrdinalIgnoreCase)));
_db = db;
}
public int ClearAliases(ulong guildId)
{
AliasMaps.TryRemove(guildId, out _);
int count;
using var uow = _db.GetDbContext();
var gc = uow.GuildConfigsForId(guildId, set => set.Include(x => x.CommandAliases));
count = gc.CommandAliases.Count;
gc.CommandAliases.Clear();
uow.SaveChanges();
return count;
}
public async Task<string> TransformInput(
IGuild guild,
IMessageChannel channel,
IUser user,
string input)
{
if (guild is null || string.IsNullOrWhiteSpace(input))
return null;
if (AliasMaps.TryGetValue(guild.Id, out var maps))
{
string newInput = null;
foreach (var (k, v) in maps)
{
if (string.Equals(input, k, StringComparison.OrdinalIgnoreCase))
{
newInput = v;
}
else if (input.StartsWith(k + ' ', StringComparison.OrdinalIgnoreCase))
{
if (v.Contains("%target%"))
newInput = v.Replace("%target%", input[k.Length..]);
else
newInput = v + ' ' + input[k.Length..];
}
if (newInput is not null)
{
try
{
var toDelete = await channel.SendConfirmAsync(_eb, $"{input} => {newInput}");
toDelete.DeleteAfter(1.5f);
}
catch
{
// ignored
}
return newInput;
}
}
return null;
}
return null;
}
}

View File

@@ -1,48 +0,0 @@
#nullable disable
using NCalc;
using System.Reflection;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class CalcCommands : NadekoModule
{
[Cmd]
public async Task Calculate([Leftover] string expression)
{
var expr = new Expression(expression, EvaluateOptions.IgnoreCase | EvaluateOptions.NoCache);
expr.EvaluateParameter += Expr_EvaluateParameter;
var result = expr.Evaluate();
if (!expr.HasErrors())
await SendConfirmAsync("⚙ " + GetText(strs.result), result.ToString());
else
await SendErrorAsync("⚙ " + GetText(strs.error), expr.Error);
}
private static void Expr_EvaluateParameter(string name, ParameterArgs args)
{
switch (name.ToLowerInvariant())
{
case "pi":
args.Result = Math.PI;
break;
case "e":
args.Result = Math.E;
break;
}
}
[Cmd]
public async Task CalcOps()
{
var selection = typeof(Math).GetTypeInfo()
.GetMethods()
.DistinctBy(x => x.Name)
.Select(x => x.Name)
.Except(new[] { "ToString", "Equals", "GetHashCode", "GetType" });
await SendConfirmAsync(GetText(strs.calcops(prefix)), string.Join(", ", selection));
}
}
}

View File

@@ -1,154 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
public partial class ConfigCommands : NadekoModule
{
private readonly IEnumerable<IConfigService> _settingServices;
public ConfigCommands(IEnumerable<IConfigService> settingServices)
=> _settingServices = settingServices.Where(x => x.Name != "medusa");
[Cmd]
[OwnerOnly]
public async Task ConfigReload(string name)
{
var setting = _settingServices.FirstOrDefault(x
=> x.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase));
if (setting is null)
{
var configNames = _settingServices.Select(x => x.Name);
var embed = _eb.Create()
.WithErrorColor()
.WithDescription(GetText(strs.config_not_found(Format.Code(name))))
.AddField(GetText(strs.config_list), string.Join("\n", configNames));
await ctx.Channel.EmbedAsync(embed);
return;
}
setting.Reload();
await ctx.OkAsync();
}
[Cmd]
[OwnerOnly]
public async Task Config(string name = null, string prop = null, [Leftover] string value = null)
{
var configNames = _settingServices.Select(x => x.Name);
// if name is not provided, print available configs
name = name?.ToLowerInvariant();
if (string.IsNullOrWhiteSpace(name))
{
var embed = _eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.config_list))
.WithDescription(string.Join("\n", configNames));
await ctx.Channel.EmbedAsync(embed);
return;
}
var setting = _settingServices.FirstOrDefault(x
=> x.Name.StartsWith(name, StringComparison.InvariantCultureIgnoreCase));
// if config name is not found, print error and the list of configs
if (setting is null)
{
var embed = _eb.Create()
.WithErrorColor()
.WithDescription(GetText(strs.config_not_found(Format.Code(name))))
.AddField(GetText(strs.config_list), string.Join("\n", configNames));
await ctx.Channel.EmbedAsync(embed);
return;
}
name = setting.Name;
// if prop is not sent, then print the list of all props and values in that config
prop = prop?.ToLowerInvariant();
var propNames = setting.GetSettableProps();
if (string.IsNullOrWhiteSpace(prop))
{
var propStrings = GetPropsAndValuesString(setting, propNames);
var embed = _eb.Create().WithOkColor().WithTitle($"⚙️ {setting.Name}").WithDescription(propStrings);
await ctx.Channel.EmbedAsync(embed);
return;
}
// if the prop is invalid -> print error and list of
var exists = propNames.Any(x => x == prop);
if (!exists)
{
var propStrings = GetPropsAndValuesString(setting, propNames);
var propErrorEmbed = _eb.Create()
.WithErrorColor()
.WithDescription(GetText(
strs.config_prop_not_found(Format.Code(prop), Format.Code(name))))
.AddField($"⚙️ {setting.Name}", propStrings);
await ctx.Channel.EmbedAsync(propErrorEmbed);
return;
}
// if prop is sent, but value is not, then we have to check
// if prop is valid ->
if (string.IsNullOrWhiteSpace(value))
{
value = setting.GetSetting(prop);
if (string.IsNullOrWhiteSpace(value))
value = "-";
if (prop != "currency.sign")
value = Format.Code(Format.Sanitize(value.TrimTo(1000)), "json");
var embed = _eb.Create()
.WithOkColor()
.AddField("Config", Format.Code(setting.Name), true)
.AddField("Prop", Format.Code(prop), true)
.AddField("Value", value);
var comment = setting.GetComment(prop);
if (!string.IsNullOrWhiteSpace(comment))
embed.AddField("Comment", comment);
await ctx.Channel.EmbedAsync(embed);
return;
}
var success = setting.SetSetting(prop, value);
if (!success)
{
await ReplyErrorLocalizedAsync(strs.config_edit_fail(Format.Code(prop), Format.Code(value)));
return;
}
await ctx.OkAsync();
}
private string GetPropsAndValuesString(IConfigService config, IReadOnlyCollection<string> names)
{
var propValues = names.Select(pr =>
{
var val = config.GetSetting(pr);
if (pr != "currency.sign")
val = val?.TrimTo(28);
return val?.Replace("\n", "") ?? "-";
})
.ToList();
var strings = names.Zip(propValues, (name, value) => $"{name,-25} = {value}\n");
return Format.Code(string.Concat(strings), "hs");
}
}
}

View File

@@ -1,216 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Patronage;
using System.Text;
using Nadeko.Common;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class InfoCommands : NadekoModule
{
private readonly DiscordSocketClient _client;
private readonly IStatsService _stats;
private readonly IPatronageService _ps;
public InfoCommands(DiscordSocketClient client, IStatsService stats, IPatronageService ps)
{
_client = client;
_stats = stats;
_ps = ps;
}
[Cmd]
[OwnerOnly]
public Task ServerInfo([Leftover] string guildName)
=> InternalServerInfo(guildName);
[Cmd]
[RequireContext(ContextType.Guild)]
public Task ServerInfo()
=> InternalServerInfo();
private async Task InternalServerInfo(string guildName = null)
{
var channel = (ITextChannel)ctx.Channel;
guildName = guildName?.ToUpperInvariant();
SocketGuild guild;
if (string.IsNullOrWhiteSpace(guildName))
guild = (SocketGuild)channel.Guild;
else
guild = _client.Guilds.FirstOrDefault(g => g.Name.ToUpperInvariant() == guildName.ToUpperInvariant());
if (guild is null)
return;
var ownername = guild.GetUser(guild.OwnerId);
var textchn = guild.TextChannels.Count;
var voicechn = guild.VoiceChannels.Count;
var channels = $@"{GetText(strs.text_channels(textchn))}
{GetText(strs.voice_channels(voicechn))}";
var createdAt = new DateTime(2015, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(guild.Id >> 22);
var features = guild.Features.Value.ToString();
if (string.IsNullOrWhiteSpace(features))
features = "-";
var embed = _eb.Create()
.WithAuthor(GetText(strs.server_info))
.WithTitle(guild.Name)
.AddField(GetText(strs.id), guild.Id.ToString(), true)
.AddField(GetText(strs.owner), ownername.ToString(), true)
.AddField(GetText(strs.members), guild.MemberCount.ToString(), true)
.AddField(GetText(strs.channels), channels, true)
.AddField(GetText(strs.created_at), $"{createdAt:dd.MM.yyyy HH:mm}", true)
.AddField(GetText(strs.roles), (guild.Roles.Count - 1).ToString(), true)
.AddField(GetText(strs.features), features)
.WithOkColor();
if (Uri.IsWellFormedUriString(guild.IconUrl, UriKind.Absolute))
embed.WithThumbnailUrl(guild.IconUrl);
if (guild.Emotes.Any())
{
embed.AddField(GetText(strs.custom_emojis) + $"({guild.Emotes.Count})",
string.Join(" ", guild.Emotes.Shuffle().Take(20).Select(e => $"{e.Name} {e}")).TrimTo(1020));
}
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task ChannelInfo(ITextChannel channel = null)
{
var ch = channel ?? (ITextChannel)ctx.Channel;
if (ch is null)
return;
var createdAt = new DateTime(2015, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc).AddMilliseconds(ch.Id >> 22);
var usercount = (await ch.GetUsersAsync().FlattenAsync()).Count();
var embed = _eb.Create()
.WithTitle(ch.Name)
.WithDescription(ch.Topic?.SanitizeMentions(true))
.AddField(GetText(strs.id), ch.Id.ToString(), true)
.AddField(GetText(strs.created_at), $"{createdAt:dd.MM.yyyy HH:mm}", true)
.AddField(GetText(strs.users), usercount.ToString(), true)
.WithOkColor();
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[RequireUserPermission(GuildPermission.ManageRoles)]
public async Task RoleInfo([Leftover] SocketRole role)
{
if (role.IsEveryone)
return;
var createdAt = new DateTime(2015, 1, 1, 0, 0, 0, 0, DateTimeKind.Utc)
.AddMilliseconds(role.Id >> 22);
var usercount = role.Members.LongCount();
var embed = _eb.Create()
.WithTitle(role.Name.TrimTo(128))
.WithDescription(role.Permissions.ToList().Join(" | "))
.AddField(GetText(strs.id), role.Id.ToString(), true)
.AddField(GetText(strs.created_at), $"{createdAt:dd.MM.yyyy HH:mm}", true)
.AddField(GetText(strs.users), usercount.ToString(), true)
.AddField(GetText(strs.color), $"#{role.Color.R:X2}{role.Color.G:X2}{role.Color.B:X2}", true)
.AddField(GetText(strs.mentionable), role.IsMentionable.ToString(), true)
.AddField(GetText(strs.hoisted), role.IsHoisted.ToString(), true)
.WithOkColor();
if (!string.IsNullOrWhiteSpace(role.GetIconUrl()))
embed = embed.WithThumbnailUrl(role.GetIconUrl());
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task UserInfo(IGuildUser usr = null)
{
var user = usr ?? ctx.User as IGuildUser;
if (user is null)
return;
var embed = _eb.Create().AddField(GetText(strs.name), $"**{user.Username}**#{user.Discriminator}", true);
if (!string.IsNullOrWhiteSpace(user.Nickname))
embed.AddField(GetText(strs.nickname), user.Nickname, true);
var joinedAt = GetJoinedAt(user);
embed.AddField(GetText(strs.id), user.Id.ToString(), true)
.AddField(GetText(strs.joined_server), $"{joinedAt?.ToString("dd.MM.yyyy HH:mm") ?? "?"}", true)
.AddField(GetText(strs.joined_discord), $"{user.CreatedAt:dd.MM.yyyy HH:mm}", true)
.AddField(GetText(strs.roles),
$"**({user.RoleIds.Count - 1})** - {string.Join("\n", user.GetRoles().Take(10).Where(r => r.Id != r.Guild.EveryoneRole.Id).Select(r => r.Name)).SanitizeMentions(true)}",
true)
.WithOkColor();
var patron = await _ps.GetPatronAsync(user.Id);
if (patron.Tier != PatronTier.None)
{
embed.WithFooter(patron.Tier switch
{
PatronTier.V => "❤️❤️",
PatronTier.X => "❤️❤️❤️",
PatronTier.XX => "❤️❤️❤️❤️",
PatronTier.L => "❤️❤️❤️❤️❤️",
_ => "❤️",
});
}
var av = user.RealAvatarUrl();
if (av.IsAbsoluteUri)
embed.WithThumbnailUrl(av.ToString());
await ctx.Channel.EmbedAsync(embed);
}
private DateTimeOffset? GetJoinedAt(IGuildUser user)
{
var joinedAt = user.JoinedAt;
if (user.GuildId != 117523346618318850)
return joinedAt;
if (user.Id == 351244576092192778)
return new DateTimeOffset(2019, 12, 25, 9, 33, 0, TimeSpan.Zero);
return joinedAt;
}
[Cmd]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task Activity(int page = 1)
{
const int activityPerPage = 10;
page -= 1;
if (page < 0)
return;
var startCount = page * activityPerPage;
var str = new StringBuilder();
foreach (var kvp in _cmdHandler.UserMessagesSent.OrderByDescending(kvp => kvp.Value)
.Skip(page * activityPerPage)
.Take(activityPerPage))
{
str.AppendLine(GetText(strs.activity_line(++startCount,
Format.Bold(kvp.Key.ToString()),
kvp.Value / _stats.GetUptime().TotalSeconds,
kvp.Value)));
}
await ctx.Channel.EmbedAsync(_eb.Create()
.WithTitle(GetText(strs.activity_page(page + 1)))
.WithOkColor()
.WithFooter(GetText(
strs.activity_users_total(_cmdHandler.UserMessagesSent.Count)))
.WithDescription(str.ToString()));
}
}
}

View File

@@ -1,93 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class InviteCommands : NadekoModule<InviteService>
{
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(ChannelPerm.CreateInstantInvite)]
[UserPerm(ChannelPerm.CreateInstantInvite)]
[NadekoOptions<InviteService.Options>]
public async Task InviteCreate(params string[] args)
{
var (opts, success) = OptionsParser.ParseFrom(new InviteService.Options(), args);
if (!success)
return;
var ch = (ITextChannel)ctx.Channel;
var invite = await ch.CreateInviteAsync(opts.Expire, opts.MaxUses, opts.Temporary, opts.Unique);
await SendConfirmAsync($"{ctx.User.Mention} https://discord.gg/{invite.Code}");
}
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(ChannelPerm.ManageChannels)]
[UserPerm(ChannelPerm.ManageChannels)]
public async Task InviteList(int page = 1, [Leftover] ITextChannel ch = null)
{
if (--page < 0)
return;
var channel = ch ?? (ITextChannel)ctx.Channel;
var invites = await channel.GetInvitesAsync();
await ctx.SendPaginatedConfirmAsync(page,
cur =>
{
var i = 1;
var invs = invites.Skip(cur * 9).Take(9).ToList();
if (!invs.Any())
return _eb.Create().WithErrorColor().WithDescription(GetText(strs.no_invites));
var embed = _eb.Create().WithOkColor();
foreach (var inv in invites)
{
var expiryString = inv.MaxAge is null or 0 || inv.CreatedAt is null
? "∞"
: (inv.CreatedAt.Value.AddSeconds(inv.MaxAge.Value).UtcDateTime - DateTime.UtcNow).ToString(
"""d\.hh\:mm\:ss""");
var creator = inv.Inviter.ToString().TrimTo(25);
var usesString = $"{inv.Uses} / {(inv.MaxUses == 0 ? "" : inv.MaxUses?.ToString())}";
var desc = $@"`{GetText(strs.inv_uses)}` **{usesString}**
`{GetText(strs.inv_expire)}` **{expiryString}**
{inv.Url} ";
embed.AddField($"#{i++} {creator}", desc);
}
return embed;
},
invites.Count,
9);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(ChannelPerm.ManageChannels)]
[UserPerm(ChannelPerm.ManageChannels)]
public async Task InviteDelete(int index)
{
if (--index < 0)
return;
var ch = (ITextChannel)ctx.Channel;
var invites = await ch.GetInvitesAsync();
if (invites.Count <= index)
return;
var inv = invites.ElementAt(index);
await inv.DeleteAsync();
await ReplyAsync(GetText(strs.invite_deleted(Format.Bold(inv.Code))));
}
}
}

View File

@@ -1,48 +0,0 @@
#nullable disable
using CommandLine;
namespace NadekoBot.Modules.Utility.Services;
public class InviteService : INService
{
public class Options : INadekoCommandOptions
{
[Option('m',
"max-uses",
Required = false,
Default = 0,
HelpText = "Maximum number of times the invite can be used. Default 0 (never).")]
public int MaxUses { get; set; }
[Option('u',
"unique",
Required = false,
Default = false,
HelpText =
"Not setting this flag will result in bot getting the existing invite with the same settings if it exists, instead of creating a new one.")]
public bool Unique { get; set; } = false;
[Option('t',
"temporary",
Required = false,
Default = false,
HelpText = "If this flag is set, the user will be kicked from the guild once they close their client.")]
public bool Temporary { get; set; } = false;
[Option('e',
"expire",
Required = false,
Default = 0,
HelpText = "Time in seconds to expire the invite. Default 0 (no expiry).")]
public int Expire { get; set; }
public void NormalizeOptions()
{
if (MaxUses < 0)
MaxUses = 0;
if (Expire < 0)
Expire = 0;
}
}
}

View File

@@ -1,36 +0,0 @@
using NadekoBot.Common.Configs;
namespace NadekoBot.Modules.Utility.Patronage;
public class PatronageConfig : ConfigServiceBase<PatronConfigData>
{
public override string Name
=> "patron";
private static readonly TypedKey<PatronConfigData> _changeKey
= new TypedKey<PatronConfigData>("config.patron.updated");
private const string FILE_PATH = "data/patron.yml";
public PatronageConfig(IConfigSeria serializer, IPubSub pubSub) : base(FILE_PATH, serializer, pubSub, _changeKey)
{
AddParsedProp("enabled",
x => x.IsEnabled,
bool.TryParse,
ConfigPrinters.ToString);
Migrate();
}
private void Migrate()
{
ModifyConfig(c =>
{
if (c.Version == 1)
{
c.Version = 2;
c.IsEnabled = false;
}
});
}
}

View File

@@ -1,199 +0,0 @@
#nullable disable
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Modules.Utility.Patronage;
using NadekoBot.Modules.Gambling.Bank;
using NadekoBot.Modules.Gambling.Services;
using NadekoBot.Services.Currency;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility;
public class CurrencyRewardService : INService, IDisposable
{
private readonly ICurrencyService _cs;
private readonly IPatronageService _ps;
private readonly DbService _db;
private readonly IBankService _bs;
private readonly IEmbedBuilderService _eb;
private readonly GamblingConfigService _config;
private readonly DiscordSocketClient _client;
public CurrencyRewardService(
ICurrencyService cs,
IPatronageService ps,
DbService db,
IBankService bs,
IEmbedBuilderService eb,
GamblingConfigService config,
DiscordSocketClient client)
{
_cs = cs;
_ps = ps;
_db = db;
_bs = bs;
_eb = eb;
_config = config;
_client = client;
_ps.OnNewPatronPayment += OnNewPayment;
_ps.OnPatronRefunded += OnPatronRefund;
_ps.OnPatronUpdated += OnPatronUpdate;
}
public void Dispose()
{
_ps.OnNewPatronPayment -= OnNewPayment;
_ps.OnPatronRefunded -= OnPatronRefund;
_ps.OnPatronUpdated -= OnPatronUpdate;
}
private async Task OnPatronUpdate(Patron oldPatron, Patron newPatron)
{
// if pledge was increased
if (oldPatron.Amount < newPatron.Amount)
{
var conf = _config.Data;
var newAmount = (long)(newPatron.Amount * conf.PatreonCurrencyPerCent);
RewardedUser old;
await using (var ctx = _db.GetDbContext())
{
old = await ctx.GetTable<RewardedUser>()
.Where(x => x.PlatformUserId == newPatron.UniquePlatformUserId)
.FirstOrDefaultAsync();
if (old is null)
{
await OnNewPayment(newPatron);
return;
}
// no action as the amount is the same or lower
if (old.AmountRewardedThisMonth >= newAmount)
return;
var count = await ctx.GetTable<RewardedUser>()
.Where(x => x.PlatformUserId == newPatron.UniquePlatformUserId)
.UpdateAsync(_ => new()
{
PlatformUserId = newPatron.UniquePlatformUserId,
UserId = newPatron.UserId,
// amount before bonuses
AmountRewardedThisMonth = newAmount,
LastReward = newPatron.PaidAt
});
// shouldn't ever happen
if (count == 0)
return;
}
var oldAmount = old.AmountRewardedThisMonth;
var realNewAmount = GetRealCurrencyReward(
(int)(newAmount / conf.PatreonCurrencyPerCent),
newAmount,
out var percentBonus);
var realOldAmount = GetRealCurrencyReward(
(int)(oldAmount / conf.PatreonCurrencyPerCent),
oldAmount,
out _);
var diff = realNewAmount - realOldAmount;
if (diff <= 0)
return; // no action if new is lower
// if the user pledges 5$ or more, they will get X % more flowers where X is amount in dollars,
// up to 100%
await _cs.AddAsync(newPatron.UserId, diff, new TxData("patron","update"));
_ = SendMessageToUser(newPatron.UserId,
$"You've received an additional **{diff}**{_config.Data.Currency.Sign} as a currency reward (+{percentBonus}%)!");
}
}
private long GetRealCurrencyReward(int pledgeCents, long modifiedAmount, out int percentBonus)
{
// needs at least 5$ to be eligible for a bonus
if (pledgeCents < 500)
{
percentBonus = 0;
return modifiedAmount;
}
var dollarValue = pledgeCents / 100;
percentBonus = dollarValue switch
{
>= 100 => 100,
>= 50 => 50,
>= 20 => 20,
>= 10 => 10,
>= 5 => 5,
_ => 0
};
return (long)(modifiedAmount * (1 + (percentBonus / 100.0f)));
}
// on a new payment, always give the full amount.
private async Task OnNewPayment(Patron patron)
{
var amount = (long)(patron.Amount * _config.Data.PatreonCurrencyPerCent);
await using var ctx = _db.GetDbContext();
await ctx.GetTable<RewardedUser>()
.InsertOrUpdateAsync(() => new()
{
PlatformUserId = patron.UniquePlatformUserId,
UserId = patron.UserId,
AmountRewardedThisMonth = amount,
LastReward = patron.PaidAt,
},
old => new()
{
AmountRewardedThisMonth = amount,
UserId = patron.UserId,
LastReward = patron.PaidAt
},
() => new()
{
PlatformUserId = patron.UniquePlatformUserId
});
var realAmount = GetRealCurrencyReward(patron.Amount, amount, out var percentBonus);
await _cs.AddAsync(patron.UserId, realAmount, new("patron", "new"));
_ = SendMessageToUser(patron.UserId,
$"You've received **{realAmount}**{_config.Data.Currency.Sign} as a currency reward (**+{percentBonus}%**)!");
}
private async Task SendMessageToUser(ulong userId, string message)
{
try
{
var user = (IUser)_client.GetUser(userId) ?? await _client.Rest.GetUserAsync(userId);
if (user is null)
return;
var eb = _eb.Create()
.WithOkColor()
.WithDescription(message);
await user.EmbedAsync(eb);
}
catch
{
Log.Warning("Unable to send a \"Currency Reward\" message to the patron {UserId}", userId);
}
}
private async Task OnPatronRefund(Patron patron)
{
await using var ctx = _db.GetDbContext();
_ = await ctx.GetTable<RewardedUser>()
.UpdateAsync(old => new()
{
AmountRewardedThisMonth = old.AmountRewardedThisMonth * 2
});
}
}

View File

@@ -1,11 +0,0 @@
using NadekoBot.Db.Models;
namespace NadekoBot.Modules.Utility.Patronage;
public readonly struct InsufficientTier
{
public FeatureType FeatureType { get; init; }
public string Feature { get; init; }
public PatronTier RequiredTier { get; init; }
public PatronTier UserTier { get; init; }
}

View File

@@ -1,150 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Common.Patreon;
using OneOf;
using OneOf.Types;
using System.Net.Http.Json;
using System.Text.Json;
namespace NadekoBot.Modules.Utility;
public class PatreonClient : IDisposable
{
private readonly string _clientId;
private readonly string _clientSecret;
private string refreshToken;
private string accessToken = string.Empty;
private readonly HttpClient _http;
private DateTime refreshAt = DateTime.UtcNow;
public PatreonClient(string clientId, string clientSecret, string refreshToken)
{
_clientId = clientId;
_clientSecret = clientSecret;
this.refreshToken = refreshToken;
_http = new();
}
public void Dispose()
=> _http.Dispose();
public PatreonCredentials GetCredentials()
=> new PatreonCredentials()
{
AccessToken = accessToken,
ClientId = _clientId,
ClientSecret = _clientSecret,
RefreshToken = refreshToken,
};
public async Task<OneOf<Success, Error<string>>> RefreshTokenAsync(bool force)
{
if (!force && IsTokenValid())
return new Success();
var res = await _http.PostAsync("https://www.patreon.com/api/oauth2/token"
+ "?grant_type=refresh_token"
+ $"&refresh_token={refreshToken}"
+ $"&client_id={_clientId}"
+ $"&client_secret={_clientSecret}",
null);
if (!res.IsSuccessStatusCode)
return new Error<string>($"Request did not return a sucess status code. Status code: {res.StatusCode}");
try
{
var data = await res.Content.ReadFromJsonAsync<PatreonRefreshData>();
if (data is null)
return new Error<string>($"Invalid data retrieved from Patreon.");
refreshToken = data.RefreshToken;
accessToken = data.AccessToken;
refreshAt = DateTime.UtcNow.AddSeconds(data.ExpiresIn - 5.Minutes().TotalSeconds);
return new Success();
}
catch (Exception ex)
{
return new Error<string>($"Error during deserialization: {ex.Message}");
}
}
private async ValueTask<bool> EnsureTokenValidAsync()
{
if (!IsTokenValid())
{
var res = await RefreshTokenAsync(true);
return res.Match(
static _ => true,
static err =>
{
Log.Warning("Error getting token: {ErrorMessage}", err.Value);
return false;
});
}
return true;
}
private bool IsTokenValid()
=> refreshAt > DateTime.UtcNow && !string.IsNullOrWhiteSpace(accessToken);
public async Task<OneOf<IAsyncEnumerable<IReadOnlyCollection<PatreonMemberData>>, Error<string>>> GetMembersAsync(string campaignId)
{
if (!await EnsureTokenValidAsync())
return new Error<string>("Unable to get patreon token");
return OneOf<IAsyncEnumerable<IReadOnlyCollection<PatreonMemberData>>, Error<string>>.FromT0(
GetMembersInternalAsync(campaignId));
}
private async IAsyncEnumerable<IReadOnlyCollection<PatreonMemberData>> GetMembersInternalAsync(string campaignId)
{
_http.DefaultRequestHeaders.Clear();
_http.DefaultRequestHeaders.TryAddWithoutValidation("Authorization",
$"Bearer {accessToken}");
var page =
$"https://www.patreon.com/api/oauth2/v2/campaigns/{campaignId}/members"
+ $"?fields%5Bmember%5D=full_name,currently_entitled_amount_cents,last_charge_date,last_charge_status"
+ $"&fields%5Buser%5D=social_connections"
+ $"&include=user"
+ $"&sort=-last_charge_date";
PatreonMembersResponse data;
do
{
var res = await _http.GetStreamAsync(page);
data = await JsonSerializer.DeserializeAsync<PatreonMembersResponse>(res);
if (data is null)
break;
var userData = data.Data
.Join(data.Included,
static m => m.Relationships.User.Data.Id,
static u => u.Id,
static (m, u) => new PatreonMemberData()
{
PatreonUserId = m.Relationships.User.Data.Id,
UserId = ulong.TryParse(
u.Attributes?.SocialConnections?.Discord?.UserId ?? string.Empty,
out var userId)
? userId
: 0,
EntitledToCents = m.Attributes.CurrentlyEntitledAmountCents,
LastChargeDate = m.Attributes.LastChargeDate,
LastChargeStatus = m.Attributes.LastChargeStatus
})
.ToArray();
yield return userData;
} while (!string.IsNullOrWhiteSpace(page = data.Links?.Next));
}
}

View File

@@ -1,10 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility;
public readonly struct PatreonCredentials
{
public string ClientId { get; init; }
public string ClientSecret { get; init; }
public string AccessToken { get; init; }
public string RefreshToken { get; init; }
}

View File

@@ -1,33 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility;
public sealed class PatreonMemberData : ISubscriberData
{
public string PatreonUserId { get; init; }
public ulong UserId { get; init; }
public DateTime? LastChargeDate { get; init; }
public string LastChargeStatus { get; init; }
public int EntitledToCents { get; init; }
public string UniquePlatformUserId
=> PatreonUserId;
ulong ISubscriberData.UserId
=> UserId;
public int Cents
=> EntitledToCents;
public DateTime? LastCharge
=> LastChargeDate;
public SubscriptionChargeStatus ChargeStatus
=> LastChargeStatus switch
{
"Paid" => SubscriptionChargeStatus.Paid,
"Fraud" or "Refunded" => SubscriptionChargeStatus.Refunded,
"Declined" or "Pending" => SubscriptionChargeStatus.Unpaid,
_ => SubscriptionChargeStatus.Other,
};
}
public sealed class PatreonPledgeData
{
}

View File

@@ -1,22 +0,0 @@
#nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot.Modules.Utility;
public sealed class PatreonRefreshData
{
[JsonPropertyName("access_token")]
public string AccessToken { get; set; }
[JsonPropertyName("refresh_token")]
public string RefreshToken { get; set; }
[JsonPropertyName("expires_in")]
public long ExpiresIn { get; set; }
[JsonPropertyName("scope")]
public string Scope { get; set; }
[JsonPropertyName("token_type")]
public string TokenType { get; set; }
}

View File

@@ -1,81 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Patronage;
namespace NadekoBot.Modules.Utility;
/// <summary>
/// Service tasked with handling pledges on patreon
/// </summary>
public sealed class PatreonSubscriptionHandler : ISubscriptionHandler, INService
{
private readonly IBotCredsProvider _credsProvider;
private readonly PatreonClient _patreonClient;
public PatreonSubscriptionHandler(IBotCredsProvider credsProvider)
{
_credsProvider = credsProvider;
var botCreds = credsProvider.GetCreds();
_patreonClient = new PatreonClient(botCreds.Patreon.ClientId, botCreds.Patreon.ClientSecret, botCreds.Patreon.RefreshToken);
}
public async IAsyncEnumerable<IReadOnlyCollection<ISubscriberData>> GetPatronsAsync()
{
var botCreds = _credsProvider.GetCreds();
if (string.IsNullOrWhiteSpace(botCreds.Patreon.CampaignId)
|| string.IsNullOrWhiteSpace(botCreds.Patreon.ClientId)
|| string.IsNullOrWhiteSpace(botCreds.Patreon.ClientSecret)
|| string.IsNullOrWhiteSpace(botCreds.Patreon.RefreshToken))
yield break;
var result = await _patreonClient.RefreshTokenAsync(false);
if (!result.TryPickT0(out _, out var error))
{
Log.Warning("Unable to refresh patreon token: {ErrorMessage}", error.Value);
yield break;
}
var patreonCreds = _patreonClient.GetCredentials();
_credsProvider.ModifyCredsFile(c =>
{
c.Patreon.AccessToken = patreonCreds.AccessToken;
c.Patreon.RefreshToken = patreonCreds.RefreshToken;
});
IAsyncEnumerable<IEnumerable<ISubscriberData>> data;
try
{
var maybeUserData = await _patreonClient.GetMembersAsync(botCreds.Patreon.CampaignId);
data = maybeUserData.Match(
static userData => userData,
static err =>
{
Log.Warning("Error while getting patreon members: {ErrorMessage}", err.Value);
return AsyncEnumerable.Empty<IReadOnlyCollection<ISubscriberData>>();
});
}
catch (Exception ex)
{
Log.Warning(ex,
"Unexpected error while refreshing patreon members: {ErroMessage}",
ex.Message);
yield break;
}
var now = DateTime.UtcNow;
var firstOfThisMonth = new DateOnly(now.Year, now.Month, 1);
await foreach (var batch in data)
{
// send only active patrons
var toReturn = batch.Where(x => x.Cents > 0
&& x.LastCharge is { } lc
&& lc.ToUniversalTime().ToDateOnly() >= firstOfThisMonth)
.ToArray();
if (toReturn.Length > 0)
yield return toReturn;
}
}
}

View File

@@ -1,153 +0,0 @@
using NadekoBot.Modules.Utility.Patronage;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[OnlyPublicBot]
public partial class PatronageCommands : NadekoModule
{
private readonly PatronageService _service;
private readonly PatronageConfig _pConf;
public PatronageCommands(PatronageService service, PatronageConfig pConf)
{
_service = service;
_pConf = pConf;
}
[Cmd]
[Priority(2)]
public Task Patron()
=> InternalPatron(ctx.User);
[Cmd]
[Priority(0)]
[OwnerOnly]
public Task Patron(IUser user)
=> InternalPatron(user);
[Cmd]
[Priority(0)]
[OwnerOnly]
public async Task PatronMessage(PatronTier tierAndHigher, string message)
{
_ = ctx.Channel.TriggerTypingAsync();
var result = await _service.SendMessageToPatronsAsync(tierAndHigher, message);
await ReplyConfirmLocalizedAsync(strs.patron_msg_sent(
Format.Code(tierAndHigher.ToString()),
Format.Bold(result.Success.ToString()),
Format.Bold(result.Failed.ToString())));
}
// [Cmd]
// [OwnerOnly]
// public async Task PatronGift(IUser user, int amount)
// {
// // i can't figure out a good way to gift more than one month at the moment.
//
// if (amount < 1)
// return;
//
// var patron = _service.GiftPatronAsync(user, amount);
//
// var eb = _eb.Create(ctx);
//
// await ctx.Channel.EmbedAsync(eb.WithDescription($"Added **{days}** days of Patron benefits to {user.Mention}!")
// .AddField("Tier", Format.Bold(patron.Tier.ToString()), true)
// .AddField("Amount", $"**{patron.Amount / 100.0f:N1}$**", true)
// .AddField("Until", TimestampTag.FromDateTime(patron.ValidThru.AddDays(1))));
//
//
// }
private async Task InternalPatron(IUser user)
{
if (!_pConf.Data.IsEnabled)
{
await ReplyErrorLocalizedAsync(strs.patron_not_enabled);
return;
}
var patron = await _service.GetPatronAsync(user.Id);
var quotaStats = await _service.GetUserQuotaStatistic(user.Id);
var eb = _eb.Create(ctx)
.WithAuthor(user)
.WithTitle(GetText(strs.patron_info))
.WithOkColor();
if (quotaStats.Commands.Count == 0
&& quotaStats.Groups.Count == 0
&& quotaStats.Modules.Count == 0)
{
eb.WithDescription(GetText(strs.no_quota_found));
}
else
{
eb.AddField(GetText(strs.tier), Format.Bold(patron.Tier.ToFullName()), true)
.AddField(GetText(strs.pledge), $"**{patron.Amount / 100.0f:N1}$**", true);
if (patron.Tier != PatronTier.None)
eb.AddField(GetText(strs.expires), patron.ValidThru.AddDays(1).ToShortAndRelativeTimestampTag(), true);
eb.AddField(GetText(strs.quotas), "", false);
if (quotaStats.Commands.Count > 0)
{
var text = GetQuotaList(quotaStats.Commands);
if (!string.IsNullOrWhiteSpace(text))
eb.AddField(GetText(strs.commands), text, true);
}
if (quotaStats.Groups.Count > 0)
{
var text = GetQuotaList(quotaStats.Groups);
if (!string.IsNullOrWhiteSpace(text))
eb.AddField(GetText(strs.groups), text, true);
}
if (quotaStats.Modules.Count > 0)
{
var text = GetQuotaList(quotaStats.Modules);
if (!string.IsNullOrWhiteSpace(text))
eb.AddField(GetText(strs.modules), text, true);
}
}
try
{
await ctx.User.EmbedAsync(eb);
_ = ctx.OkAsync();
}
catch
{
await ReplyErrorLocalizedAsync(strs.cant_dm);
}
}
private string GetQuotaList(IReadOnlyDictionary<string, FeatureQuotaStats> featureQuotaStats)
{
var text = string.Empty;
foreach (var (key, q) in featureQuotaStats)
{
text += $"\n\t`{key}`\n";
if (q.Hourly != default)
text += $" {GetEmoji(q.Hourly)} {q.Hourly.Cur}/{q.Hourly.Max} per hour\n";
if (q.Daily != default)
text += $" {GetEmoji(q.Daily)} {q.Daily.Cur}/{q.Daily.Max} per day\n";
if (q.Monthly != default)
text += $" {GetEmoji(q.Monthly)} {q.Monthly.Cur}/{q.Monthly.Max} per month\n";
}
return text;
}
private string GetEmoji((uint Cur, uint Max) limit)
=> limit.Cur < limit.Max
? "✅"
: "⚠️";
}
}

View File

@@ -1,838 +0,0 @@
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db.Models;
using OneOf;
using OneOf.Types;
using StackExchange.Redis;
using CommandInfo = Discord.Commands.CommandInfo;
namespace NadekoBot.Modules.Utility.Patronage;
/// <inheritdoc cref="IPatronageService"/>
public sealed class PatronageService
: IPatronageService,
IReadyExecutor,
IExecPreCommand,
INService
{
public event Func<Patron, Task> OnNewPatronPayment = static delegate { return Task.CompletedTask; };
public event Func<Patron, Patron, Task> OnPatronUpdated = static delegate { return Task.CompletedTask; };
public event Func<Patron, Task> OnPatronRefunded = static delegate { return Task.CompletedTask; };
// this has to run right before the command
public int Priority
=> int.MinValue;
private static readonly PatronTier[] _tiers = Enum.GetValues<PatronTier>();
private readonly PatronageConfig _pConf;
private readonly DbService _db;
private readonly DiscordSocketClient _client;
private readonly ISubscriptionHandler _subsHandler;
private readonly IEmbedBuilderService _eb;
private static readonly TypedKey<long> _quotaKey
= new($"quota:last_hourly_reset");
private readonly IBotCache _cache;
private readonly IBotCredsProvider _creds;
public PatronageService(
PatronageConfig pConf,
DbService db,
DiscordSocketClient client,
ISubscriptionHandler subsHandler,
IEmbedBuilderService eb,
IBotCache cache,
IBotCredsProvider creds)
{
_pConf = pConf;
_db = db;
_client = client;
_subsHandler = subsHandler;
_eb = eb;
_cache = cache;
_creds = creds;
}
public Task OnReadyAsync()
{
if (_client.ShardId != 0)
return Task.CompletedTask;
return Task.WhenAll(ResetLoopAsync(), LoadSubscribersLoopAsync());
}
private async Task LoadSubscribersLoopAsync()
{
var timer = new PeriodicTimer(TimeSpan.FromSeconds(60));
while (await timer.WaitForNextTickAsync())
{
try
{
if (!_pConf.Data.IsEnabled)
continue;
await foreach (var batch in _subsHandler.GetPatronsAsync())
{
await ProcesssPatronsAsync(batch);
}
}
catch (Exception ex)
{
Log.Error(ex, "Error processing patrons");
}
}
}
public async Task ResetLoopAsync()
{
await Task.Delay(1.Minutes());
while (true)
{
try
{
if (!_pConf.Data.IsEnabled)
{
await Task.Delay(1.Minutes());
continue;
}
var now = DateTime.UtcNow;
var lastRun = DateTime.MinValue;
var result = await _cache.GetAsync(_quotaKey);
if (result.TryGetValue(out var lastVal) && lastVal != default)
{
lastRun = DateTime.FromBinary(lastVal);
}
var nowDate = now.ToDateOnly();
var lastDate = lastRun.ToDateOnly();
await using var ctx = _db.GetDbContext();
await using var tran = await ctx.Database.BeginTransactionAsync();
if ((lastDate.Day == 1 || (lastDate.Month != nowDate.Month)) && nowDate.Day > 1)
{
// assumes bot won't be offline for a year
await ctx.GetTable<PatronQuota>()
.TruncateAsync();
}
else if (nowDate.DayNumber != lastDate.DayNumber)
{
// day is different, means hour is different.
// reset both hourly and daily quota counts.
await ctx.GetTable<PatronQuota>()
.UpdateAsync((old) => new()
{
HourlyCount = 0,
DailyCount = 0,
});
}
else if (now.Hour != lastRun.Hour) // if it's not, just reset hourly quotas
{
await ctx.GetTable<PatronQuota>()
.UpdateAsync((old) => new()
{
HourlyCount = 0
});
}
// assumes that the code above runs in less than an hour
await _cache.AddAsync(_quotaKey, now.ToBinary());
await tran.CommitAsync();
}
catch (Exception ex)
{
Log.Error(ex, "Error in quota reset loop. Message: {ErrorMessage}", ex.Message);
}
await Task.Delay(TimeSpan.FromHours(1).Add(TimeSpan.FromMinutes(1)));
}
}
private async Task ProcesssPatronsAsync(IReadOnlyCollection<ISubscriberData> subscribersEnum)
{
// process only users who have discord accounts connected
var subscribers = subscribersEnum.Where(x => x.UserId != 0).ToArray();
if (subscribers.Length == 0)
return;
var todayDate = DateTime.UtcNow.Date;
await using var ctx = _db.GetDbContext();
// handle paid users
foreach (var subscriber in subscribers.Where(x => x.ChargeStatus == SubscriptionChargeStatus.Paid))
{
if (subscriber.LastCharge is null)
continue;
var lastChargeUtc = subscriber.LastCharge.Value.ToUniversalTime();
var dateInOneMonth = lastChargeUtc.Date.AddMonths(1);
// await using var tran = await ctx.Database.BeginTransactionAsync();
try
{
var dbPatron = await ctx.GetTable<PatronUser>()
.FirstOrDefaultAsync(x
=> x.UniquePlatformUserId == subscriber.UniquePlatformUserId);
if (dbPatron is null)
{
// if the user is not in the database alrady
dbPatron = await ctx.GetTable<PatronUser>()
.InsertWithOutputAsync(() => new()
{
UniquePlatformUserId = subscriber.UniquePlatformUserId,
UserId = subscriber.UserId,
AmountCents = subscriber.Cents,
LastCharge = lastChargeUtc,
ValidThru = dateInOneMonth,
});
// await tran.CommitAsync();
var newPatron = PatronUserToPatron(dbPatron);
_ = SendWelcomeMessage(newPatron);
await OnNewPatronPayment(newPatron);
}
else
{
if (dbPatron.LastCharge.Month < lastChargeUtc.Month || dbPatron.LastCharge.Year < lastChargeUtc.Year)
{
// user is charged again for this month
// if his sub would end in teh future, extend it by one month.
// if it's not, just add 1 month to the last charge date
var count = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == subscriber.UniquePlatformUserId)
.UpdateAsync(old => new()
{
UserId = subscriber.UserId,
AmountCents = subscriber.Cents,
LastCharge = lastChargeUtc,
ValidThru = old.ValidThru >= todayDate
// ? Sql.DateAdd(Sql.DateParts.Month, 1, old.ValidThru).Value
? old.ValidThru.AddMonths(1)
: dateInOneMonth,
});
// this should never happen
if (count == 0)
{
// await tran.RollbackAsync();
continue;
}
// await tran.CommitAsync();
await OnNewPatronPayment(PatronUserToPatron(dbPatron));
}
else if (dbPatron.AmountCents != subscriber.Cents // if user changed the amount
|| dbPatron.UserId != subscriber.UserId) // if user updated user id)
{
var cents = subscriber.Cents;
// the user updated the pledge or changed the connected discord account
await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == subscriber.UniquePlatformUserId)
.UpdateAsync(old => new()
{
UserId = subscriber.UserId,
AmountCents = cents,
LastCharge = lastChargeUtc,
ValidThru = old.ValidThru,
});
var newPatron = dbPatron.Clone();
newPatron.AmountCents = cents;
newPatron.UserId = subscriber.UserId;
// idk what's going on but UpdateWithOutputAsync doesn't work properly here
// nor does firstordefault after update. I'm not seeing something obvious
await OnPatronUpdated(
PatronUserToPatron(dbPatron),
PatronUserToPatron(newPatron));
}
}
}
catch (Exception ex)
{
Log.Error(ex,
"Unexpected error occured while processing rewards for patron {UserId}",
subscriber.UserId);
}
}
var expiredDate = DateTime.MinValue;
foreach (var patron in subscribers.Where(x => x.ChargeStatus == SubscriptionChargeStatus.Refunded))
{
// if the subscription is refunded, Disable user's valid thru
var changedCount = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == patron.UniquePlatformUserId
&& x.ValidThru != expiredDate)
.UpdateAsync(old => new()
{
ValidThru = expiredDate
});
if (changedCount == 0)
continue;
var updated = await ctx.GetTable<PatronUser>()
.Where(x => x.UniquePlatformUserId == patron.UniquePlatformUserId)
.FirstAsync();
await OnPatronRefunded(PatronUserToPatron(updated));
}
}
public async Task<bool> ExecPreCommandAsync(ICommandContext ctx,
string moduleName,
CommandInfo command)
{
var ownerId = ctx.Guild?.OwnerId ?? 0;
var result = await AttemptRunCommand(
ctx.User.Id,
ownerId: ownerId,
command.Aliases.First().ToLowerInvariant(),
command.Module.Parent == null ? string.Empty : command.Module.GetGroupName().ToLowerInvariant(),
moduleName.ToLowerInvariant()
);
return result.Match(
_ => false,
ins =>
{
var eb = _eb.Create(ctx)
.WithPendingColor()
.WithTitle("Insufficient Patron Tier")
.AddField("For", $"{ins.FeatureType}: `{ins.Feature}`", true)
.AddField("Required Tier",
$"[{ins.RequiredTier.ToFullName()}](https://patreon.com/join/nadekobot)",
true);
if (ctx.Guild is null || ctx.Guild?.OwnerId == ctx.User.Id)
eb.WithDescription("You don't have the sufficent Patron Tier to run this command.")
.WithFooter("You can use '.patron' and '.donate' commands for more info");
else
eb.WithDescription(
"Neither you nor the server owner have the sufficent Patron Tier to run this command.")
.WithFooter("You can use '.patron' and '.donate' commands for more info");
_ = ctx.WarningAsync();
if (ctx.Guild?.OwnerId == ctx.User.Id)
_ = ctx.Channel.EmbedAsync(eb);
else
_ = ctx.User.EmbedAsync(eb);
return true;
},
quota =>
{
var eb = _eb.Create(ctx)
.WithPendingColor()
.WithTitle("Quota Limit Reached");
if (quota.IsOwnQuota || ctx.User.Id == ownerId)
{
eb.WithDescription($"You've reached your quota of `{quota.Quota} {quota.QuotaPeriod.ToFullName()}`")
.WithFooter("You may want to check your quota by using the '.patron' command.");
}
else
{
eb.WithDescription(
$"This server reached the quota of {quota.Quota} `{quota.QuotaPeriod.ToFullName()}`")
.WithFooter("You may contact the server owner about this issue.\n"
+ "Alternatively, you can become patron yourself by using the '.donate' command.\n"
+ "If you're already a patron, it means you've reached your quota.\n"
+ "You can use '.patron' command to check your quota status.");
}
eb.AddField("For", $"{quota.FeatureType}: `{quota.Feature}`", true)
.AddField("Resets At", quota.ResetsAt.ToShortAndRelativeTimestampTag(), true);
_ = ctx.WarningAsync();
// send the message in the server in case it's the owner
if (ctx.Guild?.OwnerId == ctx.User.Id)
_ = ctx.Channel.EmbedAsync(eb);
else
_ = ctx.User.EmbedAsync(eb);
return true;
});
}
private async ValueTask<OneOf<OneOf.Types.Success, InsufficientTier, QuotaLimit>> AttemptRunCommand(
ulong userId,
ulong ownerId,
string commandName,
string groupName,
string moduleName)
{
// try to run as a user
var res = await AttemptRunCommand(userId, commandName, groupName, moduleName, true);
// if it fails, try to run as an owner
// but only if the command is ran in a server
// and if the owner is not the user
if (!res.IsT0 && ownerId != 0 && ownerId != userId)
res = await AttemptRunCommand(ownerId, commandName, groupName, moduleName, false);
return res;
}
/// <summary>
/// Returns either the current usage counter if limit wasn't reached, or QuotaLimit if it is.
/// </summary>
public async ValueTask<OneOf<(uint Hourly, uint Daily, uint Monthly), QuotaLimit>> TryIncrementQuotaCounterAsync(ulong userId,
bool isSelf,
FeatureType featureType,
string featureName,
uint? maybeHourly,
uint? maybeDaily,
uint? maybeMonthly)
{
await using var ctx = _db.GetDbContext();
var now = DateTime.UtcNow;
await using var tran = await ctx.Database.BeginTransactionAsync();
var userQuotaData = await ctx.GetTable<PatronQuota>()
.FirstOrDefaultAsyncLinqToDB(x => x.UserId == userId
&& x.Feature == featureName)
?? new PatronQuota();
// if hourly exists, if daily exists, etc...
if (maybeHourly is uint hourly && userQuotaData.HourlyCount >= hourly)
{
return new QuotaLimit()
{
QuotaPeriod = QuotaPer.PerHour,
Quota = hourly,
// quite a neat trick. https://stackoverflow.com/a/5733560
ResetsAt = now.Date.AddHours(now.Hour + 1),
Feature = featureName,
FeatureType = featureType,
IsOwnQuota = isSelf
};
}
if (maybeDaily is uint daily
&& userQuotaData.DailyCount >= daily)
{
return new QuotaLimit()
{
QuotaPeriod = QuotaPer.PerDay,
Quota = daily,
ResetsAt = now.Date.AddDays(1),
Feature = featureName,
FeatureType = featureType,
IsOwnQuota = isSelf
};
}
if (maybeMonthly is uint monthly && userQuotaData.MonthlyCount >= monthly)
{
return new QuotaLimit()
{
QuotaPeriod = QuotaPer.PerMonth,
Quota = monthly,
ResetsAt = now.Date.SecondOfNextMonth(),
Feature = featureName,
FeatureType = featureType,
IsOwnQuota = isSelf
};
}
await ctx.GetTable<PatronQuota>()
.InsertOrUpdateAsync(() => new()
{
UserId = userId,
FeatureType = featureType,
Feature = featureName,
DailyCount = 1,
MonthlyCount = 1,
HourlyCount = 1,
},
(old) => new()
{
HourlyCount = old.HourlyCount + 1,
DailyCount = old.DailyCount + 1,
MonthlyCount = old.MonthlyCount + 1,
},
() => new()
{
UserId = userId,
FeatureType = featureType,
Feature = featureName,
});
await tran.CommitAsync();
return (userQuotaData.HourlyCount + 1, userQuotaData.DailyCount + 1, userQuotaData.MonthlyCount + 1);
}
/// <summary>
/// Attempts to add 1 to user's quota for the command, group and module.
/// Input MUST BE lowercase
/// </summary>
/// <param name="userId">Id of the user who is attempting to run the command</param>
/// <param name="commandName">Name of the command the user is trying to run</param>
/// <param name="groupName">Name of the command's group</param>
/// <param name="moduleName">Name of the command's top level module</param>
/// <param name="isSelf">Whether this is check is for the user himself. False if it's someone else's id (owner)</param>
/// <returns>Either a succcess (user can run the command) or one of the error values.</returns>
private async ValueTask<OneOf<OneOf.Types.Success, InsufficientTier, QuotaLimit>> AttemptRunCommand(
ulong userId,
string commandName,
string groupName,
string moduleName,
bool isSelf)
{
var confData = _pConf.Data;
if (!confData.IsEnabled)
return default;
if (_creds.GetCreds().IsOwner(userId))
return default;
// get user tier
var patron = await GetPatronAsync(userId);
FeatureType quotaForFeatureType;
if (confData.Quotas.Commands.TryGetValue(commandName, out var quotaData))
{
quotaForFeatureType = FeatureType.Command;
}
else if (confData.Quotas.Groups.TryGetValue(groupName, out quotaData))
{
quotaForFeatureType = FeatureType.Group;
}
else if (confData.Quotas.Modules.TryGetValue(moduleName, out quotaData))
{
quotaForFeatureType = FeatureType.Module;
}
else
{
return default;
}
var featureName = quotaForFeatureType switch
{
FeatureType.Command => commandName,
FeatureType.Group => groupName,
FeatureType.Module => moduleName,
_ => throw new ArgumentOutOfRangeException(nameof(quotaForFeatureType))
};
if (!TryGetTierDataOrLower(quotaData, patron.Tier, out var data))
{
return new InsufficientTier()
{
Feature = featureName,
FeatureType = quotaForFeatureType,
RequiredTier = quotaData.Count == 0
? PatronTier.ComingSoon
: quotaData.Keys.First(),
UserTier = patron.Tier,
};
}
// no quota limits for this tier
if (data is null)
return default;
var quotaCheckResult = await TryIncrementQuotaCounterAsync(userId,
isSelf,
quotaForFeatureType,
featureName,
data.TryGetValue(QuotaPer.PerHour, out var hourly) ? hourly : null,
data.TryGetValue(QuotaPer.PerDay, out var daily) ? daily : null,
data.TryGetValue(QuotaPer.PerMonth, out var monthly) ? monthly : null
);
return quotaCheckResult.Match<OneOf<Success, InsufficientTier, QuotaLimit>>(
_ => new Success(),
x => x);
}
private bool TryGetTierDataOrLower<T>(
IReadOnlyDictionary<PatronTier, T?> data,
PatronTier tier,
out T? o)
{
// check for quotas on this tier
if (data.TryGetValue(tier, out o))
return true;
// if there are none, get the quota first tier below this one
// which has quotas specified
for (var i = _tiers.Length - 1; i >= 0; i--)
{
var lowerTier = _tiers[i];
if (lowerTier < tier && data.TryGetValue(lowerTier, out o))
return true;
}
// if there are none, that means the feature is intended
// to be patron-only but the quotas haven't been specified yet
// so it will be marked as "Coming Soon"
o = default;
return false;
}
public async Task<Patron> GetPatronAsync(ulong userId)
{
await using var ctx = _db.GetDbContext();
// this can potentially return multiple users if the user
// is subscribed on multiple platforms
// or if there are multiple users on the same platform who connected the same discord account?!
var users = await ctx.GetTable<PatronUser>()
.Where(x => x.UserId == userId)
.ToListAsync();
// first find all active subscriptions
// and return the one with the highest amount
var maxActive = users.Where(x => !x.ValidThru.IsBeforeToday()).MaxBy(x => x.AmountCents);
if (maxActive is not null)
return PatronUserToPatron(maxActive);
// if there are no active subs, return the one with the highest amount
var max = users.MaxBy(x => x.AmountCents);
if (max is null)
return default; // no patron with that name
return PatronUserToPatron(max);
}
public async Task<UserQuotaStats> GetUserQuotaStatistic(ulong userId)
{
var pConfData = _pConf.Data;
if (!pConfData.IsEnabled)
return new();
var patron = await GetPatronAsync(userId);
await using var ctx = _db.GetDbContext();
var allPatronQuotas = await ctx.GetTable<PatronQuota>()
.Where(x => x.UserId == userId)
.ToListAsync();
var allQuotasDict = allPatronQuotas
.GroupBy(static x => x.FeatureType)
.ToDictionary(static x => x.Key, static x => x.ToDictionary(static y => y.Feature));
allQuotasDict.TryGetValue(FeatureType.Command, out var data);
var userCommandQuotaStats = GetFeatureQuotaStats(patron.Tier, data, pConfData.Quotas.Commands);
allQuotasDict.TryGetValue(FeatureType.Group, out data);
var userGroupQuotaStats = GetFeatureQuotaStats(patron.Tier, data, pConfData.Quotas.Groups);
allQuotasDict.TryGetValue(FeatureType.Module, out data);
var userModuleQuotaStats = GetFeatureQuotaStats(patron.Tier, data, pConfData.Quotas.Modules);
return new UserQuotaStats()
{
Tier = patron.Tier,
Commands = userCommandQuotaStats,
Groups = userGroupQuotaStats,
Modules = userModuleQuotaStats,
};
}
private IReadOnlyDictionary<string, FeatureQuotaStats> GetFeatureQuotaStats(
PatronTier patronTier,
IReadOnlyDictionary<string, PatronQuota>? allQuotasDict,
Dictionary<string, Dictionary<PatronTier, Dictionary<QuotaPer, uint>?>> commands)
{
var userCommandQuotaStats = new Dictionary<string, FeatureQuotaStats>();
foreach (var (key, quotaData) in commands)
{
if (TryGetTierDataOrLower(quotaData, patronTier, out var data))
{
// if data is null that means the quota for the user's tier is unlimited
// no point in returning it?
if (data is null)
continue;
var (daily, hourly, monthly) = default((uint, uint, uint));
// try to get users stats for this feature
// if it fails just leave them at 0
if (allQuotasDict?.TryGetValue(key, out var quota) ?? false)
(daily, hourly, monthly) = (quota.DailyCount, quota.HourlyCount, quota.MonthlyCount);
userCommandQuotaStats[key] = new FeatureQuotaStats()
{
Hourly = data.TryGetValue(QuotaPer.PerHour, out var hourD)
? (hourly, hourD)
: default,
Daily = data.TryGetValue(QuotaPer.PerDay, out var maxD)
? (daily, maxD)
: default,
Monthly = data.TryGetValue(QuotaPer.PerMonth, out var maxM)
? (monthly, maxM)
: default,
};
}
}
return userCommandQuotaStats;
}
public async Task<FeatureLimit> TryGetFeatureLimitAsync(FeatureLimitKey key, ulong userId, int? defaultValue)
{
var conf = _pConf.Data;
// if patron system is disabled, the quota is just default
if (!conf.IsEnabled)
return new()
{
Name = key.PrettyName,
Quota = defaultValue,
IsPatronLimit = false
};
if (!conf.Quotas.Features.TryGetValue(key.Key, out var data))
return new()
{
Name = key.PrettyName,
Quota = defaultValue,
IsPatronLimit = false,
};
var patron = await GetPatronAsync(userId);
if (!TryGetTierDataOrLower(data, patron.Tier, out var limit))
return new()
{
Name = key.PrettyName,
Quota = 0,
IsPatronLimit = true,
};
return new()
{
Name = key.PrettyName,
Quota = limit,
IsPatronLimit = true
};
}
// public async Task<Patron> GiftPatronAsync(IUser user, int amount)
// {
// if (amount < 1)
// throw new ArgumentOutOfRangeException(nameof(amount));
//
//
// }
private Patron PatronUserToPatron(PatronUser user)
=> new Patron()
{
UniquePlatformUserId = user.UniquePlatformUserId,
UserId = user.UserId,
Amount = user.AmountCents,
Tier = CalculateTier(user),
PaidAt = user.LastCharge,
ValidThru = user.ValidThru,
};
private PatronTier CalculateTier(PatronUser user)
{
if (user.ValidThru.IsBeforeToday())
return PatronTier.None;
return user.AmountCents switch
{
>= 10_000 => PatronTier.C,
>= 5000 => PatronTier.L,
>= 2000 => PatronTier.XX,
>= 1000 => PatronTier.X,
>= 500 => PatronTier.V,
>= 100 => PatronTier.I,
_ => PatronTier.None
};
}
private async Task SendWelcomeMessage(Patron patron)
{
try
{
var user = (IUser)_client.GetUser(patron.UserId) ?? await _client.Rest.GetUserAsync(patron.UserId);
if (user is null)
return;
var eb = _eb.Create()
.WithOkColor()
.WithTitle("❤️ Thank you for supporting NadekoBot! ❤️")
.WithDescription(
"Your donation has been processed and you will receive the rewards shortly.\n"
+ "You can visit <https://www.patreon.com/join/nadekobot> to see rewards for your tier. 🎉")
.AddField("Tier", Format.Bold(patron.Tier.ToString()), true)
.AddField("Pledge", $"**{patron.Amount / 100.0f:N1}$**", true)
.AddField("Expires",
patron.ValidThru.AddDays(1).ToShortAndRelativeTimestampTag(),
true)
.AddField("Instructions",
"""
*- Within the next **1-2 minutes** you will have all of the benefits of the Tier you've subscribed to.*
*- You can check your benefits on <https://www.patreon.com/join/nadekobot>*
*- You can use the `.patron` command in this chat to check your current quota usage for the Patron-only commands*
*- **ALL** of the servers that you **own** will enjoy your Patron benefits.*
*- You can use any of the commands available in your tier on any server (assuming you have sufficient permissions to run those commands)*
*- Any user in any of your servers can use Patron-only commands, but they will spend **your quota**, which is why it's recommended to use Nadeko's command cooldown system (.h .cmdcd) or permission system to limit the command usage for your server members.*
*- Permission guide can be found here if you're not familiar with it: <https://nadekobot.readthedocs.io/en/latest/permissions-system/>*
""",
isInline: false)
.WithFooter($"platform id: {patron.UniquePlatformUserId}");
await user.EmbedAsync(eb);
}
catch
{
Log.Warning("Unable to send a \"Welcome\" message to the patron {UserId}", patron.UserId);
}
}
public async Task<(int Success, int Failed)> SendMessageToPatronsAsync(PatronTier tierAndHigher, string message)
{
await using var ctx = _db.GetDbContext();
var patrons = await ctx.GetTable<PatronUser>()
.Where(x => x.ValidThru > DateTime.UtcNow)
.ToArrayAsync();
var text = SmartText.CreateFrom(message);
var succ = 0;
var fail = 0;
foreach (var patron in patrons)
{
try
{
var user = await _client.GetUserAsync(patron.UserId);
await user.SendAsync(text);
++succ;
}
catch
{
++fail;
}
await Task.Delay(1000);
}
return (succ, fail);
}
public PatronConfigData GetConfig()
=> _pConf.Data;
}

View File

@@ -1,6 +0,0 @@
namespace NadekoBot.Modules.Utility;
public interface IQuoteService
{
Task<int> DeleteAllAuthorQuotesAsync(ulong guildId, ulong userId);
}

View File

@@ -1,416 +0,0 @@
#nullable disable warnings
using NadekoBot.Common.Yml;
using NadekoBot.Db;
using NadekoBot.Services.Database.Models;
using YamlDotNet.Serialization;
using YamlDotNet.Serialization.NamingConventions;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class QuoteCommands : NadekoModule
{
private const string PREPEND_EXPORT =
"""
# Keys are keywords, Each key has a LIST of quotes in the following format:
# - id: Alphanumeric id used for commands related to the quote. (Note, when using .quotesimport, a new id will be generated.)
# an: Author name
# aid: Author id
# txt: Quote text
""";
private static readonly ISerializer _exportSerializer = new SerializerBuilder()
.WithEventEmitter(args
=> new MultilineScalarFlowStyleEmitter(args))
.WithNamingConvention(
CamelCaseNamingConvention.Instance)
.WithIndentedSequences()
.ConfigureDefaultValuesHandling(DefaultValuesHandling
.OmitDefaults)
.DisableAliases()
.Build();
private readonly DbService _db;
private readonly IHttpClientFactory _http;
private readonly IQuoteService _qs;
public QuoteCommands(DbService db, IQuoteService qs, IHttpClientFactory http)
{
_db = db;
_http = http;
_qs = qs;
}
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(1)]
public Task ListQuotes(OrderType order = OrderType.Keyword)
=> ListQuotes(1, order);
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(0)]
public async Task ListQuotes(int page = 1, OrderType order = OrderType.Keyword)
{
page -= 1;
if (page < 0)
return;
IEnumerable<Quote> quotes;
await using (var uow = _db.GetDbContext())
{
quotes = uow.Quotes.GetGroup(ctx.Guild.Id, page, order);
}
if (quotes.Any())
{
await SendConfirmAsync(GetText(strs.quotes_page(page + 1)),
string.Join("\n",
quotes.Select(q
=> $"`#{q.Id}` {Format.Bold(q.Keyword.SanitizeAllMentions()),-20} by {q.AuthorName.SanitizeAllMentions()}")));
}
else
await ReplyErrorLocalizedAsync(strs.quotes_page_none);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuotePrint([Leftover] string keyword)
{
if (string.IsNullOrWhiteSpace(keyword))
return;
keyword = keyword.ToUpperInvariant();
Quote quote;
await using (var uow = _db.GetDbContext())
{
quote = await uow.Quotes.GetRandomQuoteByKeywordAsync(ctx.Guild.Id, keyword);
//if (quote is not null)
//{
// quote.UseCount += 1;
// uow.Complete();
//}
}
if (quote is null)
return;
var rep = new ReplacementBuilder().WithDefault(Context).Build();
var text = SmartText.CreateFrom(quote.Text);
text = rep.Replace(text);
await ctx.Channel.SendAsync($"`#{quote.Id}` 📣 " + text, true);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteShow(int id)
{
Quote? quote;
await using (var uow = _db.GetDbContext())
{
quote = uow.Quotes.GetById(id);
if (quote?.GuildId != ctx.Guild.Id)
quote = null;
}
if (quote is null)
{
await ReplyErrorLocalizedAsync(strs.quotes_notfound);
return;
}
await ShowQuoteData(quote);
}
private async Task ShowQuoteData(Quote data)
=> await ctx.Channel.EmbedAsync(_eb.Create(ctx)
.WithOkColor()
.WithTitle(GetText(strs.quote_id($"#{data.Id}")))
.AddField(GetText(strs.trigger), data.Keyword)
.AddField(GetText(strs.response),
Format.Sanitize(data.Text).Replace("](", "]\\("))
.WithFooter(
GetText(strs.created_by($"{data.AuthorName} ({data.AuthorId})"))));
private async Task QuoteSearchinternalAsync(string? keyword, string textOrAuthor)
{
if (string.IsNullOrWhiteSpace(textOrAuthor))
return;
keyword = keyword?.ToUpperInvariant();
Quote quote;
await using (var uow = _db.GetDbContext())
{
quote = await uow.Quotes.SearchQuoteKeywordTextAsync(ctx.Guild.Id, keyword, textOrAuthor);
}
if (quote is null)
return;
await ctx.Channel.SendMessageAsync($"`#{quote.Id}` 💬 "
+ quote.Keyword.ToLowerInvariant()
+ ": "
+ quote.Text.SanitizeAllMentions());
}
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(0)]
public Task QuoteSearch(string textOrAuthor)
=> QuoteSearchinternalAsync(null, textOrAuthor);
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(1)]
public Task QuoteSearch(string keyword, [Leftover] string textOrAuthor)
=> QuoteSearchinternalAsync(keyword, textOrAuthor);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteId(int id)
{
if (id < 0)
return;
Quote quote;
var rep = new ReplacementBuilder().WithDefault(Context).Build();
await using (var uow = _db.GetDbContext())
{
quote = uow.Quotes.GetById(id);
}
if (quote is null || quote.GuildId != ctx.Guild.Id)
{
await SendErrorAsync(GetText(strs.quotes_notfound));
return;
}
var infoText = $"`#{quote.Id} added by {quote.AuthorName.SanitizeAllMentions()}` 🗯️ "
+ quote.Keyword.ToLowerInvariant().SanitizeAllMentions()
+ ":\n";
var text = SmartText.CreateFrom(quote.Text);
text = rep.Replace(text);
await ctx.Channel.SendAsync(infoText + text, true);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteAdd(string keyword, [Leftover] string text)
{
if (string.IsNullOrWhiteSpace(keyword) || string.IsNullOrWhiteSpace(text))
return;
keyword = keyword.ToUpperInvariant();
Quote q;
await using (var uow = _db.GetDbContext())
{
uow.Quotes.Add(q = new()
{
AuthorId = ctx.Message.Author.Id,
AuthorName = ctx.Message.Author.Username,
GuildId = ctx.Guild.Id,
Keyword = keyword,
Text = text
});
await uow.SaveChangesAsync();
}
await ReplyConfirmLocalizedAsync(strs.quote_added_new(Format.Code(q.Id.ToString())));
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteDelete(int id)
{
var hasManageMessages = ((IGuildUser)ctx.Message.Author).GuildPermissions.ManageMessages;
var success = false;
string response;
await using (var uow = _db.GetDbContext())
{
var q = uow.Quotes.GetById(id);
if (q?.GuildId != ctx.Guild.Id || (!hasManageMessages && q.AuthorId != ctx.Message.Author.Id))
response = GetText(strs.quotes_remove_none);
else
{
uow.Quotes.Remove(q);
await uow.SaveChangesAsync();
success = true;
response = GetText(strs.quote_deleted(id));
}
}
if (success)
await SendConfirmAsync(response);
else
await SendErrorAsync(response);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public Task QuoteDeleteAuthor(IUser user)
=> QuoteDeleteAuthor(user.Id);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task QuoteDeleteAuthor(ulong userId)
{
var hasManageMessages = ((IGuildUser)ctx.Message.Author).GuildPermissions.ManageMessages;
if (userId == ctx.User.Id || hasManageMessages)
{
var deleted = await _qs.DeleteAllAuthorQuotesAsync(ctx.Guild.Id, userId);
await ReplyConfirmLocalizedAsync(strs.quotes_deleted_count(deleted));
}
else
{
await ReplyErrorLocalizedAsync(strs.insuf_perms_u);
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task DelAllQuotes([Leftover] string keyword)
{
if (string.IsNullOrWhiteSpace(keyword))
return;
keyword = keyword.ToUpperInvariant();
await using (var uow = _db.GetDbContext())
{
uow.Quotes.RemoveAllByKeyword(ctx.Guild.Id, keyword.ToUpperInvariant());
await uow.SaveChangesAsync();
}
await ReplyConfirmLocalizedAsync(strs.quotes_deleted(Format.Bold(keyword.SanitizeAllMentions())));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
public async Task QuotesExport()
{
IEnumerable<Quote> quotes;
await using (var uow = _db.GetDbContext())
{
quotes = uow.Quotes.GetForGuild(ctx.Guild.Id).ToList();
}
var exprsDict = quotes.GroupBy(x => x.Keyword)
.ToDictionary(x => x.Key, x => x.Select(ExportedQuote.FromModel));
var text = PREPEND_EXPORT + _exportSerializer.Serialize(exprsDict).UnescapeUnicodeCodePoints();
await using var stream = await text.ToStream();
await ctx.Channel.SendFileAsync(stream, "quote-export.yml");
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[Ratelimit(300)]
#if GLOBAL_NADEKO
[OwnerOnly]
#endif
public async Task QuotesImport([Leftover] string? input = null)
{
input = input?.Trim();
_ = ctx.Channel.TriggerTypingAsync();
if (input is null)
{
var attachment = ctx.Message.Attachments.FirstOrDefault();
if (attachment is null)
{
await ReplyErrorLocalizedAsync(strs.expr_import_no_input);
return;
}
using var client = _http.CreateClient();
input = await client.GetStringAsync(attachment.Url);
if (string.IsNullOrWhiteSpace(input))
{
await ReplyErrorLocalizedAsync(strs.expr_import_no_input);
return;
}
}
var succ = await ImportExprsAsync(ctx.Guild.Id, input);
if (!succ)
{
await ReplyErrorLocalizedAsync(strs.expr_import_invalid_data);
return;
}
await ctx.OkAsync();
}
private async Task<bool> ImportExprsAsync(ulong guildId, string input)
{
Dictionary<string, List<ExportedQuote>> data;
try
{
data = Yaml.Deserializer.Deserialize<Dictionary<string, List<ExportedQuote>>>(input);
if (data.Sum(x => x.Value.Count) == 0)
return false;
}
catch
{
return false;
}
await using var uow = _db.GetDbContext();
foreach (var entry in data)
{
var keyword = entry.Key;
await uow.Quotes.AddRangeAsync(entry.Value.Where(quote => !string.IsNullOrWhiteSpace(quote.Txt))
.Select(quote => new Quote
{
GuildId = guildId,
Keyword = keyword,
Text = quote.Txt,
AuthorId = quote.Aid,
AuthorName = quote.An
}));
}
await uow.SaveChangesAsync();
return true;
}
public class ExportedQuote
{
public string Id { get; set; }
public string An { get; set; }
public ulong Aid { get; set; }
public string Txt { get; set; }
public static ExportedQuote FromModel(Quote quote)
=> new()
{
Id = ((kwum)quote.Id).ToString(),
An = quote.AuthorName,
Aid = quote.AuthorId,
Txt = quote.Text
};
}
}
}

View File

@@ -1,32 +0,0 @@
#nullable disable warnings
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility;
public sealed class QuoteService : IQuoteService, INService
{
private readonly DbService _db;
public QuoteService(DbService db)
{
_db = db;
}
/// <summary>
/// Delete all quotes created by the author in a guild
/// </summary>
/// <param name="guildId">ID of the guild</param>
/// <param name="userId">ID of the user</param>
/// <returns>Number of deleted qutoes</returns>
public async Task<int> DeleteAllAuthorQuotesAsync(ulong guildId, ulong userId)
{
await using var ctx = _db.GetDbContext();
var deleted = await ctx.GetTable<Quote>()
.Where(x => x.GuildId == guildId && x.AuthorId == userId)
.DeleteAsync();
return deleted;
}
}

View File

@@ -1,224 +0,0 @@
#nullable disable
using Humanizer.Localisation;
using NadekoBot.Db;
using NadekoBot.Modules.Administration.Services;
using NadekoBot.Modules.Utility.Services;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class RemindCommands : NadekoModule<RemindService>
{
public enum MeOrHere
{
Me,
Here
}
public enum Server
{
Server = int.MinValue,
Srvr = int.MinValue,
Serv = int.MinValue,
S = int.MinValue
}
private readonly DbService _db;
private readonly GuildTimezoneService _tz;
public RemindCommands(DbService db, GuildTimezoneService tz)
{
_db = db;
_tz = tz;
}
[Cmd]
[Priority(1)]
public async Task Remind(MeOrHere meorhere, [Leftover] string remindString)
{
if (!_service.TryParseRemindMessage(remindString, out var remindData))
{
await ReplyErrorLocalizedAsync(strs.remind_invalid);
return;
}
ulong target;
target = meorhere == MeOrHere.Me ? ctx.User.Id : ctx.Channel.Id;
if (!await RemindInternal(target,
meorhere == MeOrHere.Me || ctx.Guild is null,
remindData.Time,
remindData.What))
await ReplyErrorLocalizedAsync(strs.remind_too_long);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(0)]
public async Task Remind(ITextChannel channel, [Leftover] string remindString)
{
var perms = ((IGuildUser)ctx.User).GetPermissions(channel);
if (!perms.SendMessages || !perms.ViewChannel)
{
await ReplyErrorLocalizedAsync(strs.cant_read_or_send);
return;
}
if (!_service.TryParseRemindMessage(remindString, out var remindData))
{
await ReplyErrorLocalizedAsync(strs.remind_invalid);
return;
}
if (!await RemindInternal(channel.Id, false, remindData.Time, remindData.What))
await ReplyErrorLocalizedAsync(strs.remind_too_long);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[Priority(0)]
public Task RemindList(Server _, int page = 1)
=> RemindListInternal(page, true);
[Cmd]
[Priority(1)]
public Task RemindList(int page = 1)
=> RemindListInternal(page, false);
private async Task RemindListInternal(int page, bool isServer)
{
if (--page < 0)
return;
var embed = _eb.Create()
.WithOkColor()
.WithTitle(GetText(isServer ? strs.reminder_server_list : strs.reminder_list));
List<Reminder> rems;
await using (var uow = _db.GetDbContext())
{
if (isServer)
rems = uow.Reminders.RemindersForServer(ctx.Guild.Id, page).ToList();
else
rems = uow.Reminders.RemindersFor(ctx.User.Id, page).ToList();
}
if (rems.Any())
{
var i = 0;
foreach (var rem in rems)
{
var when = rem.When;
var diff = when - DateTime.UtcNow;
embed.AddField(
$"#{++i + (page * 10)} {rem.When:HH:mm yyyy-MM-dd} UTC "
+ $"(in {diff.Humanize(2, minUnit: TimeUnit.Minute, culture: Culture)})",
$@"`Target:` {(rem.IsPrivate ? "DM" : "Channel")}
`TargetId:` {rem.ChannelId}
`Message:` {rem.Message?.TrimTo(50)}");
}
}
else
embed.WithDescription(GetText(strs.reminders_none));
embed.AddPaginatedFooter(page + 1, null);
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.Administrator)]
[Priority(0)]
public Task RemindDelete(Server _, int index)
=> RemindDelete(index, true);
[Cmd]
[Priority(1)]
public Task RemindDelete(int index)
=> RemindDelete(index, false);
private async Task RemindDelete(int index, bool isServer)
{
if (--index < 0)
return;
Reminder rem = null;
await using (var uow = _db.GetDbContext())
{
var rems = isServer
? uow.Reminders.RemindersForServer(ctx.Guild.Id, index / 10).ToList()
: uow.Reminders.RemindersFor(ctx.User.Id, index / 10).ToList();
var pageIndex = index % 10;
if (rems.Count > pageIndex)
{
rem = rems[pageIndex];
uow.Reminders.Remove(rem);
uow.SaveChanges();
}
}
if (rem is null)
await ReplyErrorLocalizedAsync(strs.reminder_not_exist);
else
await ReplyConfirmLocalizedAsync(strs.reminder_deleted(index + 1));
}
private async Task<bool> RemindInternal(
ulong targetId,
bool isPrivate,
TimeSpan ts,
string message)
{
var time = DateTime.UtcNow + ts;
if (ts > TimeSpan.FromDays(60))
return false;
if (ctx.Guild is not null)
{
var perms = ((IGuildUser)ctx.User).GetPermissions((IGuildChannel)ctx.Channel);
if (!perms.MentionEveryone)
message = message.SanitizeAllMentions();
}
var rem = new Reminder
{
ChannelId = targetId,
IsPrivate = isPrivate,
When = time,
Message = message,
UserId = ctx.User.Id,
ServerId = ctx.Guild?.Id ?? 0
};
await using (var uow = _db.GetDbContext())
{
uow.Reminders.Add(rem);
await uow.SaveChangesAsync();
}
var gTime = ctx.Guild is null ? time : TimeZoneInfo.ConvertTime(time, _tz.GetTimeZoneOrUtc(ctx.Guild.Id));
try
{
await SendConfirmAsync("⏰ "
+ GetText(strs.remind(
Format.Bold(!isPrivate ? $"<#{targetId}>" : ctx.User.Username),
Format.Bold(message),
ts.Humanize(3, minUnit: TimeUnit.Second, culture: Culture),
gTime,
gTime)));
}
catch
{
}
return true;
}
}
}

View File

@@ -1,252 +0,0 @@
#nullable disable
using System.Globalization;
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Services.Database.Models;
using System.Text.RegularExpressions;
namespace NadekoBot.Modules.Utility.Services;
public class RemindService : INService, IReadyExecutor, IRemindService
{
private readonly Regex _regex =
new(@"^(?:(?:at|on(?:\sthe)?)?\s*(?<date>(?:\d{2}:\d{2}\s)?\d{1,2}\.\d{1,2}(?:\.\d{2,4})?)|(?:in\s?)?\s*(?:(?<mo>\d+)(?:\s?(?:months?|mos?),?))?(?:(?:\sand\s|\s*)?(?<w>\d+)(?:\s?(?:weeks?|w),?))?(?:(?:\sand\s|\s*)?(?<d>\d+)(?:\s?(?:days?|d),?))?(?:(?:\sand\s|\s*)?(?<h>\d+)(?:\s?(?:hours?|h),?))?(?:(?:\sand\s|\s*)?(?<m>\d+)(?:\s?(?:minutes?|mins?|m),?))?)\s+(?:to:?\s+)?(?<what>(?:\r\n|[\r\n]|.)+)",
RegexOptions.Compiled | RegexOptions.Multiline);
private readonly DiscordSocketClient _client;
private readonly DbService _db;
private readonly IBotCredentials _creds;
private readonly IEmbedBuilderService _eb;
private readonly CultureInfo _culture;
public RemindService(
DiscordSocketClient client,
DbService db,
IBotCredentials creds,
IEmbedBuilderService eb)
{
_client = client;
_db = db;
_creds = creds;
_eb = eb;
try
{
_culture = new CultureInfo("en-GB");
}
catch
{
_culture = CultureInfo.InvariantCulture;
}
}
public async Task OnReadyAsync()
{
using var timer = new PeriodicTimer(TimeSpan.FromSeconds(15));
while (await timer.WaitForNextTickAsync())
{
await OnReminderLoopTickInternalAsync();
}
}
private async Task OnReminderLoopTickInternalAsync()
{
try
{
var now = DateTime.UtcNow;
var reminders = await GetRemindersBeforeAsync(now);
if (reminders.Count == 0)
return;
Log.Information("Executing {ReminderCount} reminders", reminders.Count);
// make groups of 5, with 1.5 second inbetween each one to ensure against ratelimits
foreach (var group in reminders.Chunk(5))
{
var executedReminders = group.ToList();
await executedReminders.Select(ReminderTimerAction).WhenAll();
await RemoveReminders(executedReminders.Select(x => x.Id));
await Task.Delay(1500);
}
}
catch (Exception ex)
{
Log.Warning(ex, "Error in reminder loop: {ErrorMessage}", ex.Message);
}
}
private async Task RemoveReminders(IEnumerable<int> reminders)
{
await using var uow = _db.GetDbContext();
await uow.Reminders
.ToLinqToDBTable()
.DeleteAsync(x => reminders.Contains(x.Id));
await uow.SaveChangesAsync();
}
private async Task<List<Reminder>> GetRemindersBeforeAsync(DateTime now)
{
await using var uow = _db.GetDbContext();
return await uow.Reminders
.ToLinqToDBTable()
.Where(x => Linq2DbExpressions.GuildOnShard(x.ServerId, _creds.TotalShards, _client.ShardId)
&& x.When < now)
.ToListAsyncLinqToDB();
}
public bool TryParseRemindMessage(string input, out RemindObject obj)
{
var m = _regex.Match(input);
obj = default;
if (m.Length == 0)
return false;
var values = new Dictionary<string, int>();
var what = m.Groups["what"].Value;
if (string.IsNullOrWhiteSpace(what))
{
Log.Warning("No message provided for the reminder");
return false;
}
TimeSpan ts;
var dateString = m.Groups["date"].Value;
if (!string.IsNullOrWhiteSpace(dateString))
{
var now = DateTime.UtcNow;
if (!DateTime.TryParse(dateString, _culture, DateTimeStyles.None, out var dt))
{
Log.Warning("Invalid remind datetime format");
return false;
}
if (now >= dt)
{
Log.Warning("That remind time has already passed");
return false;
}
ts = dt - now;
}
else
{
foreach (var groupName in _regex.GetGroupNames())
{
if (groupName is "0" or "what")
continue;
if (string.IsNullOrWhiteSpace(m.Groups[groupName].Value))
{
values[groupName] = 0;
continue;
}
if (!int.TryParse(m.Groups[groupName].Value, out var value))
{
Log.Warning("Reminder regex group {GroupName} has invalid value", groupName);
return false;
}
if (value < 1)
{
Log.Warning("Reminder time value has to be an integer greater than 0");
return false;
}
values[groupName] = value;
}
ts = new TimeSpan((30 * values["mo"]) + (7 * values["w"]) + values["d"], values["h"], values["m"], 0);
}
obj = new()
{
Time = ts,
What = what
};
return true;
}
private async Task ReminderTimerAction(Reminder r)
{
try
{
IMessageChannel ch;
if (r.IsPrivate)
{
var user = _client.GetUser(r.ChannelId);
if (user is null)
return;
ch = await user.CreateDMChannelAsync();
}
else
ch = _client.GetGuild(r.ServerId)?.GetTextChannel(r.ChannelId);
if (ch is null)
return;
var st = SmartText.CreateFrom(r.Message);
if (st is SmartEmbedText set)
{
await ch.SendMessageAsync(null, embed: set.GetEmbed().Build());
}
else if (st is SmartEmbedTextArray seta)
{
await ch.SendMessageAsync(null, embeds: seta.GetEmbedBuilders().Map(x => x.Build()));
}
else
{
await ch.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle("Reminder")
.AddField("Created At",
r.DateAdded.HasValue ? r.DateAdded.Value.ToLongDateString() : "?")
.AddField("By",
(await ch.GetUserAsync(r.UserId))?.ToString() ?? r.UserId.ToString()),
r.Message);
}
}
catch (Exception ex)
{
Log.Warning(ex, "Error executing reminder {ReminderId}: {ErrorMessage}", r.Id, ex.Message);
}
}
public struct RemindObject
{
public string What { get; set; }
public TimeSpan Time { get; set; }
}
public async Task AddReminderAsync(ulong userId,
ulong targetId,
ulong? guildId,
bool isPrivate,
DateTime time,
string message)
{
var rem = new Reminder
{
UserId = userId,
ChannelId = targetId,
ServerId = guildId ?? 0,
IsPrivate = isPrivate,
When = time,
Message = message,
};
await using var ctx = _db.GetDbContext();
await ctx.Reminders
.AddAsync(rem);
await ctx.SaveChangesAsync();
}
}

View File

@@ -1,204 +0,0 @@
using NadekoBot.Common.TypeReaders;
using NadekoBot.Common.TypeReaders.Models;
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class RepeatCommands : NadekoModule<RepeaterService>
{
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task RepeatSkip(int index)
{
if (--index < 0)
return;
var result = await _service.ToggleSkipNextAsync(ctx.Guild.Id, index);
if (result is null)
{
await ReplyErrorLocalizedAsync(strs.index_out_of_range);
return;
}
if (result is true)
{
await ReplyConfirmLocalizedAsync(strs.repeater_skip_next);
}
else
{
await ReplyConfirmLocalizedAsync(strs.repeater_dont_skip_next);
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task RepeatInvoke(int index)
{
if (--index < 0)
return;
var success = await _service.TriggerExternal(ctx.Guild.Id, index);
if (!success)
await ReplyErrorLocalizedAsync(strs.repeat_invoke_none);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task RepeatRemove(int index)
{
if (--index < 0)
return;
var removed = await _service.RemoveByIndexAsync(ctx.Guild.Id, index);
if (removed is null)
{
await ReplyErrorLocalizedAsync(strs.repeater_remove_fail);
return;
}
var description = GetRepeaterInfoString(removed);
await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.repeater_removed(index + 1)))
.WithDescription(description));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task RepeatRedundant(int index)
{
if (--index < 0)
return;
var result = await _service.ToggleRedundantAsync(ctx.Guild.Id, index);
if (result is null)
{
await ReplyErrorLocalizedAsync(strs.index_out_of_range);
return;
}
if (result.Value)
await ReplyErrorLocalizedAsync(strs.repeater_redundant_no(index + 1));
else
await ReplyConfirmLocalizedAsync(strs.repeater_redundant_yes(index + 1));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(-1)]
public Task Repeat([Leftover] string message)
=> Repeat(null, null, message);
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(0)]
public Task Repeat(StoopidTime interval, [Leftover] string message)
=> Repeat(null, interval, message);
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(1)]
public Task Repeat(GuildDateTime dt, [Leftover] string message)
=> Repeat(dt, null, message);
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(2)]
public async Task Repeat(GuildDateTime? dt, StoopidTime? interval, [Leftover] string message)
{
var startTimeOfDay = dt?.InputTimeUtc.TimeOfDay;
// if interval not null, that means user specified it (don't change it)
// if interval is null set the default to:
// if time of day is specified: 1 day
// else 5 minutes
var realInterval =
interval?.Time ?? (startTimeOfDay is null ? TimeSpan.FromMinutes(5) : TimeSpan.FromDays(1));
if (string.IsNullOrWhiteSpace(message)
|| (interval is not null
&& (interval.Time > TimeSpan.FromMinutes(25000) || interval.Time < TimeSpan.FromMinutes(1))))
return;
message = ((IGuildUser)ctx.User).GuildPermissions.MentionEveryone
? message
: message.SanitizeMentions(true);
var runner = await _service.AddRepeaterAsync(ctx.Channel.Id,
ctx.Guild.Id,
realInterval,
message,
false,
startTimeOfDay);
if (runner is null)
{
await ReplyErrorLocalizedAsync(strs.repeater_exceed_limit(5));
return;
}
var description = GetRepeaterInfoString(runner);
await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.repeater_created))
.WithDescription(description));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task RepeatList()
{
var repeaters = _service.GetRepeaters(ctx.Guild.Id);
if (repeaters.Count == 0)
{
await ReplyConfirmLocalizedAsync(strs.repeaters_none);
return;
}
var embed = _eb.Create().WithTitle(GetText(strs.list_of_repeaters)).WithOkColor();
var i = 0;
foreach (var runner in repeaters.OrderBy(r => r.Repeater.Id))
{
var description = GetRepeaterInfoString(runner);
var name = $"#`{++i}` {(_service.IsRepeaterSkipped(runner.Repeater.Id) ? "🦘" : "")}";
embed.AddField(name, description);
}
await ctx.Channel.EmbedAsync(embed);
}
private string GetRepeaterInfoString(RunningRepeater runner)
{
var intervalString = Format.Bold(runner.Repeater.Interval.ToPrettyStringHm());
var executesIn = runner.NextTime < DateTime.UtcNow ? TimeSpan.Zero : runner.NextTime - DateTime.UtcNow;
var executesInString = Format.Bold(executesIn.ToPrettyStringHm());
var message = Format.Sanitize(runner.Repeater.Message.TrimTo(50));
var description = string.Empty;
if (_service.IsNoRedundant(runner.Repeater.Id))
description = Format.Underline(Format.Bold(GetText(strs.no_redundant))) + "\n\n";
description += $"<#{runner.Repeater.ChannelId}>\n"
+ $"`{GetText(strs.interval_colon)}` {intervalString}\n"
+ $"`{GetText(strs.executes_in_colon)}` {executesInString}\n"
+ $"`{GetText(strs.message_colon)}` {message}";
return description;
}
}
}

View File

@@ -1,422 +0,0 @@
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore;
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility.Services;
public sealed class RepeaterService : IReadyExecutor, INService
{
private const int MAX_REPEATERS = 5;
private readonly DbService _db;
private readonly IBotCredentials _creds;
private readonly DiscordSocketClient _client;
private readonly LinkedList<RunningRepeater> _repeaterQueue;
private readonly ConcurrentHashSet<int> _noRedundant;
private readonly ConcurrentHashSet<int> _skipNext = new();
private readonly object _queueLocker = new();
public RepeaterService(
DiscordSocketClient client,
DbService db,
IBotCredentials creds)
{
_db = db;
_creds = creds;
_client = client;
using var uow = _db.GetDbContext();
var shardRepeaters = uow.Set<Repeater>()
.Where(x => (int)(x.GuildId / Math.Pow(2, 22)) % _creds.TotalShards
== _client.ShardId)
.AsNoTracking()
.ToList();
_noRedundant = new(shardRepeaters.Where(x => x.NoRedundant).Select(x => x.Id));
_repeaterQueue = new(shardRepeaters.Select(rep => new RunningRepeater(rep)).OrderBy(x => x.NextTime));
}
public Task OnReadyAsync()
{
_ = Task.Run(RunRepeatersLoop);
return Task.CompletedTask;
}
private async Task RunRepeatersLoop()
{
while (true)
{
try
{
// calculate timeout for the first item
var timeout = GetNextTimeout();
// wait it out, and recalculate afterwards
// because repeaters might've been modified meanwhile
if (timeout > TimeSpan.Zero)
{
await Task.Delay(timeout > TimeSpan.FromMinutes(1) ? TimeSpan.FromMinutes(1) : timeout);
continue;
}
// collect (remove) all repeaters which need to run (3 seconds tolerance)
var now = DateTime.UtcNow + TimeSpan.FromSeconds(3);
var toExecute = new List<RunningRepeater>();
lock (_repeaterQueue)
{
var current = _repeaterQueue.First;
while (true)
{
if (current is null || current.Value.NextTime > now)
break;
toExecute.Add(current.Value);
current = current.Next;
}
}
// execute
foreach (var chunk in toExecute.Chunk(5))
await chunk.Where(x => !_skipNext.TryRemove(x.Repeater.Id)).Select(Trigger).WhenAll();
// reinsert
foreach (var rep in toExecute)
await HandlePostExecute(rep);
}
catch (Exception ex)
{
Log.Error(ex, "Critical error in repeater queue: {ErrorMessage}", ex.Message);
await Task.Delay(5000);
}
}
}
private async Task HandlePostExecute(RunningRepeater rep)
{
if (rep.ErrorCount >= 10)
{
RemoveFromQueue(rep.Repeater.Id);
await RemoveRepeaterInternal(rep.Repeater);
return;
}
UpdatePosition(rep);
}
private void UpdatePosition(RunningRepeater rep)
{
lock (_queueLocker)
{
rep.UpdateNextTime();
_repeaterQueue.Remove(rep);
AddToQueue(rep);
}
}
public async Task<bool> TriggerExternal(ulong guildId, int index)
{
await using var uow = _db.GetDbContext();
var toTrigger = await uow.Repeaters.AsNoTracking()
.Where(x => x.GuildId == guildId)
.Skip(index)
.FirstOrDefaultAsyncEF();
if (toTrigger is null)
return false;
LinkedListNode<RunningRepeater>? node;
lock (_queueLocker)
{
node = _repeaterQueue.FindNode(x => x.Repeater.Id == toTrigger.Id);
if (node is null)
return false;
_repeaterQueue.Remove(node);
}
await Trigger(node.Value);
await HandlePostExecute(node.Value);
return true;
}
private void AddToQueue(RunningRepeater rep)
{
lock (_queueLocker)
{
var current = _repeaterQueue.First;
if (current is null)
{
_repeaterQueue.AddFirst(rep);
return;
}
while (current is not null && current.Value.NextTime < rep.NextTime)
current = current.Next;
if (current is null)
_repeaterQueue.AddLast(rep);
else
_repeaterQueue.AddBefore(current, rep);
}
}
private TimeSpan GetNextTimeout()
{
lock (_queueLocker)
{
var first = _repeaterQueue.First;
// if there are no items in the queue, just wait out the minimum duration (1 minute) and try again
if (first is null)
return TimeSpan.FromMinutes(1);
return first.Value.NextTime - DateTime.UtcNow;
}
}
private async Task Trigger(RunningRepeater rr)
{
var repeater = rr.Repeater;
void ChannelMissingError()
{
rr.ErrorCount = int.MaxValue;
Log.Warning("[Repeater] Channel [{Channelid}] for not found or insufficient permissions. "
+ "Repeater will be removed. ",
repeater.ChannelId);
}
var channel = _client.GetChannel(repeater.ChannelId) as ITextChannel;
if (channel is null)
{
try { channel = await _client.Rest.GetChannelAsync(repeater.ChannelId) as ITextChannel; }
catch { }
}
if (channel is null)
{
ChannelMissingError();
return;
}
var guild = _client.GetGuild(channel.GuildId);
if (guild is null)
{
ChannelMissingError();
return;
}
if (_noRedundant.Contains(repeater.Id))
{
try
{
var lastMsgInChannel = await channel.GetMessagesAsync(2).Flatten().FirstAsync();
if (lastMsgInChannel is not null && lastMsgInChannel.Id == repeater.LastMessageId)
return;
}
catch (Exception ex)
{
Log.Warning(ex,
"[Repeater] Error while getting last channel message in {GuildId}/{ChannelId} "
+ "Bot probably doesn't have the permission to read message history",
guild.Id,
channel.Id);
}
}
if (repeater.LastMessageId is { } lastMessageId)
{
try
{
var oldMsg = await channel.GetMessageAsync(lastMessageId);
if (oldMsg is not null)
await oldMsg.DeleteAsync();
}
catch (Exception ex)
{
Log.Warning(ex,
"[Repeater] Error while deleting previous message in {GuildId}/{ChannelId}",
guild.Id,
channel.Id);
}
}
var rep = new ReplacementBuilder().WithDefault(guild.CurrentUser, channel, guild, _client).Build();
try
{
var text = SmartText.CreateFrom(repeater.Message);
text = rep.Replace(text);
var newMsg = await channel.SendAsync(text);
_ = newMsg.AddReactionAsync(new Emoji("🔄"));
if (_noRedundant.Contains(repeater.Id))
{
await SetRepeaterLastMessageInternal(repeater.Id, newMsg.Id);
repeater.LastMessageId = newMsg.Id;
}
rr.ErrorCount = 0;
}
catch (Exception ex)
{
Log.Error(ex, "[Repeater] Error sending repeat message ({ErrorCount})", rr.ErrorCount++);
}
}
private async Task RemoveRepeaterInternal(Repeater r)
{
_noRedundant.TryRemove(r.Id);
await using var uow = _db.GetDbContext();
await uow.Repeaters.DeleteAsync(x => x.Id == r.Id);
await uow.SaveChangesAsync();
}
private RunningRepeater? RemoveFromQueue(int id)
{
lock (_queueLocker)
{
var node = _repeaterQueue.FindNode(x => x.Repeater.Id == id);
if (node is null)
return null;
_repeaterQueue.Remove(node);
return node.Value;
}
}
private async Task SetRepeaterLastMessageInternal(int repeaterId, ulong lastMsgId)
{
await using var uow = _db.GetDbContext();
await uow.Repeaters.AsQueryable()
.Where(x => x.Id == repeaterId)
.UpdateAsync(rep => new()
{
LastMessageId = lastMsgId
});
}
public async Task<RunningRepeater?> AddRepeaterAsync(
ulong channelId,
ulong guildId,
TimeSpan interval,
string message,
bool isNoRedundant,
TimeSpan? startTimeOfDay)
{
var rep = new Repeater
{
ChannelId = channelId,
GuildId = guildId,
Interval = interval,
Message = message,
NoRedundant = isNoRedundant,
LastMessageId = null,
StartTimeOfDay = startTimeOfDay,
DateAdded = DateTime.UtcNow
};
await using var uow = _db.GetDbContext();
if (await uow.Repeaters.CountAsyncEF(x => x.GuildId == guildId) < MAX_REPEATERS)
uow.Repeaters.Add(rep);
else
return null;
await uow.SaveChangesAsync();
if (isNoRedundant)
_noRedundant.Add(rep.Id);
var runner = new RunningRepeater(rep);
AddToQueue(runner);
return runner;
}
public async Task<RunningRepeater?> RemoveByIndexAsync(ulong guildId, int index)
{
if (index > MAX_REPEATERS * 2)
throw new ArgumentOutOfRangeException(nameof(index));
await using var uow = _db.GetDbContext();
var toRemove = await uow.Repeaters.AsNoTracking()
.Where(x => x.GuildId == guildId)
.Skip(index)
.FirstOrDefaultAsyncEF();
if (toRemove is null)
return null;
// first try removing from queue because it can fail
// while triggering. Instruct user to try again
var removed = RemoveFromQueue(toRemove.Id);
if (removed is null)
return null;
_noRedundant.TryRemove(toRemove.Id);
uow.Repeaters.Remove(toRemove);
await uow.SaveChangesAsync();
return removed;
}
public IReadOnlyCollection<RunningRepeater> GetRepeaters(ulong guildId)
{
lock (_queueLocker)
{
return _repeaterQueue.Where(x => x.Repeater.GuildId == guildId).ToList();
}
}
public async Task<bool?> ToggleRedundantAsync(ulong guildId, int index)
{
await using var uow = _db.GetDbContext();
var toToggle = await uow.Repeaters.AsQueryable()
.Where(x => x.GuildId == guildId)
.Skip(index)
.FirstOrDefaultAsyncEF();
if (toToggle is null)
return null;
var newValue = toToggle.NoRedundant = !toToggle.NoRedundant;
if (newValue)
_noRedundant.Add(toToggle.Id);
else
_noRedundant.TryRemove(toToggle.Id);
await uow.SaveChangesAsync();
return newValue;
}
public async Task<bool?> ToggleSkipNextAsync(ulong guildId, int index)
{
await using var ctx = _db.GetDbContext();
var toSkip = await ctx.Repeaters
.Where(x => x.GuildId == guildId)
.Skip(index)
.FirstOrDefaultAsyncEF();
if (toSkip is null)
return null;
if (_skipNext.Add(toSkip.Id))
return true;
_skipNext.TryRemove(toSkip.Id);
return false;
}
public bool IsNoRedundant(int repeaterId)
=> _noRedundant.Contains(repeaterId);
public bool IsRepeaterSkipped(int repeaterId)
=> _skipNext.Contains(repeaterId);
}

View File

@@ -1,92 +0,0 @@
#nullable disable
using NadekoBot.Services.Database.Models;
namespace NadekoBot.Modules.Utility.Services;
public sealed class RunningRepeater
{
public DateTime NextTime { get; private set; }
public Repeater Repeater { get; }
public int ErrorCount { get; set; }
public RunningRepeater(Repeater repeater)
{
Repeater = repeater;
NextTime = CalculateInitialExecution();
}
public void UpdateNextTime()
=> NextTime = DateTime.UtcNow + Repeater.Interval;
private DateTime CalculateInitialExecution()
{
if (Repeater.StartTimeOfDay is not null)
{
// if there was a start time of day
// calculate whats the next time of day repeat should trigger at
// based on teh dateadded
// i know this is not null because of the check in the query
var added = Repeater.DateAdded;
// initial trigger was the time of day specified by the command.
var initialTriggerTimeOfDay = Repeater.StartTimeOfDay.Value;
DateTime initialDateTime;
// if added timeofday is less than specified timeofday for initial trigger
// that means the repeater first ran that same day at that exact specified time
if (added.TimeOfDay <= initialTriggerTimeOfDay)
// in that case, just add the difference to make sure the timeofday is the same
initialDateTime = added + (initialTriggerTimeOfDay - added.TimeOfDay);
else
// if not, then it ran at that time the following day
// in other words; Add one day, and subtract how much time passed since that time of day
initialDateTime = added + TimeSpan.FromDays(1) - (added.TimeOfDay - initialTriggerTimeOfDay);
return CalculateInitialInterval(initialDateTime);
}
// if repeater is not running daily, its initial time is the time it was Added at, plus the interval
return CalculateInitialInterval(Repeater.DateAdded + Repeater.Interval);
}
/// <summary>
/// Calculate when is the proper time to run the repeater again based on initial time repeater ran.
/// </summary>
/// <param name="initialDateTime">Initial time repeater ran at (or should run at).</param>
private DateTime CalculateInitialInterval(DateTime initialDateTime)
{
// if the initial time is greater than now, that means the repeater didn't still execute a single time.
// just schedule it
if (initialDateTime > DateTime.UtcNow)
return initialDateTime;
// else calculate based on minutes difference
// get the difference
var diff = DateTime.UtcNow - initialDateTime;
// see how many times the repeater theoretically ran already
var triggerCount = diff / Repeater.Interval;
// ok lets say repeater was scheduled to run 10h ago.
// we have an interval of 2.4h
// repeater should've ran 4 times- that's 9.6h
// next time should be in 2h from now exactly
// 10/2.4 is 4.166
// 4.166 - Math.Truncate(4.166) is 0.166
// initial interval multiplier is 1 - 0.166 = 0.834
// interval (2.4h) * 0.834 is 2.0016 and that is the initial interval
var initialIntervalMultiplier = 1 - (triggerCount - Math.Truncate(triggerCount));
return DateTime.UtcNow + (Repeater.Interval * initialIntervalMultiplier);
}
public override bool Equals(object obj)
=> obj is RunningRepeater rr && rr.Repeater.Id == Repeater.Id;
public override int GetHashCode()
=> Repeater.Id;
}

View File

@@ -1,97 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Common;
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
public partial class StreamRoleCommands : NadekoModule<StreamRoleService>
{
[Cmd]
[BotPerm(GuildPerm.ManageRoles)]
[UserPerm(GuildPerm.ManageRoles)]
[RequireContext(ContextType.Guild)]
public async Task StreamRole(IRole fromRole, IRole addRole)
{
await _service.SetStreamRole(fromRole, addRole);
await ReplyConfirmLocalizedAsync(strs.stream_role_enabled(Format.Bold(fromRole.ToString()),
Format.Bold(addRole.ToString())));
}
[Cmd]
[BotPerm(GuildPerm.ManageRoles)]
[UserPerm(GuildPerm.ManageRoles)]
[RequireContext(ContextType.Guild)]
public async Task StreamRole()
{
await _service.StopStreamRole(ctx.Guild);
await ReplyConfirmLocalizedAsync(strs.stream_role_disabled);
}
[Cmd]
[BotPerm(GuildPerm.ManageRoles)]
[UserPerm(GuildPerm.ManageRoles)]
[RequireContext(ContextType.Guild)]
public async Task StreamRoleKeyword([Leftover] string keyword = null)
{
var kw = await _service.SetKeyword(ctx.Guild, keyword);
if (string.IsNullOrWhiteSpace(keyword))
await ReplyConfirmLocalizedAsync(strs.stream_role_kw_reset);
else
await ReplyConfirmLocalizedAsync(strs.stream_role_kw_set(Format.Bold(kw)));
}
[Cmd]
[BotPerm(GuildPerm.ManageRoles)]
[UserPerm(GuildPerm.ManageRoles)]
[RequireContext(ContextType.Guild)]
public async Task StreamRoleBlacklist(AddRemove action, [Leftover] IGuildUser user)
{
var success = await _service.ApplyListAction(StreamRoleListType.Blacklist,
ctx.Guild,
action,
user.Id,
user.ToString());
if (action == AddRemove.Add)
{
if (success)
await ReplyConfirmLocalizedAsync(strs.stream_role_bl_add(Format.Bold(user.ToString())));
else
await ReplyConfirmLocalizedAsync(strs.stream_role_bl_add_fail(Format.Bold(user.ToString())));
}
else if (success)
await ReplyConfirmLocalizedAsync(strs.stream_role_bl_rem(Format.Bold(user.ToString())));
else
await ReplyErrorLocalizedAsync(strs.stream_role_bl_rem_fail(Format.Bold(user.ToString())));
}
[Cmd]
[BotPerm(GuildPerm.ManageRoles)]
[UserPerm(GuildPerm.ManageRoles)]
[RequireContext(ContextType.Guild)]
public async Task StreamRoleWhitelist(AddRemove action, [Leftover] IGuildUser user)
{
var success = await _service.ApplyListAction(StreamRoleListType.Whitelist,
ctx.Guild,
action,
user.Id,
user.ToString());
if (action == AddRemove.Add)
{
if (success)
await ReplyConfirmLocalizedAsync(strs.stream_role_wl_add(Format.Bold(user.ToString())));
else
await ReplyConfirmLocalizedAsync(strs.stream_role_wl_add_fail(Format.Bold(user.ToString())));
}
else if (success)
await ReplyConfirmLocalizedAsync(strs.stream_role_wl_rem(Format.Bold(user.ToString())));
else
await ReplyErrorLocalizedAsync(strs.stream_role_wl_rem_fail(Format.Bold(user.ToString())));
}
}
}

View File

@@ -1,339 +0,0 @@
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db;
using NadekoBot.Modules.Utility.Common;
using NadekoBot.Modules.Utility.Common.Exceptions;
using NadekoBot.Services.Database.Models;
using System.Net;
namespace NadekoBot.Modules.Utility.Services;
public class StreamRoleService : IReadyExecutor, INService
{
private readonly DbService _db;
private readonly DiscordSocketClient _client;
private readonly ConcurrentDictionary<ulong, StreamRoleSettings> _guildSettings;
private readonly QueueRunner _queueRunner;
public StreamRoleService(DiscordSocketClient client, DbService db, Bot bot)
{
_db = db;
_client = client;
_guildSettings = bot.AllGuildConfigs.ToDictionary(x => x.GuildId, x => x.StreamRole)
.Where(x => x.Value is { Enabled: true })
.ToConcurrent();
_client.PresenceUpdated += OnPresenceUpdate;
_queueRunner = new QueueRunner();
}
private Task OnPresenceUpdate(SocketUser user, SocketPresence? oldPresence, SocketPresence? newPresence)
{
_ = Task.Run(async () =>
{
if (oldPresence?.Activities?.Count != newPresence?.Activities?.Count)
{
var guildUsers = _client.Guilds
.Select(x => x.GetUser(user.Id))
.Where(x => x is not null);
foreach (var guildUser in guildUsers)
{
if (_guildSettings.TryGetValue(guildUser.Guild.Id, out var s))
await RescanUser(guildUser, s);
}
}
});
return Task.CompletedTask;
}
public Task OnReadyAsync()
=> Task.WhenAll(_client.Guilds.Select(RescanUsers).WhenAll(), _queueRunner.RunAsync());
/// <summary>
/// Adds or removes a user from a blacklist or a whitelist in the specified guild.
/// </summary>
/// <param name="listType">List type</param>
/// <param name="guild">Guild</param>
/// <param name="action">Add or rem action</param>
/// <param name="userId">User's Id</param>
/// <param name="userName">User's name#discrim</param>
/// <returns>Whether the operation was successful</returns>
public async Task<bool> ApplyListAction(
StreamRoleListType listType,
IGuild guild,
AddRemove action,
ulong userId,
string userName)
{
ArgumentNullException.ThrowIfNull(userName, nameof(userName));
var success = false;
await using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
if (listType == StreamRoleListType.Whitelist)
{
var userObj = new StreamRoleWhitelistedUser
{
UserId = userId,
Username = userName
};
if (action == AddRemove.Rem)
{
var toDelete = streamRoleSettings.Whitelist.FirstOrDefault(x => x.Equals(userObj));
if (toDelete is not null)
{
uow.Remove(toDelete);
success = true;
}
}
else
success = streamRoleSettings.Whitelist.Add(userObj);
}
else
{
var userObj = new StreamRoleBlacklistedUser
{
UserId = userId,
Username = userName
};
if (action == AddRemove.Rem)
{
var toRemove = streamRoleSettings.Blacklist.FirstOrDefault(x => x.Equals(userObj));
if (toRemove is not null)
success = streamRoleSettings.Blacklist.Remove(toRemove);
}
else
success = streamRoleSettings.Blacklist.Add(userObj);
}
await uow.SaveChangesAsync();
UpdateCache(guild.Id, streamRoleSettings);
}
if (success)
await RescanUsers(guild);
return success;
}
/// <summary>
/// Sets keyword on a guild and updates the cache.
/// </summary>
/// <param name="guild">Guild Id</param>
/// <param name="keyword">Keyword to set</param>
/// <returns>The keyword set</returns>
public async Task<string?> SetKeyword(IGuild guild, string? keyword)
{
keyword = keyword?.Trim().ToLowerInvariant();
await using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
streamRoleSettings.Keyword = keyword;
UpdateCache(guild.Id, streamRoleSettings);
await uow.SaveChangesAsync();
}
await RescanUsers(guild);
return keyword;
}
/// <summary>
/// Gets the currently set keyword on a guild.
/// </summary>
/// <param name="guildId">Guild Id</param>
/// <returns>The keyword set</returns>
public string GetKeyword(ulong guildId)
{
if (_guildSettings.TryGetValue(guildId, out var outSetting))
return outSetting.Keyword;
StreamRoleSettings setting;
using (var uow = _db.GetDbContext())
{
setting = uow.GetStreamRoleSettings(guildId);
}
UpdateCache(guildId, setting);
return setting.Keyword;
}
/// <summary>
/// Sets the role to monitor, and a role to which to add to
/// the user who starts streaming in the monitored role.
/// </summary>
/// <param name="fromRole">Role to monitor</param>
/// <param name="addRole">Role to add to the user</param>
public async Task SetStreamRole(IRole fromRole, IRole addRole)
{
ArgumentNullException.ThrowIfNull(fromRole, nameof(fromRole));
ArgumentNullException.ThrowIfNull(addRole, nameof(addRole));
StreamRoleSettings setting;
await using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow.GetStreamRoleSettings(fromRole.Guild.Id);
streamRoleSettings.Enabled = true;
streamRoleSettings.AddRoleId = addRole.Id;
streamRoleSettings.FromRoleId = fromRole.Id;
setting = streamRoleSettings;
await uow.SaveChangesAsync();
}
UpdateCache(fromRole.Guild.Id, setting);
foreach (var usr in await fromRole.GetMembersAsync())
{
await RescanUser(usr, setting, addRole);
}
}
/// <summary>
/// Stops the stream role feature on the specified guild.
/// </summary>
/// <param name="guild">Guild</param>
/// <param name="cleanup">Whether to rescan users</param>
public async Task StopStreamRole(IGuild guild, bool cleanup = false)
{
await using (var uow = _db.GetDbContext())
{
var streamRoleSettings = uow.GetStreamRoleSettings(guild.Id);
streamRoleSettings.Enabled = false;
streamRoleSettings.AddRoleId = 0;
streamRoleSettings.FromRoleId = 0;
await uow.SaveChangesAsync();
}
if (_guildSettings.TryRemove(guild.Id, out _) && cleanup)
await RescanUsers(guild);
}
private async ValueTask RescanUser(IGuildUser user, StreamRoleSettings setting, IRole? addRole = null)
=> await _queueRunner.EnqueueAsync(() => RescanUserInternal(user, setting, addRole));
private async Task RescanUserInternal(IGuildUser user, StreamRoleSettings setting, IRole? addRole = null)
{
if (user.IsBot)
return;
var g = (StreamingGame?)user.Activities.FirstOrDefault(a
=> a is StreamingGame
&& (string.IsNullOrWhiteSpace(setting.Keyword)
|| a.Name.ToUpperInvariant().Contains(setting.Keyword.ToUpperInvariant())
|| setting.Whitelist.Any(x => x.UserId == user.Id)));
if (g is not null
&& setting.Enabled
&& setting.Blacklist.All(x => x.UserId != user.Id)
&& user.RoleIds.Contains(setting.FromRoleId))
{
await _queueRunner.EnqueueAsync(async () =>
{
try
{
addRole ??= user.Guild.GetRole(setting.AddRoleId);
if (addRole is null)
{
await StopStreamRole(user.Guild);
Log.Warning("Stream role in server {RoleId} no longer exists. Stopping", setting.AddRoleId);
return;
}
//check if he doesn't have addrole already, to avoid errors
if (!user.RoleIds.Contains(addRole.Id))
{
await user.AddRoleAsync(addRole);
Log.Information("Added stream role to user {User} in {Server} server",
user.ToString(),
user.Guild.ToString());
}
}
catch (HttpException ex) when (ex.HttpCode == HttpStatusCode.Forbidden)
{
await StopStreamRole(user.Guild);
Log.Warning(ex, "Error adding stream role(s). Forcibly disabling stream role feature");
throw new StreamRolePermissionException();
}
catch (Exception ex)
{
Log.Warning(ex, "Failed adding stream role");
}
});
}
else
{
//check if user is in the addrole
if (user.RoleIds.Contains(setting.AddRoleId))
{
await _queueRunner.EnqueueAsync(async () =>
{
try
{
addRole ??= user.Guild.GetRole(setting.AddRoleId);
if (addRole is null)
{
await StopStreamRole(user.Guild);
Log.Warning(
"Addrole doesn't exist in {GuildId} server. Forcibly disabling stream role feature",
user.Guild.Id);
return;
}
// need to check again in case queuer is taking too long to execute
if (user.RoleIds.Contains(setting.AddRoleId))
{
await user.RemoveRoleAsync(addRole);
}
Log.Information("Removed stream role from the user {User} in {Server} server",
user.ToString(),
user.Guild.ToString());
}
catch (HttpException ex)
{
if (ex.HttpCode == HttpStatusCode.Forbidden)
{
await StopStreamRole(user.Guild);
Log.Warning(ex, "Error removing stream role(s). Forcibly disabling stream role feature");
}
}
});
}
}
}
private async Task RescanUsers(IGuild guild)
{
if (!_guildSettings.TryGetValue(guild.Id, out var setting))
return;
var addRole = guild.GetRole(setting.AddRoleId);
if (addRole is null)
return;
if (setting.Enabled)
{
var users = await guild.GetUsersAsync(CacheMode.CacheOnly);
foreach (var usr in users.Where(x
=> x.RoleIds.Contains(setting.FromRoleId) || x.RoleIds.Contains(addRole.Id)))
{
if (usr is { } x)
await RescanUser(x, setting, addRole);
}
}
}
private void UpdateCache(ulong guildId, StreamRoleSettings setting)
=> _guildSettings.AddOrUpdate(guildId, _ => setting, (_, _) => setting);
}

View File

@@ -1,99 +0,0 @@
#nullable disable
using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Modules.Utility.Common;
using System.Text.Json;
using System.Text.Json.Serialization;
namespace NadekoBot.Modules.Utility.Services;
public class ConverterService : INService, IReadyExecutor
{
private static readonly TypedKey<List<ConvertUnit>> _convertKey =
new("convert:units");
private readonly TimeSpan _updateInterval = new(12, 0, 0);
private readonly DiscordSocketClient _client;
private readonly IBotCache _cache;
private readonly IHttpClientFactory _httpFactory;
public ConverterService(
DiscordSocketClient client,
IBotCache cache,
IHttpClientFactory factory)
{
_client = client;
_cache = cache;
_httpFactory = factory;
}
public async Task OnReadyAsync()
{
if (_client.ShardId != 0)
return;
using var timer = new PeriodicTimer(_updateInterval);
do
{
try
{
await UpdateCurrency();
}
catch
{
// ignored
}
} while (await timer.WaitForNextTickAsync());
}
private async Task<Rates> GetCurrencyRates()
{
using var http = _httpFactory.CreateClient();
var res = await http.GetStringAsync("https://convertapi.nadeko.bot/latest");
return JsonSerializer.Deserialize<Rates>(res);
}
private async Task UpdateCurrency()
{
var unitTypeString = "currency";
var currencyRates = await GetCurrencyRates();
var baseType = new ConvertUnit
{
Triggers = new[] { currencyRates.Base },
Modifier = decimal.One,
UnitType = unitTypeString
};
var units = currencyRates.ConversionRates.Select(u => new ConvertUnit
{
Triggers = new[] { u.Key },
Modifier = u.Value,
UnitType = unitTypeString
})
.ToList();
var stream = File.OpenRead("data/units.json");
var defaultUnits = await JsonSerializer.DeserializeAsync<ConvertUnit[]>(stream);
if(defaultUnits is not null)
units.AddRange(defaultUnits);
units.Add(baseType);
await _cache.AddAsync(_convertKey, units);
}
public async Task<IReadOnlyList<ConvertUnit>> GetUnitsAsync()
=> (await _cache.GetAsync(_convertKey)).TryGetValue(out var list)
? list
: Array.Empty<ConvertUnit>();
}
public class Rates
{
[JsonPropertyName("base")]
public string Base { get; set; }
[JsonPropertyName("date")]
public DateTime Date { get; set; }
[JsonPropertyName("rates")]
public Dictionary<string, decimal> ConversionRates { get; set; }
}

View File

@@ -1,96 +0,0 @@
#nullable disable
using NadekoBot.Modules.Utility.Services;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class UnitConverterCommands : NadekoModule<ConverterService>
{
[Cmd]
public async Task ConvertList()
{
var units = await _service.GetUnitsAsync();
var embed = _eb.Create().WithTitle(GetText(strs.convertlist)).WithOkColor();
foreach (var g in units.GroupBy(x => x.UnitType))
{
embed.AddField(g.Key.ToTitleCase(),
string.Join(", ", g.Select(x => x.Triggers.FirstOrDefault()).OrderBy(x => x)));
}
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[Priority(0)]
public async Task Convert(string origin, string target, decimal value)
{
var units = await _service.GetUnitsAsync();
var originUnit = units.FirstOrDefault(x
=> x.Triggers.Select(y => y.ToUpperInvariant()).Contains(origin.ToUpperInvariant()));
var targetUnit = units.FirstOrDefault(x
=> x.Triggers.Select(y => y.ToUpperInvariant()).Contains(target.ToUpperInvariant()));
if (originUnit is null || targetUnit is null)
{
await ReplyErrorLocalizedAsync(strs.convert_not_found(Format.Bold(origin), Format.Bold(target)));
return;
}
if (originUnit.UnitType != targetUnit.UnitType)
{
await ReplyErrorLocalizedAsync(strs.convert_type_error(Format.Bold(originUnit.Triggers.First()),
Format.Bold(targetUnit.Triggers.First())));
return;
}
decimal res;
if (originUnit.Triggers == targetUnit.Triggers)
res = value;
else if (originUnit.UnitType == "temperature")
{
//don't really care too much about efficiency, so just convert to Kelvin, then to target
switch (originUnit.Triggers.First().ToUpperInvariant())
{
case "C":
res = value + 273.15m; //celcius!
break;
case "F":
res = (value + 459.67m) * (5m / 9m);
break;
default:
res = value;
break;
}
//from Kelvin to target
switch (targetUnit.Triggers.First().ToUpperInvariant())
{
case "C":
res -= 273.15m; //celcius!
break;
case "F":
res = (res * (9m / 5m)) - 459.67m;
break;
}
}
else
{
if (originUnit.UnitType == "currency")
res = value * targetUnit.Modifier / originUnit.Modifier;
else
res = value * originUnit.Modifier / targetUnit.Modifier;
}
res = Math.Round(res, 4);
await SendConfirmAsync(GetText(strs.convert(value,
originUnit.Triggers.Last(),
res,
targetUnit.Triggers.Last())));
}
}
}

View File

@@ -1,637 +0,0 @@
#nullable disable
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
using NadekoBot.Modules.Utility.Services;
using Newtonsoft.Json;
using System.Diagnostics;
using System.Text;
using System.Text.Json;
using System.Text.Json.Serialization;
using Nadeko.Common;
using SystemTextJsonSamples;
namespace NadekoBot.Modules.Utility;
public partial class Utility : NadekoModule
{
public enum CreateInviteType
{
Any,
New
}
public enum MeOrBot { Me, Bot }
private static readonly JsonSerializerOptions _showEmbedSerializerOptions = new()
{
WriteIndented = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNamingPolicy = LowerCaseNamingPolicy.Default
};
private static SemaphoreSlim sem = new(1, 1);
private readonly DiscordSocketClient _client;
private readonly ICoordinator _coord;
private readonly IStatsService _stats;
private readonly IBotCredentials _creds;
private readonly DownloadTracker _tracker;
private readonly IHttpClientFactory _httpFactory;
private readonly VerboseErrorsService _veService;
public Utility(
DiscordSocketClient client,
ICoordinator coord,
IStatsService stats,
IBotCredentials creds,
DownloadTracker tracker,
IHttpClientFactory httpFactory,
VerboseErrorsService veService)
{
_client = client;
_coord = coord;
_stats = stats;
_creds = creds;
_tracker = tracker;
_httpFactory = httpFactory;
_veService = veService;
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(1)]
public async Task Say(ITextChannel channel, [Leftover] SmartText message)
{
var rep = new ReplacementBuilder()
.WithDefault(ctx.User, channel, (SocketGuild)ctx.Guild, (DiscordSocketClient)ctx.Client)
.Build();
message = rep.Replace(message);
await channel.SendAsync(message, !((IGuildUser)ctx.User).GuildPermissions.MentionEveryone);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
[Priority(0)]
public Task Say([Leftover] SmartText message)
=> Say((ITextChannel)ctx.Channel, message);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task WhosPlaying([Leftover] string game)
{
game = game?.Trim().ToUpperInvariant();
if (string.IsNullOrWhiteSpace(game))
return;
if (ctx.Guild is not SocketGuild socketGuild)
{
Log.Warning("Can't cast guild to socket guild");
return;
}
var rng = new NadekoRandom();
var arr = await Task.Run(() => socketGuild.Users
.Where(u => u.Activities.FirstOrDefault()?.Name?.ToUpperInvariant()
== game)
.Select(u => u.Username)
.OrderBy(_ => rng.Next())
.Take(60)
.ToArray());
var i = 0;
if (arr.Length == 0)
await ReplyErrorLocalizedAsync(strs.nobody_playing_game);
else
{
await SendConfirmAsync("```css\n"
+ string.Join("\n",
arr.GroupBy(_ => i++ / 2)
.Select(ig => string.Concat(ig.Select(el => $"• {el,-27}"))))
+ "\n```");
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(0)]
public async Task InRole(int page, [Leftover] IRole role = null)
{
if (--page < 0)
return;
await ctx.Channel.TriggerTypingAsync();
await _tracker.EnsureUsersDownloadedAsync(ctx.Guild);
var users = await ctx.Guild.GetUsersAsync(
CacheMode.CacheOnly
);
var roleUsers = users.Where(u => role is null ? u.RoleIds.Count == 1 : u.RoleIds.Contains(role.Id))
.Select(u => $"`{u.Id,18}` {u}")
.ToArray();
await ctx.SendPaginatedConfirmAsync(page,
cur =>
{
var pageUsers = roleUsers.Skip(cur * 20).Take(20).ToList();
if (pageUsers.Count == 0)
return _eb.Create().WithOkColor().WithDescription(GetText(strs.no_user_on_this_page));
return _eb.Create()
.WithOkColor()
.WithTitle(GetText(strs.inrole_list(Format.Bold(role?.Name ?? "No Role"), roleUsers.Length)))
.WithDescription(string.Join("\n", pageUsers));
},
roleUsers.Length,
20);
}
[Cmd]
[RequireContext(ContextType.Guild)]
[Priority(1)]
public Task InRole([Leftover] IRole role = null)
=> InRole(1, role);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task CheckPerms(MeOrBot who = MeOrBot.Me)
{
var builder = new StringBuilder();
var user = who == MeOrBot.Me ? (IGuildUser)ctx.User : ((SocketGuild)ctx.Guild).CurrentUser;
var perms = user.GetPermissions((ITextChannel)ctx.Channel);
foreach (var p in perms.GetType()
.GetProperties()
.Where(static p =>
{
var method = p.GetGetMethod();
if (method is null)
return false;
return !method.GetParameters().Any();
}))
builder.AppendLine($"{p.Name} : {p.GetValue(perms, null)}");
await SendConfirmAsync(builder.ToString());
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task UserId([Leftover] IGuildUser target = null)
{
var usr = target ?? ctx.User;
await ReplyConfirmLocalizedAsync(strs.userid("🆔",
Format.Bold(usr.ToString()),
Format.Code(usr.Id.ToString())));
}
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task RoleId([Leftover] IRole role)
=> await ReplyConfirmLocalizedAsync(strs.roleid("🆔",
Format.Bold(role.ToString()),
Format.Code(role.Id.ToString())));
[Cmd]
public async Task ChannelId()
=> await ReplyConfirmLocalizedAsync(strs.channelid("🆔", Format.Code(ctx.Channel.Id.ToString())));
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task ServerId()
=> await ReplyConfirmLocalizedAsync(strs.serverid("🆔", Format.Code(ctx.Guild.Id.ToString())));
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task Roles(IGuildUser target, int page = 1)
{
var guild = ctx.Guild;
const int rolesPerPage = 20;
if (page is < 1 or > 100)
return;
if (target is not null)
{
var roles = target.GetRoles()
.Except(new[] { guild.EveryoneRole })
.OrderBy(r => -r.Position)
.Skip((page - 1) * rolesPerPage)
.Take(rolesPerPage)
.ToArray();
if (!roles.Any())
await ReplyErrorLocalizedAsync(strs.no_roles_on_page);
else
{
await SendConfirmAsync(GetText(strs.roles_page(page, Format.Bold(target.ToString()))),
"\n• " + string.Join("\n• ", (IEnumerable<IRole>)roles).SanitizeMentions(true));
}
}
else
{
var roles = guild.Roles.Except(new[] { guild.EveryoneRole })
.OrderBy(r => -r.Position)
.Skip((page - 1) * rolesPerPage)
.Take(rolesPerPage)
.ToArray();
if (!roles.Any())
await ReplyErrorLocalizedAsync(strs.no_roles_on_page);
else
{
await SendConfirmAsync(GetText(strs.roles_all_page(page)),
"\n• " + string.Join("\n• ", (IEnumerable<IRole>)roles).SanitizeMentions(true));
}
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
public Task Roles(int page = 1)
=> Roles(null, page);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task ChannelTopic([Leftover] ITextChannel channel = null)
{
if (channel is null)
channel = (ITextChannel)ctx.Channel;
var topic = channel.Topic;
if (string.IsNullOrWhiteSpace(topic))
await ReplyErrorLocalizedAsync(strs.no_topic_set);
else
await SendConfirmAsync(GetText(strs.channel_topic), topic);
}
[Cmd]
public async Task Stats()
{
var ownerIds = string.Join("\n", _creds.OwnerIds);
if (string.IsNullOrWhiteSpace(ownerIds))
ownerIds = "-";
await ctx.Channel.EmbedAsync(_eb.Create()
.WithOkColor()
.WithAuthor($"NadekoBot v{StatsService.BOT_VERSION}",
"https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/avatar.png",
"https://nadekobot.readthedocs.io/en/latest/")
.AddField(GetText(strs.author), _stats.Author, true)
.AddField(GetText(strs.botid), _client.CurrentUser.Id.ToString(), true)
.AddField(GetText(strs.shard),
$"#{_client.ShardId} / {_creds.TotalShards}",
true)
.AddField(GetText(strs.commands_ran), _stats.CommandsRan.ToString(), true)
.AddField(GetText(strs.messages),
$"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)",
true)
.AddField(GetText(strs.memory),
FormattableString.Invariant($"{_stats.GetPrivateMemoryMegabytes():F2} MB"),
true)
.AddField(GetText(strs.owner_ids), ownerIds, true)
.AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true)
.AddField(GetText(strs.presence),
GetText(strs.presence_txt(_coord.GetGuildCount(),
_stats.TextChannels,
_stats.VoiceChannels)),
true));
}
[Cmd]
public async Task
Showemojis([Leftover] string _) // need to have the parameter so that the message.tags gets populated
{
var tags = ctx.Message.Tags.Where(t => t.Type == TagType.Emoji).Select(t => (Emote)t.Value);
var result = string.Join("\n", tags.Select(m => GetText(strs.showemojis(m, m.Url))));
if (string.IsNullOrWhiteSpace(result))
await ReplyErrorLocalizedAsync(strs.showemojis_none);
else
await ctx.Channel.SendMessageAsync(result.TrimTo(2000));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(GuildPerm.ManageEmojisAndStickers)]
[UserPerm(GuildPerm.ManageEmojisAndStickers)]
[Priority(2)]
public Task EmojiAdd(string name, Emote emote)
=> EmojiAdd(name, emote.Url);
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(GuildPerm.ManageEmojisAndStickers)]
[UserPerm(GuildPerm.ManageEmojisAndStickers)]
[Priority(1)]
public Task EmojiAdd(Emote emote)
=> EmojiAdd(emote.Name, emote.Url);
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(GuildPerm.ManageEmojisAndStickers)]
[UserPerm(GuildPerm.ManageEmojisAndStickers)]
[Priority(0)]
public async Task EmojiAdd(string name, string url = null)
{
name = name.Trim(':');
url ??= ctx.Message.Attachments.FirstOrDefault()?.Url;
if (url is null)
return;
using var http = _httpFactory.CreateClient();
using var res = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
if (!res.IsImage() || res.GetContentLength() > 262_144)
{
await ReplyErrorLocalizedAsync(strs.invalid_emoji_link);
return;
}
await using var imgStream = await res.Content.ReadAsStreamAsync();
Emote em;
try
{
em = await ctx.Guild.CreateEmoteAsync(name, new(imgStream));
}
catch (Exception ex)
{
Log.Warning(ex, "Error adding emoji on server {GuildId}", ctx.Guild.Id);
await ReplyErrorLocalizedAsync(strs.emoji_add_error);
return;
}
await ConfirmLocalizedAsync(strs.emoji_added(em.ToString()));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(GuildPerm.ManageEmojisAndStickers)]
[UserPerm(GuildPerm.ManageEmojisAndStickers)]
[Priority(0)]
public async Task EmojiRemove(params Emote[] emotes)
{
if (emotes.Length == 0)
return;
var g = (SocketGuild)ctx.Guild;
var fails = new List<Emote>();
foreach (var emote in emotes)
{
var guildEmote = g.Emotes.FirstOrDefault(x => x.Id == emote.Id);
if (guildEmote is null)
{
fails.Add(emote);
}
else
{
await ctx.Guild.DeleteEmoteAsync(guildEmote);
}
}
if (fails.Count > 0)
{
await ReplyPendingLocalizedAsync(strs.emoji_not_removed(fails.Select(x => x.ToString()).Join(" ")));
return;
}
await ctx.OkAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
[BotPerm(GuildPerm.ManageEmojisAndStickers)]
[UserPerm(GuildPerm.ManageEmojisAndStickers)]
public async Task StickerAdd(string name = null, string description = null, params string[] tags)
{
string format;
Stream stream;
if (ctx.Message.Stickers.Count is 1 && ctx.Message.Stickers.First() is SocketSticker ss)
{
name ??= ss.Name;
description = ss.Description;
tags = tags is null or { Length: 0 } ? ss.Tags.ToArray() : tags;
format = FormatToExtension(ss.Format);
using var http = _httpFactory.CreateClient();
stream = await http.GetStreamAsync(ss.GetStickerUrl());
}
// else if (ctx.Message.Attachments.FirstOrDefault() is { } attachment)
// {
// var url = attachment?.Url;
//
// if (url is null)
// return;
//
// if (name is null)
// {
// await ReplyErrorLocalizedAsync(strs.sticker_missing_name);
// return;
// }
//
// format = Path.GetExtension(attachment.Filename);
//
// if (attachment is not { Width: 300, Height: 300 })
// {
// await ReplyErrorLocalizedAsync(strs.sticker_invalid_size);
// return;
// }
//
// using var http = _httpFactory.CreateClient();
//
// using var res = await http.GetAsync(url, HttpCompletionOption.ResponseHeadersRead);
// if (res.GetContentLength() > 512.Kilobytes().Bytes)
// {
// await ReplyErrorLocalizedAsync(strs.invalid_emoji_link);
// return;
// }
//
// stream = await res.Content.ReadAsStreamAsync();
// }
else
{
await ReplyErrorLocalizedAsync(strs.sticker_error);
return;
}
try
{
if (tags.Length == 0)
tags = new[] { name };
await ctx.Guild.CreateStickerAsync(name,
string.IsNullOrWhiteSpace(description) ? "Missing description" : description,
tags,
stream,
$"{name}.{format}");
await ctx.OkAsync();
}
catch (Exception ex)
{
Log.Warning(ex, "Error occurred while adding a sticker: {Message}", ex.Message);
await ReplyErrorLocalizedAsync(strs.error_occured);
}
finally
{
await stream.DisposeAsync();
}
}
private static string FormatToExtension(StickerFormatType format)
{
switch (format)
{
case StickerFormatType.None:
case StickerFormatType.Png:
case StickerFormatType.Apng:
return "png";
case StickerFormatType.Lottie:
return "lottie";
default:
throw new ArgumentException(nameof (format));
}
}
[Cmd]
[OwnerOnly]
public async Task ListServers(int page = 1)
{
page -= 1;
if (page < 0)
return;
var guilds = _client.Guilds.OrderBy(g => g.Name)
.Skip(page * 15)
.Take(15)
.ToList();
if (!guilds.Any())
{
await ReplyErrorLocalizedAsync(strs.listservers_none);
return;
}
var embed = _eb.Create().WithOkColor();
foreach (var guild in guilds)
embed.AddField(guild.Name, GetText(strs.listservers(guild.Id, guild.MemberCount, guild.OwnerId)));
await ctx.Channel.EmbedAsync(embed);
}
[Cmd]
[RequireContext(ContextType.Guild)]
public Task ShowEmbed(ulong messageId)
=> ShowEmbed((ITextChannel)ctx.Channel, messageId);
[Cmd]
[RequireContext(ContextType.Guild)]
public async Task ShowEmbed(ITextChannel ch, ulong messageId)
{
var user = (IGuildUser)ctx.User;
var perms = user.GetPermissions(ch);
if (!perms.ReadMessageHistory || !perms.ViewChannel)
{
await ReplyErrorLocalizedAsync(strs.insuf_perms_u);
return;
}
var msg = await ch.GetMessageAsync(messageId);
if (msg is null)
{
await ReplyErrorLocalizedAsync(strs.msg_not_found);
return;
}
var embed = msg.Embeds.FirstOrDefault();
if (embed is null)
{
await ReplyErrorLocalizedAsync(strs.not_found);
return;
}
var json = SmartEmbedText.FromEmbed(embed, msg.Content).ToJson(_showEmbedSerializerOptions);
await SendConfirmAsync(Format.Sanitize(json).Replace("](", "]\\("));
}
[Cmd]
[RequireContext(ContextType.Guild)]
[OwnerOnly]
public async Task SaveChat(int cnt)
{
var msgs = new List<IMessage>(cnt);
await ctx.Channel.GetMessagesAsync(cnt).ForEachAsync(dled => msgs.AddRange(dled));
var title = $"Chatlog-{ctx.Guild.Name}/#{ctx.Channel.Name}-{DateTime.Now}.txt";
var grouping = msgs.GroupBy(x => $"{x.CreatedAt.Date:dd.MM.yyyy}")
.Select(g => new
{
date = g.Key,
messages = g.OrderBy(x => x.CreatedAt)
.Select(s =>
{
var msg = $"【{s.Timestamp:HH:mm:ss}】{s.Author}:";
if (string.IsNullOrWhiteSpace(s.ToString()))
{
if (s.Attachments.Any())
{
msg += "FILES_UPLOADED: "
+ string.Join("\n", s.Attachments.Select(x => x.Url));
}
else if (s.Embeds.Any())
{
msg += "EMBEDS: "
+ string.Join("\n--------\n",
s.Embeds.Select(x
=> $"Description: {x.Description}"));
}
}
else
msg += s.ToString();
return msg;
})
});
await using var stream = await JsonConvert.SerializeObject(grouping, Formatting.Indented).ToStream();
await ctx.User.SendFileAsync(stream, title, title);
}
[Cmd]
public async Task Ping()
{
await sem.WaitAsync(5000);
try
{
var sw = Stopwatch.StartNew();
var msg = await ctx.Channel.SendMessageAsync("🏓");
sw.Stop();
msg.DeleteAfter(0);
await SendConfirmAsync($"{Format.Bold(ctx.User.ToString())} 🏓 {(int)sw.Elapsed.TotalMilliseconds}ms");
}
finally
{
sem.Release();
}
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(GuildPerm.ManageMessages)]
public async Task VerboseError(bool? newstate = null)
{
var state = _veService.ToggleVerboseErrors(ctx.Guild.Id, newstate);
if (state)
await ReplyConfirmLocalizedAsync(strs.verbose_errors_enabled);
else
await ReplyConfirmLocalizedAsync(strs.verbose_errors_disabled);
}
}

View File

@@ -1,76 +0,0 @@
#nullable disable
using Microsoft.CodeAnalysis.CSharp.Scripting;
using Microsoft.CodeAnalysis.Scripting;
namespace NadekoBot.Modules.Utility;
public partial class Utility
{
[Group]
public partial class EvalCommands : NadekoModule
{
private readonly IServiceProvider _services;
public EvalCommands(IServiceProvider services)
{
_services = services;
}
[Cmd]
[NoPublicBot]
[OwnerOnly]
public async Task Eval([Leftover] string scriptText)
{
_ = ctx.Channel.TriggerTypingAsync();
if (scriptText.StartsWith("```cs"))
scriptText = scriptText[5..];
else if (scriptText.StartsWith("```"))
scriptText = scriptText[3..];
if (scriptText.EndsWith("```"))
scriptText = scriptText[..^3];
var script = CSharpScript.Create(scriptText,
ScriptOptions.Default
.WithReferences(this.GetType().Assembly)
.WithImports(
"System",
"NadekoBot",
"NadekoBot.Extensions",
"Microsoft.Extensions.DependencyInjection",
"NadekoBot.Common",
"System.Text",
"System.Text.Json"),
globalsType: typeof(EvalGlobals));
try
{
var result = await script.RunAsync(new EvalGlobals()
{
ctx = this.ctx,
guild = this.ctx.Guild,
channel = this.ctx.Channel,
user = this.ctx.User,
self = this,
services = _services
});
var output = result.ReturnValue?.ToString();
if (!string.IsNullOrWhiteSpace(output))
{
var eb = _eb.Create(ctx)
.WithOkColor()
.AddField("Code", scriptText)
.AddField("Output", output.TrimTo(512)!);
_ = ctx.Channel.EmbedAsync(eb);
}
}
catch (Exception ex)
{
await SendErrorAsync(ex.Message);
}
}
}
}

View File

@@ -1,13 +0,0 @@
// ReSharper disable InconsistentNaming
#nullable disable
namespace NadekoBot.Modules.Utility;
public class EvalGlobals
{
public ICommandContext ctx;
public Utility.EvalCommands self;
public IUser user;
public IMessageChannel channel;
public IGuild guild;
public IServiceProvider services;
}

View File

@@ -1,70 +0,0 @@
#nullable disable
using NadekoBot.Db;
using NadekoBot.Modules.Help.Services;
namespace NadekoBot.Modules.Utility.Services;
public class VerboseErrorsService : INService
{
private readonly ConcurrentHashSet<ulong> _guildsDisabled;
private readonly DbService _db;
private readonly CommandHandler _ch;
private readonly HelpService _hs;
public VerboseErrorsService(
Bot bot,
DbService db,
CommandHandler ch,
HelpService hs)
{
_db = db;
_ch = ch;
_hs = hs;
_ch.CommandErrored += LogVerboseError;
_guildsDisabled = new(bot.AllGuildConfigs.Where(x => !x.VerboseErrors).Select(x => x.GuildId));
}
private async Task LogVerboseError(CommandInfo cmd, ITextChannel channel, string reason)
{
if (channel is null || _guildsDisabled.Contains(channel.GuildId))
return;
try
{
var embed = _hs.GetCommandHelp(cmd, channel.Guild)
.WithTitle("Command Error")
.WithDescription(reason)
.WithFooter("Admin may disable verbose errors via `.ve` command")
.WithErrorColor();
await channel.EmbedAsync(embed);
}
catch
{
Log.Information("Verbose error wasn't able to be sent to the server: {GuildId}",
channel.GuildId);
}
}
public bool ToggleVerboseErrors(ulong guildId, bool? maybeEnabled = null)
{
using var uow = _db.GetDbContext();
var gc = uow.GuildConfigsForId(guildId, set => set);
if (maybeEnabled is bool isEnabled) // set it
gc.VerboseErrors = isEnabled;
else // toggle it
isEnabled = gc.VerboseErrors = !gc.VerboseErrors;
uow.SaveChanges();
if (isEnabled) // This doesn't need to be duplicated inside the using block
_guildsDisabled.TryRemove(guildId);
else
_guildsDisabled.Add(guildId);
return isEnabled;
}
}

View File

@@ -1,12 +0,0 @@
#nullable disable
using System.Diagnostics;
namespace NadekoBot.Modules.Utility.Common;
[DebuggerDisplay("Type: {UnitType} Trigger: {Triggers[0]} Mod: {Modifier}")]
public class ConvertUnit
{
public string[] Triggers { get; set; }
public string UnitType { get; set; }
public decimal Modifier { get; set; }
}

View File

@@ -1,20 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility.Common.Exceptions;
public class StreamRoleNotFoundException : Exception
{
public StreamRoleNotFoundException()
: base("Stream role wasn't found.")
{
}
public StreamRoleNotFoundException(string message)
: base(message)
{
}
public StreamRoleNotFoundException(string message, Exception innerException)
: base(message, innerException)
{
}
}

View File

@@ -1,20 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility.Common.Exceptions;
public class StreamRolePermissionException : Exception
{
public StreamRolePermissionException()
: base("Stream role was unable to be applied.")
{
}
public StreamRolePermissionException(string message)
: base(message)
{
}
public StreamRolePermissionException(string message, Exception innerException)
: base(message, innerException)
{
}
}

View File

@@ -1,134 +0,0 @@
#nullable disable
using System.Text.Json.Serialization;
namespace NadekoBot.Modules.Utility.Common.Patreon;
public sealed class Attributes
{
[JsonPropertyName("full_name")]
public string FullName { get; set; }
[JsonPropertyName("is_follower")]
public bool IsFollower { get; set; }
[JsonPropertyName("last_charge_date")]
public DateTime? LastChargeDate { get; set; }
[JsonPropertyName("last_charge_status")]
public string LastChargeStatus { get; set; }
[JsonPropertyName("lifetime_support_cents")]
public int LifetimeSupportCents { get; set; }
[JsonPropertyName("currently_entitled_amount_cents")]
public int CurrentlyEntitledAmountCents { get; set; }
[JsonPropertyName("patron_status")]
public string PatronStatus { get; set; }
}
public sealed class Data
{
[JsonPropertyName("id")]
public string Id { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
}
public sealed class Address
{
[JsonPropertyName("data")]
public Data Data { get; set; }
}
// public sealed class CurrentlyEntitledTiers
// {
// [JsonPropertyName("data")]
// public List<Datum> Data { get; set; }
// }
// public sealed class Relationships
// {
// [JsonPropertyName("address")]
// public Address Address { get; set; }
//
// // [JsonPropertyName("currently_entitled_tiers")]
// // public CurrentlyEntitledTiers CurrentlyEntitledTiers { get; set; }
// }
public sealed class PatreonMembersResponse
{
[JsonPropertyName("data")]
public List<PatreonMember> Data { get; set; }
[JsonPropertyName("included")]
public List<PatreonUser> Included { get; set; }
[JsonPropertyName("links")]
public PatreonLinks Links { get; set; }
}
public sealed class PatreonLinks
{
[JsonPropertyName("next")]
public string Next { get; set; }
}
public sealed class PatreonUser
{
[JsonPropertyName("attributes")]
public PatreonUserAttributes Attributes { get; set; }
[JsonPropertyName("id")]
public string Id { get; set; }
// public string Type { get; set; }
}
public sealed class PatreonUserAttributes
{
[JsonPropertyName("social_connections")]
public PatreonSocials SocialConnections { get; set; }
}
public sealed class PatreonSocials
{
[JsonPropertyName("discord")]
public DiscordSocial Discord { get; set; }
}
public sealed class DiscordSocial
{
[JsonPropertyName("user_id")]
public string UserId { get; set; }
}
public sealed class PatreonMember
{
[JsonPropertyName("attributes")]
public Attributes Attributes { get; set; }
[JsonPropertyName("relationships")]
public Relationships Relationships { get; set; }
[JsonPropertyName("type")]
public string Type { get; set; }
}
public sealed class Relationships
{
[JsonPropertyName("user")]
public PatreonRelationshipUser User { get; set; }
}
public sealed class PatreonRelationshipUser
{
[JsonPropertyName("data")]
public PatreonUserData Data { get; set; }
}
public sealed class PatreonUserData
{
[JsonPropertyName("id")]
public string Id { get; set; }
}

View File

@@ -1,8 +0,0 @@
#nullable disable
namespace NadekoBot.Modules.Utility.Common;
public enum StreamRoleListType
{
Whitelist,
Blacklist
}