mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-10 09:18:27 -04:00
Killed history
This commit is contained in:
18
NadekoBot.Core/Common/Attributes/Aliases.cs
Normal file
18
NadekoBot.Core/Common/Attributes/Aliases.cs
Normal file
@@ -0,0 +1,18 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services.Impl;
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class AliasesAttribute : AliasAttribute
|
||||
{
|
||||
public AliasesAttribute([CallerMemberName] string memberName = "")
|
||||
: base(CommandNameLoadHelper.GetAliasesFor(memberName))
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
15
NadekoBot.Core/Common/Attributes/BotPerm.cs
Normal file
15
NadekoBot.Core/Common/Attributes/BotPerm.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using Discord.Commands;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
public class BotPermAttribute : RequireBotPermissionAttribute
|
||||
{
|
||||
public BotPermAttribute(GuildPerm permission) : base((GuildPermission)permission)
|
||||
{
|
||||
}
|
||||
|
||||
public BotPermAttribute(ChannelPerm permission) : base((ChannelPermission)permission)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
36
NadekoBot.Core/Common/Attributes/CommandNameLoadHelper.cs
Normal file
36
NadekoBot.Core/Common/Attributes/CommandNameLoadHelper.cs
Normal file
@@ -0,0 +1,36 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.IO;
|
||||
using System.Linq;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
public static class CommandNameLoadHelper
|
||||
{
|
||||
|
||||
private static YamlDotNet.Serialization.IDeserializer _deserializer
|
||||
= new YamlDotNet.Serialization.Deserializer();
|
||||
|
||||
public static Lazy<Dictionary<string, string[]>> LazyCommandAliases
|
||||
= new Lazy<Dictionary<string, string[]>>(() => LoadCommandNames());
|
||||
public static Dictionary<string, string[]> LoadCommandNames(string aliasesFilePath = "data/aliases.yml")
|
||||
{
|
||||
var text = File.ReadAllText(aliasesFilePath);
|
||||
return _deserializer.Deserialize<Dictionary<string, string[]>>(text);
|
||||
}
|
||||
|
||||
public static string[] GetAliasesFor(string methodName)
|
||||
=> LazyCommandAliases.Value.TryGetValue(methodName.ToLowerInvariant(), out var aliases) && aliases.Length > 1
|
||||
? aliases.Skip(1).ToArray()
|
||||
: Array.Empty<string>();
|
||||
|
||||
public static string GetCommandNameFor(string methodName)
|
||||
{
|
||||
methodName = methodName.ToLowerInvariant();
|
||||
var toReturn = LazyCommandAliases.Value.TryGetValue(methodName, out var aliases) && aliases.Length > 0
|
||||
? aliases[0]
|
||||
: methodName;
|
||||
return toReturn;
|
||||
}
|
||||
}
|
||||
}
|
16
NadekoBot.Core/Common/Attributes/Description.cs
Normal file
16
NadekoBot.Core/Common/Attributes/Description.cs
Normal file
@@ -0,0 +1,16 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services.Impl;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class DescriptionAttribute : SummaryAttribute
|
||||
{
|
||||
// Localization.LoadCommand(memberName.ToLowerInvariant()).Desc
|
||||
public DescriptionAttribute(string text = "") : base(text)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
9
NadekoBot.Core/Common/Attributes/LeftoverAttribute.cs
Normal file
9
NadekoBot.Core/Common/Attributes/LeftoverAttribute.cs
Normal file
@@ -0,0 +1,9 @@
|
||||
namespace Discord.Commands
|
||||
{
|
||||
public class LeftoverAttribute : RemainderAttribute
|
||||
{
|
||||
public LeftoverAttribute()
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
19
NadekoBot.Core/Common/Attributes/NadekoCommand.cs
Normal file
19
NadekoBot.Core/Common/Attributes/NadekoCommand.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services.Impl;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class NadekoCommandAttribute : CommandAttribute
|
||||
{
|
||||
public NadekoCommandAttribute([CallerMemberName] string memberName="")
|
||||
: base(CommandNameLoadHelper.GetCommandNameFor(memberName))
|
||||
{
|
||||
this.MethodName = memberName.ToLowerInvariant();
|
||||
}
|
||||
|
||||
public string MethodName { get; }
|
||||
}
|
||||
}
|
14
NadekoBot.Core/Common/Attributes/NadekoModuleAttribute.cs
Normal file
14
NadekoBot.Core/Common/Attributes/NadekoModuleAttribute.cs
Normal file
@@ -0,0 +1,14 @@
|
||||
using System;
|
||||
using Discord.Commands;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Class)]
|
||||
sealed class NadekoModuleAttribute : GroupAttribute
|
||||
{
|
||||
public NadekoModuleAttribute(string moduleName) : base(moduleName)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
||||
|
15
NadekoBot.Core/Common/Attributes/NadekoOptions.cs
Normal file
15
NadekoBot.Core/Common/Attributes/NadekoOptions.cs
Normal file
@@ -0,0 +1,15 @@
|
||||
using System;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class NadekoOptionsAttribute : Attribute
|
||||
{
|
||||
public Type OptionType { get; set; }
|
||||
|
||||
public NadekoOptionsAttribute(Type t)
|
||||
{
|
||||
this.OptionType = t;
|
||||
}
|
||||
}
|
||||
}
|
19
NadekoBot.Core/Common/Attributes/OwnerOnlyAttribute.cs
Normal file
19
NadekoBot.Core/Common/Attributes/OwnerOnlyAttribute.cs
Normal file
@@ -0,0 +1,19 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
|
||||
public sealed class OwnerOnlyAttribute : PreconditionAttribute
|
||||
{
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo executingCommand, IServiceProvider services)
|
||||
{
|
||||
var creds = services.GetService<IBotCredentials>();
|
||||
|
||||
return Task.FromResult((creds.IsOwner(context.User) || context.Client.CurrentUser.Id == context.User.Id ? PreconditionResult.FromSuccess() : PreconditionResult.FromError("Not owner")));
|
||||
}
|
||||
}
|
||||
}
|
38
NadekoBot.Core/Common/Attributes/Ratelimit.cs
Normal file
38
NadekoBot.Core/Common/Attributes/Ratelimit.cs
Normal file
@@ -0,0 +1,38 @@
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services;
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
|
||||
namespace NadekoBot.Core.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class RatelimitAttribute : PreconditionAttribute
|
||||
{
|
||||
public int Seconds { get; }
|
||||
|
||||
public RatelimitAttribute(int seconds)
|
||||
{
|
||||
if (seconds <= 0)
|
||||
throw new ArgumentOutOfRangeException(nameof(seconds));
|
||||
|
||||
Seconds = seconds;
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
if (Seconds == 0)
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var cache = services.GetService<IDataCache>();
|
||||
var rem = cache.TryAddRatelimit(context.User.Id, command.Name, Seconds);
|
||||
|
||||
if(rem == null)
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
var msgContent = $"You can use this command again in {rem.Value.TotalSeconds:F1}s.";
|
||||
|
||||
return Task.FromResult(PreconditionResult.FromError(msgContent));
|
||||
}
|
||||
}
|
||||
}
|
21
NadekoBot.Core/Common/Attributes/Usage.cs
Normal file
21
NadekoBot.Core/Common/Attributes/Usage.cs
Normal file
@@ -0,0 +1,21 @@
|
||||
using System;
|
||||
using System.Runtime.CompilerServices;
|
||||
using Discord.Commands;
|
||||
using NadekoBot.Core.Services.Impl;
|
||||
using Newtonsoft.Json;
|
||||
|
||||
namespace NadekoBot.Common.Attributes
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method)]
|
||||
public sealed class UsageAttribute : RemarksAttribute
|
||||
{
|
||||
// public static string GetUsage(string memberName)
|
||||
// {
|
||||
// var usage = Localization.LoadCommand(memberName.ToLowerInvariant()).Usage;
|
||||
// return JsonConvert.SerializeObject(usage);
|
||||
// }
|
||||
public UsageAttribute(string text = "") : base(text)
|
||||
{
|
||||
}
|
||||
}
|
||||
}
|
33
NadekoBot.Core/Common/Attributes/UserPerm.cs
Normal file
33
NadekoBot.Core/Common/Attributes/UserPerm.cs
Normal file
@@ -0,0 +1,33 @@
|
||||
using System;
|
||||
using System.Threading.Tasks;
|
||||
using Discord.Commands;
|
||||
using Microsoft.Extensions.DependencyInjection;
|
||||
using NadekoBot.Modules.Administration.Services;
|
||||
|
||||
namespace Discord
|
||||
{
|
||||
[AttributeUsage(AttributeTargets.Method, AllowMultiple = false)]
|
||||
public class UserPermAttribute : PreconditionAttribute
|
||||
{
|
||||
public RequireUserPermissionAttribute UserPermissionAttribute { get; }
|
||||
|
||||
public UserPermAttribute(GuildPerm permission)
|
||||
{
|
||||
UserPermissionAttribute = new RequireUserPermissionAttribute((GuildPermission)permission);
|
||||
}
|
||||
|
||||
public UserPermAttribute(ChannelPerm permission)
|
||||
{
|
||||
UserPermissionAttribute = new RequireUserPermissionAttribute((ChannelPermission)permission);
|
||||
}
|
||||
|
||||
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
|
||||
{
|
||||
var permService = services.GetService<DiscordPermOverrideService>();
|
||||
if (permService.TryGetOverrides(context.Guild?.Id ?? 0, command.Name.ToUpperInvariant(), out var _))
|
||||
return Task.FromResult(PreconditionResult.FromSuccess());
|
||||
|
||||
return UserPermissionAttribute.CheckPermissionsAsync(context, command, services);
|
||||
}
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user