1
Fork 0
mirror of https://gitlab.com/Kwoth/nadekobot.git synced 2024-10-02 20:13:13 +00:00

* add: .prunecancel to cancel active prunes

* change: .qs improved with thumbnails
* change: .prune now reports progress
* dev: DryIoc replacing Ninject
This commit is contained in:
Kwoth 2024-05-04 06:33:45 +00:00
parent 7637de8fed
commit ea0b51d474
22 changed files with 418 additions and 235 deletions

View file

@ -10,7 +10,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- type `.h .shopadd` for more info - type `.h .shopadd` for more info
- Added `.stickyroles` Users leaving the server will have their roles saved to the database and reapplied if they rejoin within 30 days. - Added `.stickyroles` Users leaving the server will have their roles saved to the database and reapplied if they rejoin within 30 days.
- Giveaway commands - Giveaway commands
- `.ga start` starts the giveway with the specified duration and message (prize). You may have up to 5 giveaways on the server at once - `.ga start <duration> <text>` starts the giveway with the specified duration and message (prize). You may have up to 5 giveaways on the server at once
- `.ga end <id>` prematurely ends the giveaway and selects a winner - `.ga end <id>` prematurely ends the giveaway and selects a winner
- `.ga cancel <id>` cancels the giveaway and doesn't select the winner - `.ga cancel <id>` cancels the giveaway and doesn't select the winner
- `.ga list` lists active giveaways on the current server - `.ga list` lists active giveaways on the current server
@ -37,6 +37,8 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- You can now configure shop sale cut in `gambling.yml` - You can now configure shop sale cut in `gambling.yml`
- Added a page parameter to `.feedlist` - Added a page parameter to `.feedlist`
- Added seconds/sec/s to .convert command - Added seconds/sec/s to .convert command
- Added `.prunecancel` to cancel an active prune
- Added progress reporting when using `.prune`. The bot will periodically update on how many messages have been deleted
### Changed ### Changed
@ -46,6 +48,7 @@ Experimental changelog. Mostly based on [keepachangelog](https://keepachangelog.
- You can now target a different channel with .repeat, for example `.repeat #some-other 1h Hello every hour` - You can now target a different channel with .repeat, for example `.repeat #some-other 1h Hello every hour`
- `.cmds <module name>`, `.cmds <group name` and `.mdls` looks better / cleaner / simpler - `.cmds <module name>`, `.cmds <group name` and `.mdls` looks better / cleaner / simpler
- The bot will now send a discord Reply to every command - The bot will now send a discord Reply to every command
- `.queuesearch` / `.qs` will now show the results with respective video thumbnails
- A lot of code cleanup (still a lot to be done) and Quality of Life improvements - A lot of code cleanup (still a lot to be done) and Quality of Life improvements
### Fixed ### Fixed

View file

@ -1,12 +1,10 @@
#nullable disable #nullable disable
using DryIoc;
using Microsoft.Extensions.Caching.Memory; using Microsoft.Extensions.Caching.Memory;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Db;
using NadekoBot.Common.Configs; using NadekoBot.Common.Configs;
using NadekoBot.Common.ModuleBehaviors; using NadekoBot.Common.ModuleBehaviors;
using NadekoBot.Db.Models; using NadekoBot.Db.Models;
using Ninject;
using Ninject.Planning;
using System.Collections.Immutable; using System.Collections.Immutable;
using System.Diagnostics; using System.Diagnostics;
using System.Reflection; using System.Reflection;
@ -21,7 +19,7 @@ public sealed class Bot : IBot
public DiscordSocketClient Client { get; } public DiscordSocketClient Client { get; }
public IReadOnlyCollection<GuildConfig> AllGuildConfigs { get; private set; } public IReadOnlyCollection<GuildConfig> AllGuildConfigs { get; private set; }
private IKernel Services { get; set; } private IContainer Services { get; set; }
public bool IsReady { get; private set; } public bool IsReady { get; private set; }
public int ShardId { get; set; } public int ShardId { get; set; }
@ -37,8 +35,7 @@ public sealed class Bot : IBot
public Bot(int shardId, int? totalShards, string credPath = null) public Bot(int shardId, int? totalShards, string credPath = null)
{ {
if (shardId < 0) ArgumentOutOfRangeException.ThrowIfLessThan(shardId, 0);
throw new ArgumentOutOfRangeException(nameof(shardId));
ShardId = shardId; ShardId = shardId;
_credsProvider = new BotCredsProvider(totalShards, credPath); _credsProvider = new BotCredsProvider(totalShards, credPath);
@ -105,15 +102,17 @@ public sealed class Bot : IBot
AllGuildConfigs = uow.Set<GuildConfig>().GetAllGuildConfigs(startingGuildIdList).ToImmutableArray(); AllGuildConfigs = uow.Set<GuildConfig>().GetAllGuildConfigs(startingGuildIdList).ToImmutableArray();
} }
var svcs = new StandardKernel(new NinjectSettings() // var svcs = new StandardKernel(new NinjectSettings()
{ // {
// ThrowOnGetServiceNotFound = true, // // ThrowOnGetServiceNotFound = true,
ActivationCacheDisabled = true, // ActivationCacheDisabled = true,
}); // });
var svcs = new Container();
// this is required in order for medusa unloading to work // this is required in order for medusa unloading to work
svcs.Components.Remove<IPlanner, Planner>(); // svcs.Components.Remove<IPlanner, Planner>();
svcs.Components.Add<IPlanner, RemovablePlanner>(); // svcs.Components.Add<IPlanner, RemovablePlanner>();
svcs.AddSingleton<IBotCredentials, IBotCredentials>(_ => _credsProvider.GetCreds()); svcs.AddSingleton<IBotCredentials, IBotCredentials>(_ => _credsProvider.GetCreds());
svcs.AddSingleton<DbService, DbService>(_db); svcs.AddSingleton<DbService, DbService>(_db);

View file

@ -13,17 +13,25 @@ public partial class Administration
public sealed class PruneOptions : INadekoCommandOptions public sealed class PruneOptions : INadekoCommandOptions
{ {
[Option(shortName: 's', longName: "safe", Default = false, HelpText = "Whether pinned messages should be deleted.", Required = false)] [Option(shortName: 's',
longName: "safe",
Default = false,
HelpText = "Whether pinned messages should be deleted.",
Required = false)]
public bool Safe { get; set; } public bool Safe { get; set; }
[Option(shortName: 'a', longName: "after", Default = null, HelpText = "Prune only messages after the specified message ID.", Required = false)] [Option(shortName: 'a',
longName: "after",
Default = null,
HelpText = "Prune only messages after the specified message ID.",
Required = false)]
public ulong? After { get; set; } public ulong? After { get; set; }
public void NormalizeOptions() public void NormalizeOptions()
{ {
} }
} }
//deletes her own messages, no perm required //deletes her own messages, no perm required
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
@ -31,15 +39,27 @@ public partial class Administration
public async Task Prune(params string[] args) public async Task Prune(params string[] args)
{ {
var (opts, _) = OptionsParser.ParseFrom(new PruneOptions(), args); var (opts, _) = OptionsParser.ParseFrom(new PruneOptions(), args);
var user = await ctx.Guild.GetCurrentUserAsync(); var user = await ctx.Guild.GetCurrentUserAsync();
var progressMsg = await Response().Pending(strs.prune_progress(0, 100)).SendAsync();
var progress = GetProgressTracker(progressMsg);
if (opts.Safe) if (opts.Safe)
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id && !x.IsPinned, opts.After); await _service.PruneWhere((ITextChannel)ctx.Channel,
100,
x => x.Author.Id == user.Id && !x.IsPinned,
progress,
opts.After);
else else
await _service.PruneWhere((ITextChannel)ctx.Channel, 100, x => x.Author.Id == user.Id, opts.After); await _service.PruneWhere((ITextChannel)ctx.Channel,
100,
x => x.Author.Id == user.Id,
progress,
opts.After);
ctx.Message.DeleteAfter(3); ctx.Message.DeleteAfter(3);
await progressMsg.DeleteAsync();
} }
// prune x // prune x
@ -54,15 +74,52 @@ public partial class Administration
count++; count++;
if (count < 1) if (count < 1)
return; return;
if (count > 1000) if (count > 1000)
count = 1000; count = 1000;
var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args); var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args);
var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync();
var progress = GetProgressTracker(progressMsg);
if (opts.Safe) if (opts.Safe)
await _service.PruneWhere((ITextChannel)ctx.Channel, count, x => !x.IsPinned, opts.After); await _service.PruneWhere((ITextChannel)ctx.Channel,
count,
x => !x.IsPinned && x.Id != progressMsg.Id,
progress,
opts.After);
else else
await _service.PruneWhere((ITextChannel)ctx.Channel, count, _ => true, opts.After); await _service.PruneWhere((ITextChannel)ctx.Channel,
count,
x => x.Id != progressMsg.Id,
progress,
opts.After);
await progressMsg.DeleteAsync();
}
private IProgress<(int, int)> GetProgressTracker(IUserMessage progressMsg)
{
var progress = new Progress<(int, int)>(async (x) =>
{
var (deleted, total) = x;
try
{
await progressMsg.ModifyAsync(props =>
{
props.Embed = _sender.CreateEmbed()
.WithPendingColor()
.WithDescription(GetText(strs.prune_progress(deleted, total)))
.Build();
});
}
catch
{
}
});
return progress;
} }
//prune @user [x] //prune @user [x]
@ -94,21 +151,48 @@ public partial class Administration
count = 1000; count = 1000;
var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args); var (opts, _) = OptionsParser.ParseFrom<PruneOptions>(new PruneOptions(), args);
var progressMsg = await Response().Pending(strs.prune_progress(0, count)).SendAsync();
var progress = GetProgressTracker(progressMsg);
if (opts.Safe) if (opts.Safe)
{ {
await _service.PruneWhere((ITextChannel)ctx.Channel, await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned, m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks && !m.IsPinned,
opts.After); progress,
opts.After
);
} }
else else
{ {
await _service.PruneWhere((ITextChannel)ctx.Channel, await _service.PruneWhere((ITextChannel)ctx.Channel,
count, count,
m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks, m => m.Author.Id == userId && DateTime.UtcNow - m.CreatedAt < _twoWeeks,
opts.After); progress,
opts.After
);
} }
await progressMsg.DeleteAsync();
}
[Cmd]
[RequireContext(ContextType.Guild)]
[UserPerm(ChannelPerm.ManageMessages)]
[BotPerm(ChannelPerm.ManageMessages)]
public async Task PruneCancel()
{
var ok = await _service.CancelAsync(ctx.Guild.Id);
if (!ok)
{
await Response().Error(strs.prune_not_found).SendAsync();
return;
}
await Response().Confirm(strs.prune_cancelled).SendAsync();
} }
} }
} }

View file

@ -4,21 +4,29 @@ namespace NadekoBot.Modules.Administration.Services;
public class PruneService : INService public class PruneService : INService
{ {
//channelids where prunes are currently occuring //channelids where prunes are currently occuring
private readonly ConcurrentHashSet<ulong> _pruningGuilds = new(); private readonly ConcurrentDictionary<ulong, CancellationTokenSource> _pruningGuilds = new();
private readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14); private readonly TimeSpan _twoWeeks = TimeSpan.FromDays(14);
private readonly ILogCommandService _logService; private readonly ILogCommandService _logService;
public PruneService(ILogCommandService logService) public PruneService(ILogCommandService logService)
=> _logService = logService; => _logService = logService;
public async Task PruneWhere(ITextChannel channel, int amount, Func<IMessage, bool> predicate, ulong? after = null) public async Task PruneWhere(
ITextChannel channel,
int amount,
Func<IMessage, bool> predicate,
IProgress<(int deleted, int total)> progress,
ulong? after = null
)
{ {
ArgumentNullException.ThrowIfNull(channel, nameof(channel)); ArgumentNullException.ThrowIfNull(channel, nameof(channel));
var originalAmount = amount;
if (amount <= 0) if (amount <= 0)
throw new ArgumentOutOfRangeException(nameof(amount)); throw new ArgumentOutOfRangeException(nameof(amount));
if (!_pruningGuilds.Add(channel.GuildId)) using var cancelSource = new CancellationTokenSource();
if (!_pruningGuilds.TryAdd(channel.GuildId, cancelSource))
return; return;
try try
@ -26,16 +34,22 @@ public class PruneService : INService
var now = DateTime.UtcNow; var now = DateTime.UtcNow;
IMessage[] msgs; IMessage[] msgs;
IMessage lastMessage = null; IMessage lastMessage = null;
var dled = await channel.GetMessagesAsync(50).FlattenAsync();
while (amount > 0 && !cancelSource.IsCancellationRequested)
msgs = dled
.Where(predicate)
.Where(x => after is ulong a ? x.Id > a : true)
.Take(amount)
.ToArray();
while (amount > 0 && msgs.Any())
{ {
var dled = lastMessage is null
? await channel.GetMessagesAsync(50).FlattenAsync()
: await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync();
msgs = dled
.Where(predicate)
.Where(x => after is not ulong a || x.Id > a)
.Take(amount)
.ToArray();
if (!msgs.Any())
return;
lastMessage = msgs[^1]; lastMessage = msgs[^1];
var bulkDeletable = new List<IMessage>(); var bulkDeletable = new List<IMessage>();
@ -53,27 +67,17 @@ public class PruneService : INService
if (bulkDeletable.Count > 0) if (bulkDeletable.Count > 0)
{ {
await channel.DeleteMessagesAsync(bulkDeletable); await channel.DeleteMessagesAsync(bulkDeletable);
await Task.Delay(2000); amount -= msgs.Length;
progress.Report((originalAmount - amount, originalAmount));
await Task.Delay(2000, cancelSource.Token);
} }
foreach (var group in singleDeletable.Chunk(5)) foreach (var group in singleDeletable.Chunk(5))
{ {
await group.Select(x => x.DeleteAsync()).WhenAll(); await group.Select(x => x.DeleteAsync()).WhenAll();
await Task.Delay(5000); amount -= 5;
} progress.Report((originalAmount - amount, originalAmount));
await Task.Delay(5000, cancelSource.Token);
//this isn't good, because this still work as if i want to remove only specific user's messages from the last
//100 messages, Maybe this needs to be reduced by msgs.Length instead of 100
amount -= 50;
if (amount > 0)
{
dled = await channel.GetMessagesAsync(lastMessage, Direction.Before, 50).FlattenAsync();
msgs = dled
.Where(predicate)
.Where(x => after is ulong a ? x.Id > a : true)
.Take(amount)
.ToArray();
} }
} }
} }
@ -83,7 +87,16 @@ public class PruneService : INService
} }
finally finally
{ {
_pruningGuilds.TryRemove(channel.GuildId); _pruningGuilds.TryRemove(channel.GuildId, out _);
} }
} }
public async Task<bool> CancelAsync(ulong guildId)
{
if (!_pruningGuilds.TryRemove(guildId, out var source))
return false;
await source.CancelAsync();
return true;
}
} }

View file

@ -110,10 +110,10 @@ public sealed partial class Music : NadekoModule<IMusicService>
try try
{ {
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.WithAuthor(GetText(strs.queued_track) + " #" + (index + 1), MUSIC_ICON_URL) .WithAuthor(GetText(strs.queued_track) + " #" + (index + 1), MUSIC_ICON_URL)
.WithDescription($"{trackInfo.PrettyName()}\n{GetText(strs.queue)} ") .WithDescription($"{trackInfo.PrettyName()}\n{GetText(strs.queue)} ")
.WithFooter(trackInfo.Platform.ToString()); .WithFooter(trackInfo.Platform.ToString());
if (!string.IsNullOrWhiteSpace(trackInfo.Thumbnail)) if (!string.IsNullOrWhiteSpace(trackInfo.Thumbnail))
embed.WithThumbnailUrl(trackInfo.Thumbnail); embed.WithThumbnailUrl(trackInfo.Thumbnail);
@ -315,11 +315,13 @@ public sealed partial class Music : NadekoModule<IMusicService>
desc = add + "\n" + desc; desc = add + "\n" + desc;
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithAuthor(GetText(strs.player_queue(curPage + 1, (tracks.Count / LQ_ITEMS_PER_PAGE) + 1)), .WithAuthor(
MUSIC_ICON_URL) GetText(strs.player_queue(curPage + 1, (tracks.Count / LQ_ITEMS_PER_PAGE) + 1)),
.WithDescription(desc) MUSIC_ICON_URL)
.WithFooter($" {mp.PrettyVolume()} | 🎶 {tracks.Count} | ⌛ {mp.PrettyTotalTime()} ") .WithDescription(desc)
.WithOkColor(); .WithFooter(
$" {mp.PrettyVolume()} | 🎶 {tracks.Count} | ⌛ {mp.PrettyTotalTime()} ")
.WithOkColor();
return embed; return embed;
} }
@ -349,13 +351,21 @@ public sealed partial class Music : NadekoModule<IMusicService>
return; return;
} }
var resultsString = videos.Select((x, i) => $"`{i + 1}.`\n\t{Format.Bold(x.Title)}\n\t{x.Url}").Join('\n');
var msg = await Response().Confirm(resultsString).SendAsync(); var embeds = videos.Select((x, i) => _sender.CreateEmbed()
.WithOkColor()
.WithThumbnailUrl(x.Thumbnail)
.WithDescription($"`{i + 1}.` {Format.Bold(x.Title)}\n\t{x.Url}"))
.ToList();
var msg = await Response()
.Text(strs.queue_search_results)
.Embeds(embeds)
.SendAsync();
try try
{ {
var input = await GetUserInputAsync(ctx.User.Id, ctx.Channel.Id); var input = await GetUserInputAsync(ctx.User.Id, ctx.Channel.Id, str => int.TryParse(str, out _));
if (input is null || !int.TryParse(input, out var index) || (index -= 1) < 0 || index >= videos.Count) if (input is null || !int.TryParse(input, out var index) || (index -= 1) < 0 || index >= videos.Count)
{ {
_logService.AddDeleteIgnore(msg.Id); _logService.AddDeleteIgnore(msg.Id);
@ -415,10 +425,10 @@ public sealed partial class Music : NadekoModule<IMusicService>
} }
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithAuthor(GetText(strs.removed_track) + " #" + index, MUSIC_ICON_URL) .WithAuthor(GetText(strs.removed_track) + " #" + index, MUSIC_ICON_URL)
.WithDescription(track.PrettyName()) .WithDescription(track.PrettyName())
.WithFooter(track.PrettyInfo()) .WithFooter(track.PrettyInfo())
.WithErrorColor(); .WithErrorColor();
await _service.SendToOutputAsync(ctx.Guild.Id, embed); await _service.SendToOutputAsync(ctx.Guild.Id, embed);
} }
@ -583,11 +593,11 @@ public sealed partial class Music : NadekoModule<IMusicService>
} }
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithTitle(track.Title.TrimTo(65)) .WithTitle(track.Title.TrimTo(65))
.WithAuthor(GetText(strs.track_moved), MUSIC_ICON_URL) .WithAuthor(GetText(strs.track_moved), MUSIC_ICON_URL)
.AddField(GetText(strs.from_position), $"#{from + 1}", true) .AddField(GetText(strs.from_position), $"#{from + 1}", true)
.AddField(GetText(strs.to_position), $"#{to + 1}", true) .AddField(GetText(strs.to_position), $"#{to + 1}", true)
.WithOkColor(); .WithOkColor();
if (Uri.IsWellFormedUriString(track.Url, UriKind.Absolute)) if (Uri.IsWellFormedUriString(track.Url, UriKind.Absolute))
embed.WithUrl(track.Url); embed.WithUrl(track.Url);
@ -642,12 +652,12 @@ public sealed partial class Music : NadekoModule<IMusicService>
return; return;
var embed = _sender.CreateEmbed() var embed = _sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.WithAuthor(GetText(strs.now_playing), MUSIC_ICON_URL) .WithAuthor(GetText(strs.now_playing), MUSIC_ICON_URL)
.WithDescription(currentTrack.PrettyName()) .WithDescription(currentTrack.PrettyName())
.WithThumbnailUrl(currentTrack.Thumbnail) .WithThumbnailUrl(currentTrack.Thumbnail)
.WithFooter( .WithFooter(
$"{mp.PrettyVolume()} | {mp.PrettyTotalTime()} | {currentTrack.Platform} | {currentTrack.Queuer}"); $"{mp.PrettyVolume()} | {mp.PrettyTotalTime()} | {currentTrack.Platform} | {currentTrack.Queuer}");
await Response().Embed(embed).SendAsync(); await Response().Embed(embed).SendAsync();
} }

View file

@ -3,7 +3,7 @@ using System.Diagnostics.CodeAnalysis;
namespace NadekoBot.Modules.Music.Services; namespace NadekoBot.Modules.Music.Services;
public interface IMusicService : IPlaceholderProvider public interface IMusicService
{ {
/// <summary> /// <summary>
/// Leave voice channel in the specified guild if it's connected to one /// Leave voice channel in the specified guild if it's connected to one
@ -24,7 +24,7 @@ public interface IMusicService : IPlaceholderProvider
Task EnqueueDirectoryAsync(IMusicPlayer mp, string dirPath, string queuer); Task EnqueueDirectoryAsync(IMusicPlayer mp, string dirPath, string queuer);
Task<IUserMessage?> SendToOutputAsync(ulong guildId, EmbedBuilder embed); Task<IUserMessage?> SendToOutputAsync(ulong guildId, EmbedBuilder embed);
Task<bool> PlayAsync(ulong guildId, ulong voiceChannelId); Task<bool> PlayAsync(ulong guildId, ulong voiceChannelId);
Task<IList<(string Title, string Url)>> SearchVideosAsync(string query); Task<IList<(string Title, string Url, string Thumbnail)>> SearchVideosAsync(string query);
Task<bool> SetMusicChannelAsync(ulong guildId, ulong? channelId); Task<bool> SetMusicChannelAsync(ulong guildId, ulong? channelId);
Task SetRepeatAsync(ulong guildId, PlayerRepeatType repeatType); Task SetRepeatAsync(ulong guildId, PlayerRepeatType repeatType);
Task SetVolumeAsync(ulong guildId, int value); Task SetVolumeAsync(ulong guildId, int value);

View file

@ -4,7 +4,7 @@ using System.Diagnostics.CodeAnalysis;
namespace NadekoBot.Modules.Music.Services; namespace NadekoBot.Modules.Music.Services;
public sealed class MusicService : IMusicService public sealed class MusicService : IMusicService, IPlaceholderProvider
{ {
private readonly AyuVoiceStateService _voiceStateService; private readonly AyuVoiceStateService _voiceStateService;
private readonly ITrackResolveProvider _trackResolveProvider; private readonly ITrackResolveProvider _trackResolveProvider;
@ -233,23 +233,23 @@ public sealed class MusicService : IMusicService
return true; return true;
} }
private async Task<IList<(string Title, string Url)>> SearchYtLoaderVideosAsync(string query) private async Task<IList<(string Title, string Url, string Thumb)>> SearchYtLoaderVideosAsync(string query)
{ {
var result = await _ytLoader.LoadResultsAsync(query); var result = await _ytLoader.LoadResultsAsync(query);
return result.Select(x => (x.Title, x.Url)).ToList(); return result.Select(x => (x.Title, x.Url, x.Thumb)).ToList();
} }
private async Task<IList<(string Title, string Url)>> SearchGoogleApiVideosAsync(string query) private async Task<IList<(string Title, string Url, string Thumb)>> SearchGoogleApiVideosAsync(string query)
{ {
var result = await _googleApiService.GetVideoInfosByKeywordAsync(query, 5); var result = await _googleApiService.GetVideoInfosByKeywordAsync(query, 5);
return result.Select(x => (x.Name, x.Url)).ToList(); return result.Select(x => (x.Name, x.Url, x.Thumbnail)).ToList();
} }
public async Task<IList<(string Title, string Url)>> SearchVideosAsync(string query) public async Task<IList<(string Title, string Url, string Thumbnail)>> SearchVideosAsync(string query)
{ {
try try
{ {
IList<(string, string)> videos = await SearchYtLoaderVideosAsync(query); IList<(string, string, string)> videos = await SearchYtLoaderVideosAsync(query);
if (videos.Count > 0) if (videos.Count > 0)
return videos; return videos;
} }
@ -269,7 +269,7 @@ public sealed class MusicService : IMusicService
ex.Message); ex.Message);
} }
return Array.Empty<(string, string)>(); return Array.Empty<(string, string, string)>();
} }
private string GetText(ulong guildId, LocStr str) private string GetText(ulong guildId, LocStr str)
@ -434,4 +434,4 @@ public sealed class MusicService : IMusicService
} }
#endregion #endregion
} }

View file

@ -47,6 +47,7 @@ public sealed partial class YtLoader
{ {
public abstract string Url { get; } public abstract string Url { get; }
public abstract string Title { get; } public abstract string Title { get; }
public abstract string Thumb { get; }
public abstract TimeSpan Duration { get; } public abstract TimeSpan Duration { get; }
} }
@ -55,13 +56,15 @@ public sealed partial class YtLoader
private const string BASE_YOUTUBE_URL = "https://youtube.com/watch?v="; private const string BASE_YOUTUBE_URL = "https://youtube.com/watch?v=";
public override string Url { get; } public override string Url { get; }
public override string Title { get; } public override string Title { get; }
public override string Thumb { get; }
public override TimeSpan Duration { get; } public override TimeSpan Duration { get; }
private readonly string _videoId; private readonly string _videoId;
public YtTrackInfo(string title, string videoId, TimeSpan duration) public YtTrackInfo(string title, string videoId, string thumb, TimeSpan duration)
{ {
Title = title; Title = title;
Thumb = thumb;
Url = BASE_YOUTUBE_URL + videoId; Url = BASE_YOUTUBE_URL + videoId;
Duration = duration; Duration = duration;

View file

@ -5,7 +5,7 @@ using System.Text.Json;
namespace NadekoBot.Modules.Music.Services; namespace NadekoBot.Modules.Music.Services;
public sealed partial class YtLoader public sealed partial class YtLoader : INService
{ {
private static readonly byte[] _ytResultInitialData = Encoding.UTF8.GetBytes("var ytInitialData = "); private static readonly byte[] _ytResultInitialData = Encoding.UTF8.GetBytes("var ytInitialData = ");
private static readonly byte[] _ytResultJsonEnd = Encoding.UTF8.GetBytes(";<"); private static readonly byte[] _ytResultJsonEnd = Encoding.UTF8.GetBytes(";<");
@ -93,7 +93,7 @@ public sealed partial class YtLoader
continue; continue;
var videoId = elem.GetProperty("videoId").GetString(); var videoId = elem.GetProperty("videoId").GetString();
// var thumb = elem.GetProperty("thumbnail").GetProperty("thumbnails")[0].GetProperty("url").GetString(); var thumb = elem.GetProperty("thumbnail").GetProperty("thumbnails")[0].GetProperty("url").GetString();
var title = elem.GetProperty("title").GetProperty("runs")[0].GetProperty("text").GetString(); var title = elem.GetProperty("title").GetProperty("runs")[0].GetProperty("text").GetString();
var durationString = elem.GetProperty("lengthText").GetProperty("simpleText").GetString(); var durationString = elem.GetProperty("lengthText").GetProperty("simpleText").GetString();
@ -106,7 +106,7 @@ public sealed partial class YtLoader
continue; continue;
} }
tracks.Add(new YtTrackInfo(title, videoId, duration)); tracks.Add(new YtTrackInfo(title, videoId, thumb, duration));
if (tracks.Count >= 5) if (tracks.Count >= 5)
break; break;
} }

View file

@ -4,7 +4,7 @@ using NadekoBot.Db.Models;
namespace NadekoBot.Modules.Utility; namespace NadekoBot.Modules.Utility;
public sealed class TodoService public sealed class TodoService : INService
{ {
private const int ARCHIVE_MAX_COUNT = 9; private const int ARCHIVE_MAX_COUNT = 9;
private const int TODO_MAX_COUNT = 27; private const int TODO_MAX_COUNT = 27;

View file

@ -166,9 +166,10 @@ public partial class Utility : NadekoModule
return _sender.CreateEmbed().WithOkColor().WithDescription(GetText(strs.no_user_on_this_page)); return _sender.CreateEmbed().WithOkColor().WithDescription(GetText(strs.no_user_on_this_page));
return _sender.CreateEmbed() return _sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.WithTitle(GetText(strs.inrole_list(Format.Bold(role?.Name ?? "No Role"), roleUsers.Length))) .WithTitle(GetText(strs.inrole_list(Format.Bold(role?.Name ?? "No Role"),
.WithDescription(string.Join("\n", pageUsers)); roleUsers.Length)))
.WithDescription(string.Join("\n", pageUsers));
}) })
.SendAsync(); .SendAsync();
} }
@ -183,9 +184,14 @@ public partial class Utility : NadekoModule
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task CheckPerms(MeOrBot who = MeOrBot.Me) 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 user = who == MeOrBot.Me ? (IGuildUser)ctx.User : ((SocketGuild)ctx.Guild).CurrentUser;
var perms = user.GetPermissions((ITextChannel)ctx.Channel); var perms = user.GetPermissions((ITextChannel)ctx.Channel);
await SendPerms(perms);
}
private async Task SendPerms(ChannelPermissions perms)
{
var builder = new StringBuilder();
foreach (var p in perms.GetType() foreach (var p in perms.GetType()
.GetProperties() .GetProperties()
.Where(static p => .Where(static p =>
@ -199,6 +205,16 @@ public partial class Utility : NadekoModule
await Response().Confirm(builder.ToString()).SendAsync(); await Response().Confirm(builder.ToString()).SendAsync();
} }
// [Cmd]
// [RequireContext(ContextType.Guild)]
// [RequireUserPermission(GuildPermission.ManageRoles)]
// public async Task CheckPerms(SocketRole role, string perm = null)
// {
// ChannelPermissions.
// var perms = ((ITextChannel)ctx.Channel);
// await SendPerms(perms)
// }
[Cmd] [Cmd]
[RequireContext(ContextType.Guild)] [RequireContext(ContextType.Guild)]
public async Task UserId([Leftover] IGuildUser target = null) public async Task UserId([Leftover] IGuildUser target = null)
@ -305,29 +321,29 @@ public partial class Utility : NadekoModule
await Response() await Response()
.Embed(_sender.CreateEmbed() .Embed(_sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.WithAuthor($"NadekoBot v{StatsService.BotVersion}", .WithAuthor($"NadekoBot v{StatsService.BotVersion}",
"https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/avatar.png", "https://nadeko-pictures.nyc3.digitaloceanspaces.com/other/avatar.png",
"https://nadekobot.readthedocs.io/en/latest/") "https://nadekobot.readthedocs.io/en/latest/")
.AddField(GetText(strs.author), _stats.Author, true) .AddField(GetText(strs.author), _stats.Author, true)
.AddField(GetText(strs.botid), _client.CurrentUser.Id.ToString(), true) .AddField(GetText(strs.botid), _client.CurrentUser.Id.ToString(), true)
.AddField(GetText(strs.shard), .AddField(GetText(strs.shard),
$"#{_client.ShardId} / {_creds.TotalShards}", $"#{_client.ShardId} / {_creds.TotalShards}",
true) true)
.AddField(GetText(strs.commands_ran), _stats.CommandsRan.ToString(), true) .AddField(GetText(strs.commands_ran), _stats.CommandsRan.ToString(), true)
.AddField(GetText(strs.messages), .AddField(GetText(strs.messages),
$"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)", $"{_stats.MessageCounter} ({_stats.MessagesPerSecond:F2}/sec)",
true) true)
.AddField(GetText(strs.memory), .AddField(GetText(strs.memory),
FormattableString.Invariant($"{_stats.GetPrivateMemoryMegabytes():F2} MB"), FormattableString.Invariant($"{_stats.GetPrivateMemoryMegabytes():F2} MB"),
true) true)
.AddField(GetText(strs.owner_ids), ownerIds, true) .AddField(GetText(strs.owner_ids), ownerIds, true)
.AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true) .AddField(GetText(strs.uptime), _stats.GetUptimeString("\n"), true)
.AddField(GetText(strs.presence), .AddField(GetText(strs.presence),
GetText(strs.presence_txt(_coord.GetGuildCount(), GetText(strs.presence_txt(_coord.GetGuildCount(),
_stats.TextChannels, _stats.TextChannels,
_stats.VoiceChannels)), _stats.VoiceChannels)),
true)) true))
.SendAsync(); .SendAsync();
} }
@ -695,9 +711,9 @@ public partial class Utility : NadekoModule
if (!string.IsNullOrWhiteSpace(output)) if (!string.IsNullOrWhiteSpace(output))
{ {
var eb = _sender.CreateEmbed() var eb = _sender.CreateEmbed()
.WithOkColor() .WithOkColor()
.AddField("Code", scriptText) .AddField("Code", scriptText)
.AddField("Output", output.TrimTo(512)!); .AddField("Output", output.TrimTo(512)!);
_ = Response().Embed(eb).SendAsync(); _ = Response().Embed(eb).SendAsync();
} }

View file

@ -54,10 +54,11 @@
<PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0"/> <PackageReference Include="NetEscapades.Configuration.Yaml" Version="3.1.0"/>
<!-- DI --> <!-- DI -->
<PackageReference Include="Ninject" Version="3.3.6"/> <!-- <PackageReference Include="Ninject" Version="3.3.6"/>-->
<PackageReference Include="Ninject.Extensions.Conventions" Version="3.3.0"/> <!-- <PackageReference Include="Ninject.Extensions.Conventions" Version="3.3.0"/>-->
<!-- <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />--> <!-- <PackageReference Include="Microsoft.Extensions.DependencyInjection" Version="8.0.0" />-->
<PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1"/> <PackageReference Include="Microsoft.Extensions.DependencyInjection.Abstractions" Version="8.0.1"/>
<PackageReference Include="DryIoc.dll" Version="5.4.3" />
<!-- <PackageReference Include="Scrutor" Version="4.2.0" />--> <!-- <PackageReference Include="Scrutor" Version="4.2.0" />-->
<PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0"/> <PackageReference Include="Microsoft.Extensions.Http" Version="8.0.0"/>

View file

@ -93,7 +93,7 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService
return (await query.ExecuteAsync()).Items.Select(i => "https://www.youtube.com/watch?v=" + i.Id.VideoId); return (await query.ExecuteAsync()).Items.Select(i => "https://www.youtube.com/watch?v=" + i.Id.VideoId);
} }
public async Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync( public async Task<IEnumerable<(string Name, string Id, string Url, string Thumbnail)>> GetVideoInfosByKeywordAsync(
string keywords, string keywords,
int count = 1) int count = 1)
{ {
@ -108,7 +108,10 @@ public sealed partial class GoogleApiService : IGoogleApiService, INService
query.Q = keywords; query.Q = keywords;
query.Type = "video"; query.Type = "video";
return (await query.ExecuteAsync()).Items.Select(i return (await query.ExecuteAsync()).Items.Select(i
=> (i.Snippet.Title.TrimTo(50), i.Id.VideoId, "https://www.youtube.com/watch?v=" + i.Id.VideoId)); => (i.Snippet.Title.TrimTo(50),
i.Id.VideoId,
"https://www.youtube.com/watch?v=" + i.Id.VideoId,
i.Snippet.Thumbnails.High.Url));
} }
public Task<string> ShortenUrl(Uri url) public Task<string> ShortenUrl(Uri url)

View file

@ -32,7 +32,7 @@ public static class StringExtensions
{ {
if (hideDots) if (hideDots)
{ {
return str?.Substring(0, maxLength); return str?.Substring(0, Math.Min(str?.Length ?? 0, maxLength));
} }
if (str is null || str.Length <= maxLength) if (str is null || str.Length <= maxLength)

View file

@ -1,4 +1,5 @@
using Discord.Commands.Builders; using Discord.Commands.Builders;
using DryIoc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using Nadeko.Common.Medusa; using Nadeko.Common.Medusa;
using Nadeko.Medusa.Adapters; using Nadeko.Medusa.Adapters;
@ -20,7 +21,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
private readonly IBehaviorHandler _behHandler; private readonly IBehaviorHandler _behHandler;
private readonly IPubSub _pubSub; private readonly IPubSub _pubSub;
private readonly IMedusaConfigService _medusaConfig; private readonly IMedusaConfigService _medusaConfig;
private readonly IKernel _kernel; private readonly IContainer _kernel;
private readonly ConcurrentDictionary<string, ResolvedMedusa> _resolved = new(); private readonly ConcurrentDictionary<string, ResolvedMedusa> _resolved = new();
private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1); private readonly SemaphoreSlim _lock = new SemaphoreSlim(1, 1);
@ -34,7 +35,7 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
public MedusaLoaderService( public MedusaLoaderService(
CommandService cmdService, CommandService cmdService,
IKernel kernel, IContainer kernel,
IBehaviorHandler behHandler, IBehaviorHandler behHandler,
IPubSub pubSub, IPubSub pubSub,
IMedusaConfigService medusaConfig) IMedusaConfigService medusaConfig)
@ -337,14 +338,17 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
// load services // load services
ninjectModule = new MedusaNinjectModule(a, safeName); ninjectModule = new MedusaNinjectModule(a, safeName);
_kernel.Load(ninjectModule);
// todo medusa won't work, uncomment
// _kernel.Load(ninjectModule);
var sis = LoadSneksFromAssembly(safeName, a); var sis = LoadSneksFromAssembly(safeName, a);
typeReaders = LoadTypeReadersFromAssembly(a, strings); typeReaders = LoadTypeReadersFromAssembly(a, strings);
if (sis.Count == 0) if (sis.Count == 0)
{ {
_kernel.Unload(safeName); // todo uncomment
// _kernel.Unload(safeName);
return false; return false;
} }
@ -604,7 +608,8 @@ public sealed class MedusaLoaderService : IMedusaLoaderService, IReadyExecutor,
var km = lsi.KernelModule; var km = lsi.KernelModule;
lsi.KernelModule = null!; lsi.KernelModule = null!;
_kernel.Unload(km.Name); // todo uncomment
// _kernel.Unload(km.Name);
if (km is IDisposable d) if (km is IDisposable d)
d.Dispose(); d.Dispose();

View file

@ -61,7 +61,7 @@ public abstract class NadekoModule : ModuleBase
} }
// TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); ? // TypeConverter typeConverter = TypeDescriptor.GetConverter(propType); ?
public async Task<string> GetUserInputAsync(ulong userId, ulong channelId) public async Task<string> GetUserInputAsync(ulong userId, ulong channelId, Func<string, bool> validate = null)
{ {
var userInputTask = new TaskCompletionSource<string>(); var userInputTask = new TaskCompletionSource<string>();
var dsc = (DiscordSocketClient)ctx.Client; var dsc = (DiscordSocketClient)ctx.Client;
@ -89,6 +89,9 @@ public abstract class NadekoModule : ModuleBase
|| userMsg.Channel.Id != channelId) || userMsg.Channel.Id != channelId)
return Task.CompletedTask; return Task.CompletedTask;
if (validate is not null && !validate(arg.Content))
return Task.CompletedTask;
if (userInputTask.TrySetResult(arg.Content)) if (userInputTask.TrySetResult(arg.Content))
userMsg.DeleteAfter(1); userMsg.DeleteAfter(1);

View file

@ -1,43 +1,51 @@
using Ninject; using DryIoc;
namespace NadekoBot.Extensions; namespace NadekoBot.Extensions;
public static class NinjectIKernelExtensions public static class DryIocExtensions
{ {
public static IKernel AddSingleton<TImpl>(this IKernel kernel) public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container)
where TImpl : TSvc
{ {
kernel.Bind<TImpl>().ToSelf().InSingletonScope(); container.Register<TSvc, TImpl>(Reuse.Singleton);
return kernel;
return container;
}
public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container, TImpl obj)
where TImpl : TSvc
{
container.RegisterInstance<TSvc>(obj);
return container;
} }
public static IKernel AddSingleton<TInterface, TImpl>(this IKernel kernel) public static IContainer AddSingleton<TSvc, TImpl>(this IContainer container, Func<IResolverContext, TSvc> factory)
where TImpl : TInterface where TImpl : TSvc
{ {
kernel.Bind<TInterface>().To<TImpl>().InSingletonScope(); container.RegisterDelegate(factory, Reuse.Singleton);
return kernel;
return container;
} }
public static IKernel AddSingleton<TImpl>(this IKernel kernel, TImpl obj) public static IContainer AddSingleton<TImpl>(this IContainer container)
=> kernel.AddSingleton<TImpl, TImpl>(obj);
public static IKernel AddSingleton<TInterface, TImpl>(this IKernel kernel, TImpl obj)
where TImpl : TInterface
{ {
kernel.Bind<TInterface>().ToConstant(obj).InSingletonScope(); container.Register<TImpl>(Reuse.Singleton);
return kernel;
return container;
} }
public static IKernel AddSingleton<TImpl, TInterface>( public static IContainer AddSingleton<TImpl>(this IContainer container, TImpl obj)
this IKernel kernel,
Func<Ninject.Activation.IContext, TImpl> factory)
where TImpl : TInterface
{ {
kernel.Bind<TInterface>().ToMethod(factory).InSingletonScope(); container.RegisterInstance<TImpl>(obj);
return kernel;
}
public static IKernel AddSingleton<TImpl>( return container;
this IKernel kernel, }
Func<Ninject.Activation.IContext, TImpl> factory)
=> kernel.AddSingleton<TImpl, TImpl>(factory); public static IContainer AddSingleton<TImpl>(this IContainer container, Func<IResolverContext, TImpl> factory)
{
container.RegisterDelegate(factory);
return container;
}
} }

View file

@ -1,9 +1,8 @@
using DryIoc;
using Microsoft.Extensions.DependencyInjection; using Microsoft.Extensions.DependencyInjection;
using NadekoBot.Modules.Music; using NadekoBot.Modules.Music;
using NadekoBot.Modules.Music.Resolvers; using NadekoBot.Modules.Music.Resolvers;
using NadekoBot.Modules.Music.Services; using NadekoBot.Modules.Music.Services;
using Ninject;
using Ninject.Extensions.Conventions;
using Ninject.Extensions.Conventions.Syntax; using Ninject.Extensions.Conventions.Syntax;
using StackExchange.Redis; using StackExchange.Redis;
using System.Net; using System.Net;
@ -15,92 +14,103 @@ namespace NadekoBot.Extensions;
public static class ServiceCollectionExtensions public static class ServiceCollectionExtensions
{ {
public static IKernel AddBotStringsServices(this IKernel kernel, BotCacheImplemenation botCache) public static IContainer AddBotStringsServices(this IContainer svcs, BotCacheImplemenation botCache)
{ {
if (botCache == BotCacheImplemenation.Memory) if (botCache == BotCacheImplemenation.Memory)
{ {
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope(); svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
kernel.Bind<IBotStringsProvider>().To<MemoryBotStringsProvider>().InSingletonScope(); svcs.AddSingleton<IBotStringsProvider, MemoryBotStringsProvider>();
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
} }
else else
{ {
kernel.Bind<IStringsSource>().To<LocalFileStringsSource>().InSingletonScope(); svcs.AddSingleton<IStringsSource, LocalFileStringsSource>();
kernel.Bind<IBotStringsProvider>().To<RedisBotStringsProvider>().InSingletonScope(); svcs.AddSingleton<IBotStringsProvider, RedisBotStringsProvider>();
kernel.Bind<IBotStrings>().To<BotStrings>().InSingletonScope();
} }
return kernel; svcs.AddSingleton<IBotStrings, BotStrings>();
return svcs;
} }
public static IKernel AddConfigServices(this IKernel kernel, Assembly a) public static IContainer AddConfigServices(this IContainer kernel, Assembly a)
{ {
kernel.Bind(x => // kernel.RegisterMany([typeof(ConfigServiceBase<>)]);
foreach (var type in a.GetTypes()
.Where(x => !x.IsAbstract && x.IsAssignableToGenericType(typeof(ConfigServiceBase<>))))
{ {
var configs = x.From(a) kernel.RegisterMany([type],
.SelectAllClasses() getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
.Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>))); getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
}
configs.BindToSelfWithInterfaces() //
.Configure(c => c.InSingletonScope()); // kernel.Bind(x =>
}); // {
// var configs = x.From(a)
// .SelectAllClasses()
// .Where(f => f.IsAssignableToGenericType(typeof(ConfigServiceBase<>)));
//
// configs.BindToSelfWithInterfaces()
// .Configure(c => c.InSingletonScope());
// });
return kernel; return kernel;
} }
public static IKernel AddConfigMigrators(this IKernel kernel, Assembly a) public static IContainer AddConfigMigrators(this IContainer kernel, Assembly a)
=> kernel.AddSealedSubclassesOf(typeof(IConfigMigrator), a); => kernel.AddSealedSubclassesOf(typeof(IConfigMigrator), a);
public static IKernel AddMusic(this IKernel kernel) public static IContainer AddMusic(this IContainer kernel)
{ {
kernel.Bind<IMusicService, IPlaceholderProvider>() kernel.RegisterMany<MusicService>(Reuse.Singleton);
.To<MusicService>()
.InSingletonScope();
kernel.Bind<ITrackResolveProvider>().To<TrackResolveProvider>().InSingletonScope(); kernel.AddSingleton<ITrackResolveProvider, TrackResolveProvider>();
kernel.Bind<IYoutubeResolver>().To<YtdlYoutubeResolver>().InSingletonScope(); kernel.AddSingleton<IYoutubeResolver, YtdlYoutubeResolver>();
kernel.Bind<ILocalTrackResolver>().To<LocalTrackResolver>().InSingletonScope(); kernel.AddSingleton<ILocalTrackResolver, LocalTrackResolver>();
kernel.Bind<IRadioResolver>().To<RadioResolver>().InSingletonScope(); kernel.AddSingleton<IRadioResolver, RadioResolver>();
kernel.Bind<ITrackCacher>().To<TrackCacher>().InSingletonScope(); kernel.AddSingleton<ITrackCacher, TrackCacher>();
// kernel.Bind<YtLoader>().ToSelf().InSingletonScope();
return kernel; return kernel;
} }
public static IKernel AddSealedSubclassesOf(this IKernel kernel, Type baseType, Assembly a) public static IContainer AddSealedSubclassesOf(this IContainer cont, Type baseType, Assembly a)
{ {
kernel.Bind(x => var classes = a.GetExportedTypes()
.Where(x => x.IsClass && !x.IsAbstract && x.IsPublic)
.Where(x => x.IsNested && baseType.IsAssignableFrom(x));
foreach (var c in classes)
{ {
var classes = x.From(a) cont.RegisterMany([c], Reuse.Singleton);
.SelectAllClasses() // var inters = c.GetInterfaces();
.Where(c => c.IsPublic && c.IsNested && baseType.IsAssignableFrom(baseType));
classes.BindToSelfWithInterfaces().Configure(x => x.InSingletonScope()); // cont.RegisterMany(inters, c);
}); }
return kernel; return cont;
} }
public static IKernel AddCache(this IKernel kernel, IBotCredentials creds) public static IContainer AddCache(this IContainer cont, IBotCredentials creds)
{ {
if (creds.BotCache == BotCacheImplemenation.Redis) if (creds.BotCache == BotCacheImplemenation.Redis)
{ {
var conf = ConfigurationOptions.Parse(creds.RedisOptions); var conf = ConfigurationOptions.Parse(creds.RedisOptions);
kernel.Bind<ConnectionMultiplexer>().ToConstant(ConnectionMultiplexer.Connect(conf)).InSingletonScope(); cont.AddSingleton<ConnectionMultiplexer>(ConnectionMultiplexer.Connect(conf));
kernel.Bind<IBotCache>().To<RedisBotCache>().InSingletonScope(); cont.AddSingleton<IBotCache, RedisBotCache>();
kernel.Bind<IPubSub>().To<RedisPubSub>().InSingletonScope(); cont.AddSingleton<IPubSub, RedisPubSub>();
} }
else else
{ {
kernel.Bind<IBotCache>().To<MemoryBotCache>().InSingletonScope(); cont.AddSingleton<IBotCache, MemoryBotCache>();
kernel.Bind<IPubSub>().To<EventPubSub>().InSingletonScope(); cont.AddSingleton<IPubSub, EventPubSub>();
} }
return kernel return cont
.AddBotStringsServices(creds.BotCache); .AddBotStringsServices(creds.BotCache);
} }
public static IKernel AddHttpClients(this IKernel kernel) public static IContainer AddHttpClients(this IContainer kernel)
{ {
IServiceCollection svcs = new ServiceCollection(); IServiceCollection svcs = new ServiceCollection();
svcs.AddHttpClient(); svcs.AddHttpClient();
@ -117,8 +127,8 @@ public static class ServiceCollectionExtensions
}); });
var prov = svcs.BuildServiceProvider(); var prov = svcs.BuildServiceProvider();
kernel.Bind<IHttpClientFactory>().ToMethod(_ => prov.GetRequiredService<IHttpClientFactory>()); kernel.RegisterDelegate<IHttpClientFactory>(_ => prov.GetRequiredService<IHttpClientFactory>());
kernel.Bind<HttpClient>().ToMethod(_ => prov.GetRequiredService<HttpClient>()); kernel.RegisterDelegate<HttpClient>(_ => prov.GetRequiredService<HttpClient>());
return kernel; return kernel;
} }
@ -126,26 +136,39 @@ public static class ServiceCollectionExtensions
public static IConfigureSyntax BindToSelfWithInterfaces(this IJoinExcludeIncludeBindSyntax matcher) public static IConfigureSyntax BindToSelfWithInterfaces(this IJoinExcludeIncludeBindSyntax matcher)
=> matcher.BindSelection((type, types) => types.Append(type)); => matcher.BindSelection((type, types) => types.Append(type));
public static IKernel AddLifetimeServices(this IKernel kernel, Assembly a) public static IContainer AddLifetimeServices(this IContainer kernel, Assembly a)
{ {
kernel.Bind(scan => Type[] types =
[
typeof(IExecOnMessage),
typeof(IExecPreCommand),
typeof(IExecPostCommand),
typeof(IExecNoCommand),
typeof(IInputTransformer),
typeof(INService)
];
foreach (var svc in a.GetTypes()
.Where(type => type.IsClass && types.Any(t => type.IsAssignableTo(t)) && !type.HasAttribute<DIIgnoreAttribute>()))
{ {
scan.From(a) kernel.RegisterMany([svc],
.SelectAllClasses() getServiceTypes: type => type.GetImplementedTypes(ReflectionTools.AsImplementedType.SourceType),
.Where(c => (c.IsAssignableTo(typeof(INService)) getImplFactory: type => ReflectionFactory.Of(type, Reuse.Singleton));
|| c.IsAssignableTo(typeof(IExecOnMessage)) }
|| c.IsAssignableTo(typeof(IInputTransformer)) //
|| c.IsAssignableTo(typeof(IExecPreCommand)) // kernel.RegisterMany(
|| c.IsAssignableTo(typeof(IExecPostCommand)) // [a],
|| c.IsAssignableTo(typeof(IExecNoCommand))) // #if GLOBAL_NADEKO
&& !c.HasAttribute<DIIgnoreAttribute>() // && !c.HasAttribute<NoPublicBotAttribute>()
#if GLOBAL_NADEKO // #endif
&& !c.HasAttribute<NoPublicBotAttribute>() // ),
#endif // reuse:
) // Reuse.Singleton
.BindToSelfWithInterfaces() // );
.Configure(c => c.InSingletonScope());
});
// todo maybe self is missing
// todo maybe attribute doesn't work
return kernel; return kernel;
} }

View file

@ -7,7 +7,7 @@ public interface IGoogleApiService
IReadOnlyDictionary<string, string> Languages { get; } IReadOnlyDictionary<string, string> Languages { get; }
Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1); Task<IEnumerable<string>> GetVideoLinksByKeywordAsync(string keywords, int count = 1);
Task<IEnumerable<(string Name, string Id, string Url)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1); Task<IEnumerable<(string Name, string Id, string Url, string Thumbnail)>> GetVideoInfosByKeywordAsync(string keywords, int count = 1);
Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1); Task<IEnumerable<string>> GetPlaylistIdsByKeywordsAsync(string keywords, int count = 1);
Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1, string user = null); Task<IEnumerable<string>> GetRelatedVideosAsync(string id, int count = 1, string user = null);
Task<IEnumerable<string>> GetPlaylistTracksAsync(string playlistId, int count = 50); Task<IEnumerable<string>> GetPlaylistTracksAsync(string playlistId, int count = 50);

View file

@ -185,6 +185,9 @@ threaddelete:
prune: prune:
- prune - prune
- clear - clear
prunecancel:
- prunecancel
- prunec
die: die:
- die - die
setname: setname:

View file

@ -385,6 +385,10 @@ prune:
- "@Someone --safe" - "@Someone --safe"
- "@Someone X" - "@Someone X"
- "@Someone X -s" - "@Someone X -s"
prunecancel:
desc: "Cancels an active prune if there is any."
args:
- ""
die: die:
desc: "Shuts the bot down." desc: "Shuts the bot down."
args: args:

View file

@ -35,6 +35,9 @@
"banned_user": "User Banned", "banned_user": "User Banned",
"ban_prune_disabled": "Banned user's messages will no longer be deleted.", "ban_prune_disabled": "Banned user's messages will no longer be deleted.",
"ban_prune": "Bot will prune up to {0} day(s) worth of messages from banned user.", "ban_prune": "Bot will prune up to {0} day(s) worth of messages from banned user.",
"prune_cancelled": "Pruning was cancelled.",
"prune_not_found": "No active prune was found on this server.",
"prune_progress": "Pruning... {0}/{1} messages deleted.",
"timeoutdm": "You have been timed out in {0} server.\nReason: {1}", "timeoutdm": "You have been timed out in {0} server.\nReason: {1}",
"timedout_user": "User Timed Out", "timedout_user": "User Timed Out",
"remove_roles_pl": "have had their roles removed", "remove_roles_pl": "have had their roles removed",
@ -1089,5 +1092,7 @@
"todo_archive_empty": "You have no archived todos.", "todo_archive_empty": "You have no archived todos.",
"todo_archive_list": "Archived Todo Lists", "todo_archive_list": "Archived Todo Lists",
"todo_archive_not_found": "Archived todo list not found.", "todo_archive_not_found": "Archived todo list not found.",
"todo_archived_list": "Archived Todo List" "todo_archived_list": "Archived Todo List",
"search_results": "Search results",
"queue_search_results": "Type the number of the search result to queue up that track."
} }