forked from berichan/SysBot.ACNHOrders
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathGlobals.cs
44 lines (36 loc) · 1.79 KB
/
Globals.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
using System;
using System.Linq;
using System.Threading.Tasks;
using Discord.Commands;
using Discord.WebSocket;
namespace SysBot.ACNHOrders
{
public static class Globals
{
public static SysCord Self { get; set; } = default!;
public static CrossBot Bot { get; set; } = default!;
public static QueueHub Hub { get; set; } = default!;
}
public sealed class RequireQueueRoleAttribute : PreconditionAttribute
{
// Create a field to store the specified name
private readonly string _name;
// Create a constructor so the name can be specified
public RequireQueueRoleAttribute(string name) => _name = name;
public override Task<PreconditionResult> CheckPermissionsAsync(ICommandContext context, CommandInfo command, IServiceProvider services)
{
var mgr = Globals.Bot.Config;
if (mgr.CanUseSudo(context.User.Id) || Globals.Self.Owner == context.User.Id || mgr.IgnoreAllPermissions)
return Task.FromResult(PreconditionResult.FromSuccess());
// Check if this user is a Guild User, which is the only context where roles exist
if (context.User is not SocketGuildUser gUser)
return Task.FromResult(PreconditionResult.FromError("You must be in a guild to run this command."));
if (!mgr.AcceptingCommands)
return Task.FromResult(PreconditionResult.FromError("Sorry, I am not currently accepting commands!"));
bool hasRole = mgr.GetHasRole(_name, gUser.Roles.Select(z => z.Name));
if (!hasRole)
return Task.FromResult(PreconditionResult.FromError("You do not have the required role to run this command."));
return Task.FromResult(PreconditionResult.FromSuccess());
}
}
}