Skip to content

Commit 59a99f5

Browse files
committed
Instead of implementing the IService interface, Services now have an attribute with Name and Purpose properties.
Also added ReflectionExtensions, and fixed up the DIExtensions class to reflect the changes made in this commit.
1 parent d58dd0d commit 59a99f5

20 files changed

+87
-43
lines changed

src/Commands/Modules/Admin/SelfRoleCommands.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ public partial class AdminModule : VolteModule
1111
{
1212
[Command("SelfRoleAdd", "SrA", "SrAdd")]
1313
[Description("Adds a role to the list of self roles for this guild.")]
14-
[Remarks("Usage: |prefix|selfroleadd {roleName}")]
14+
[Remarks("Usage: |prefix|selfroleadd {role}")]
1515
[RequireGuildAdmin]
1616
public async Task SelfRoleAddAsync([Remainder] SocketRole role)
1717
{
@@ -24,13 +24,13 @@ public async Task SelfRoleAddAsync([Remainder] SocketRole role)
2424

2525
[Command("SelfRoleRemove", "SrR", "SrRem")]
2626
[Description("Removes a role from the list of self roles for this guild.")]
27-
[Remarks("Usage: |prefix|selfrole remove {roleName}")]
27+
[Remarks("Usage: |prefix|selfrole remove {role}")]
2828
[RequireGuildAdmin]
2929
public async Task SelfRoleRemAsync([Remainder] SocketRole role)
3030
{
3131
var config = Db.GetConfig(Context.Guild);
3232

33-
if (config.SelfRoles.Any(x => x.EqualsIgnoreCase(role.Name)))
33+
if (config.SelfRoles.ContainsIgnoreCase(role.Name))
3434
{
3535
config.SelfRoles.Remove(role.Name);
3636
await Context.CreateEmbed($"Removed **{role.Name}** from the Self Roles list for this guild.")

src/Commands/Modules/Owner/EvalCommand.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -48,9 +48,10 @@ await ExecutorUtil.ExecuteAsync(async () =>
4848

4949
var imports = new[]
5050
{
51-
"System", "System.Collections.Generic", "System.Linq", "System.Text", "System.Threading.Tasks",
52-
"System.Diagnostics", "Discord", "Qmmands", "Discord.WebSocket", "System.IO",
53-
"System.Threading", "Volte.Extensions", "Volte.Utils", "Volte.Runtime", "Volte.Data"
51+
"System", "System.Collections.Generic", "System.Linq", "System.Text",
52+
"System.Diagnostics", "Discord", "Discord.WebSocket", "System.IO", "Volte.Data",
53+
"System.Threading", "Volte.Extensions", "Volte.Utils", "Volte.Runtime",
54+
"Volte.Discord", "Volte.Services", "System.Threading.Tasks", "Qmmands"
5455
};
5556

5657
sopts = sopts.WithImports(imports).WithReferences(AppDomain.CurrentDomain.GetAssemblies()

src/Extensions/DependencyInjectionExtensions.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
using System.Linq;
33
using System.Reflection;
44
using Microsoft.Extensions.DependencyInjection;
5-
using RestSharp.Extensions;
65
using Volte.Services;
76

87
namespace Volte.Extensions
@@ -13,9 +12,8 @@ public static IServiceCollection AddVolteServices(this IServiceCollection provid
1312
{
1413
//get all the classes that implement the IService interface, arent an interface themselves, and haven't been marked as Obsolete like the GitHubService.
1514
foreach (var service in Assembly.GetEntryAssembly().GetTypes()
16-
.Where(t => typeof(IService).IsAssignableFrom(t) &&
17-
!t.IsInterface &&
18-
t.GetAttribute<ObsoleteAttribute>() == null))
15+
.Where(t => !t.HasAttribute<ObsoleteAttribute>() &&
16+
t.HasAttribute<ServiceAttribute>()))
1917
{
2018
provider.AddSingleton(service);
2119
}
Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
using System;
2+
using System.Reflection;
3+
using RestSharp.Extensions;
4+
5+
namespace Volte.Extensions
6+
{
7+
public static class ReflectionExtensions
8+
{
9+
10+
public static bool HasAttribute<T>(this TypeInfo type) where T : Attribute
11+
=> type.GetAttribute<T>() != null;
12+
13+
public static bool HasAttribute<T>(this Type type) where T : Attribute
14+
=> type.GetAttribute<T>() != null;
15+
16+
}
17+
}

src/Services/AntilinkService.cs

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,13 +2,12 @@
22
using System.Threading.Tasks;
33
using RestSharp;
44
using Volte.Commands;
5-
using Volte.Discord;
65
using Volte.Extensions;
7-
using Volte.Utils;
86

97
namespace Volte.Services
108
{
11-
public sealed class AntilinkService : IService
9+
[Service("Antilink", "The main Service for checking links sent in chat.")]
10+
public sealed class AntilinkService
1211
{
1312
private readonly DatabaseService _db;
1413

src/Services/AutoroleService.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,12 @@
11
using System.Linq;
22
using System.Threading.Tasks;
33
using Discord;
4-
using Volte.Discord;
54
using Volte.Extensions;
65

76
namespace Volte.Services
87
{
9-
public sealed class AutoroleService : IService
8+
[Service("Autorole", "The main Service for automatically applying roles when a user joins any given guild.")]
9+
public sealed class AutoroleService
1010
{
1111
private readonly DatabaseService _db;
1212

src/Services/BlacklistService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Volte.Services
66
{
7-
public sealed class BlacklistService : IService
7+
[Service("Blacklist", "The main Service for checking messages for blacklisted words/phrases in user's messages.")]
8+
public sealed class BlacklistService
89
{
910
private readonly DatabaseService _db;
1011

src/Services/DatabaseService.cs

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,8 @@
66

77
namespace Volte.Services
88
{
9-
public sealed class DatabaseService : IService
9+
[Service("Database", "The main Service for interacting with the Volte database.")]
10+
public sealed class DatabaseService
1011
{
1112
public static readonly LiteDatabase Database = new LiteDatabase("data/Volte.db");
1213

src/Services/DebugService.cs

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,14 @@
22
using Newtonsoft.Json;
33
using Newtonsoft.Json.Linq;
44
using RestSharp;
5+
using Volte.Extensions;
56

67
namespace Volte.Services
78
{
8-
public sealed class DebugService : IService
9+
[Service("Debug", "The main Service that handles HTTP POST requests for debug reports to debug.scarsz.me.")]
10+
public sealed class DebugService
911
{
10-
public string Execute(string config)
12+
public string Execute(string json)
1113
{
1214
var files = new Dictionary<string, Dictionary<string, string>>
1315
{
@@ -27,7 +29,7 @@ public string Execute(string config)
2729
"2-Server.conf", new Dictionary<string, string>
2830
{
2931
{
30-
"content", config
32+
"content", json
3133
},
3234
{
3335
"description", "Server config for debug purposes."
@@ -47,9 +49,8 @@ public string Execute(string config)
4749
req.RequestFormat = DataFormat.Json;
4850
req.Parameters.Clear();
4951
req.AddParameter("application/json", JsonConvert.SerializeObject(payload), ParameterType.RequestBody);
50-
return ((JObject) JsonConvert
51-
.DeserializeObject(httpClient.Execute(req).Content))
52-
.GetValue("url").ToString();
52+
return JsonConvert.DeserializeObject(httpClient.Execute(req).Content).Cast<JObject>().GetValue("url")
53+
.ToString();
5354
}
5455
}
5556
}

src/Services/DefaultWelcomeService.cs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,8 @@
44

55
namespace Volte.Services
66
{
7-
public sealed class DefaultWelcomeService : IService
7+
[Service("DefaultWelcome", "The main Service that handles default welcome/leaving functionality when no WelcomeApiKey is set in the bot config.")]
8+
public sealed class DefaultWelcomeService
89
{
910
private readonly DatabaseService _db;
1011

@@ -23,7 +24,9 @@ internal async Task JoinAsync(IGuildUser user)
2324
.Replace("{UserName}", user.Username)
2425
.Replace("{UserMention}", user.Mention)
2526
.Replace("{OwnerMention}", (await user.Guild.GetOwnerAsync()).Mention)
26-
.Replace("{UserTag}", user.Discriminator);
27+
.Replace("{UserTag}", user.Discriminator)
28+
.Replace("{MemberCount}", (await user.Guild.GetUsersAsync()).Count.ToString())
29+
.Replace("{UserString}", user.ToString());
2730
var c = await user.Guild.GetTextChannelAsync(config.WelcomeOptions.WelcomeChannel);
2831

2932
if (!(c is null))
@@ -48,12 +51,15 @@ internal async Task LeaveAsync(IGuildUser user)
4851
.Replace("{UserName}", user.Username)
4952
.Replace("{UserMention}", user.Mention)
5053
.Replace("{OwnerMention}", (await user.Guild.GetOwnerAsync()).Mention)
51-
.Replace("{UserTag}", user.Discriminator);
54+
.Replace("{UserTag}", user.Discriminator)
55+
.Replace("{MemberCount}", (await user.Guild.GetUsersAsync()).Count.ToString())
56+
.Replace("{UserString}", user.ToString());
5257
var c = await user.Guild.GetTextChannelAsync(config.WelcomeOptions.WelcomeChannel);
5358
if (!(c is null))
5459
{
5560
var embed = new EmbedBuilder()
56-
.WithColor(new Color(config.WelcomeOptions.WelcomeColorR,
61+
.WithColor(new Color(
62+
config.WelcomeOptions.WelcomeColorR,
5763
config.WelcomeOptions.WelcomeColorG,
5864
config.WelcomeOptions.WelcomeColorB)
5965
)

0 commit comments

Comments
 (0)