-
Notifications
You must be signed in to change notification settings - Fork 3
/
JoinRolesSetupCommands.cs
169 lines (135 loc) · 6.05 KB
/
JoinRolesSetupCommands.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
using System.Collections.Generic;
using System.Linq;
using System.Threading.Tasks;
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.Entities;
namespace RoboBot
{
public class JoinRolesSetupCommands : BaseCommandModule
{
private class CommandNames
{
public const string Enable = "joinroleenable";
public const string Disable = "joinroledisable";
public const string AddRole = "joinroleadd";
public const string RemoveRole = "joinroleremove";
public const string ListRoles = "joinrolelist";
}
private const Permissions RequiredPermissions = Permissions.Administrator;
private static JoinRolesInteractions _joinRolesInteractions;
public static void SetJoinRoleInteractions(JoinRolesInteractions joinRolesInteractions) =>
_joinRolesInteractions = joinRolesInteractions;
[RequireGuild]
[Command(CommandNames.Enable)]
public async Task Enable(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
if (_joinRolesInteractions.JoinRoles.FirstOrDefault(x => x.Guild == ctx.Guild) != null)
{
await ctx.RespondAsync($"JoinRoles are already enabled for this guild");
return;
}
_joinRolesInteractions.JoinRoles.Add(new JoinRoles(ctx.Guild, new List<JoinRole>()));
await ctx.RespondAsync(
$"JoinRoles are now enabled for this guild, add roles with {ctx.Prefix}{CommandNames.AddRole}");
_joinRolesInteractions.SaveToFile();
}
[RequireGuild]
[Command(CommandNames.Disable)]
public async Task Disable(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
JoinRoles associatedJoinRoles =
_joinRolesInteractions.JoinRoles.FirstOrDefault(joinRoles => joinRoles.Guild == ctx.Guild);
if (associatedJoinRoles == null)
{
await ctx.RespondAsync("JoinRoles are not enabled for this guild");
return;
}
_joinRolesInteractions.JoinRoles.Remove(associatedJoinRoles);
await ctx.RespondAsync("Disabled JoinRoles for this guild");
_joinRolesInteractions.SaveToFile();
}
[RequireGuild]
[Command(CommandNames.AddRole)]
public async Task AddRole(CommandContext ctx, DiscordRole role)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
JoinRoles associatedJoinRoles =
_joinRolesInteractions.JoinRoles.FirstOrDefault(joinRoles => joinRoles.Guild == ctx.Guild);
if (associatedJoinRoles == null)
{
await ctx.RespondAsync("JoinRoles are not enabled for this guild");
return;
}
JoinRole joinRole = new JoinRole(role);
if (associatedJoinRoles.Roles.Contains(joinRole))
{
await ctx.RespondAsync("This role was already added");
return;
}
associatedJoinRoles.Roles.Add(joinRole);
await ctx.RespondAsync($"Added {role.Name} to the roles to add when a person joins");
_joinRolesInteractions.SaveToFile();
}
[RequireGuild]
[Command(CommandNames.AddRole)]
public async Task AddRole(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide a mention to the role for this command");
}
[RequireGuild]
[Command(CommandNames.RemoveRole)]
public async Task RemoveRole(CommandContext ctx, DiscordRole role)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
JoinRoles associatedJoinRoles =
_joinRolesInteractions.JoinRoles.FirstOrDefault(joinRoles => joinRoles.Guild == ctx.Guild);
if (associatedJoinRoles == null)
{
await ctx.RespondAsync("JoinRoles are not enabled for this guild");
return;
}
JoinRole joinRole = new JoinRole(role);
if (!associatedJoinRoles.Roles.Contains(joinRole))
{
await ctx.RespondAsync("This role was not added in the first place");
return;
}
associatedJoinRoles.Roles.Remove(joinRole);
await ctx.RespondAsync($"Removed {role.Name} to the roles to add when a person joins");
_joinRolesInteractions.SaveToFile();
}
[RequireGuild]
[Command(CommandNames.RemoveRole)]
public async Task RemoveRole(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
await ctx.RespondAsync("You need to provide a mention to the role for this command");
}
[RequireGuild]
[Command(CommandNames.ListRoles)]
public async Task ListRoles(CommandContext ctx)
{
if (!await CommandHelpers.CheckPermissions(ctx, RequiredPermissions))
return;
JoinRoles associatedJoinRoles =
_joinRolesInteractions.JoinRoles.FirstOrDefault(joinRoles => joinRoles.Guild == ctx.Guild);
if (associatedJoinRoles == null)
{
await ctx.RespondAsync("JoinRoles are not enabled for this guild");
return;
}
await ctx.RespondAsync(associatedJoinRoles.ToString());
}
}
}