-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCommands.cs
134 lines (102 loc) · 5.06 KB
/
Commands.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
using DSharpPlus;
using DSharpPlus.Entities;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.Attributes;
namespace BirthdayBot
{
[SlashCommandGroup("birthday", "Allows you to add, remove and list birthdays.")]
public class Commands : ApplicationCommandModule
{
// Only server admins can use the setup channel command
[SlashCommand("channel", "Sets up the bot in the current channel"), SlashRequireUserPermissions(Permissions.Administrator)]
public static async Task SetupChannel(InteractionContext ctx,
[Option("channel", "The channel to set up the bot in")] DiscordChannel channel)
{
await Database.SetChannelId(ctx.Guild.Id, channel.Id);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"Bot set up in {channel.Mention}!"));
}
// Only server admins can use the setup role command
[SlashCommand("role", "Sets up the role for the bot to assign to the birthday person"), SlashRequireUserPermissions(Permissions.Administrator)]
public static async Task SetupRole(InteractionContext ctx,
[Option("role", "Role that will be assigned to the birthday person")] DiscordRole role)
{
await Database.SetRole(ctx.Guild.Id, role.Id);
await ctx.CreateResponseAsync(InteractionResponseType.ChannelMessageWithSource,
new DiscordInteractionResponseBuilder().WithContent($"Configured {role.Mention}!"));
}
[SlashCommand("add", "Adds your birthday to the bot")]
public static async Task AddBirthday(InteractionContext ctx,
[Option("day", "Your birthday day")] long day,
[Option("month", "Your birthday month")] long month,
[Option("year", "Your birthday year (optional)")] long? year = null)
{
DateTime birthday;
if (year.HasValue)
{
if (!DateTime.TryParse($"{year.Value}-{month}-{day}", out birthday))
{
await ctx.CreateResponseAsync("Invalid date!", true);
return;
}
}
else
{
try
{
birthday = new DateTime(0001, (int)month, (int)day);
}
catch (ArgumentOutOfRangeException)
{
await ctx.CreateResponseAsync("Invalid date!", true);
return;
}
}
await Database.AddBirthday(ctx.Guild.Id, ctx.User.Id, birthday);
await ctx.CreateResponseAsync("Birthday added!", true);
await Functions.CheckConfig(ctx);
await Functions.UpdateListEmbeds(ctx);
}
[SlashCommand("remove", "Removes your birthday from the bot")]
public static async Task RemoveBirthday(InteractionContext ctx)
{
await Database.RemoveBirthday(ctx.Guild.Id, ctx.User.Id);
await ctx.CreateResponseAsync("Birthday removed!", true);
await Functions.UpdateListEmbeds(ctx);
}
[SlashCommand("list", "Lists all set birthdays and their current age if they have set a year")]
public static async Task ListBirthdays(InteractionContext ctx)
{
// Return something else it throws an error...
await ctx.CreateResponseAsync("Here's a new list!", true);
// Fetch birthdays
List<(ulong guildId, ulong userId, DateTime birthday)> birthdays = await Database.GetBirthdays(ctx.Guild.Id);
// Build embed
DiscordEmbed embed = Functions.BuildListEmbed(birthdays, ctx.Guild);
// Send Embed
DiscordMessage message = await ctx.Channel.SendMessageAsync(embed: embed);
// Save its id to the DB
await Database.SetListMessageId(ctx.Guild.Id, ctx.Channel.Id, message.Id);
}
private readonly BirthdayCheckerService _birthdayChecker;
public Commands(BirthdayCheckerService birthdayChecker)
{
_birthdayChecker = birthdayChecker;
}
[SlashCommand("check", "Manually checks for birthdays and sends a message in the specified channel"), SlashRequireUserPermissions(Permissions.Administrator)]
public async Task CheckBirthdays(InteractionContext ctx)
{
await Functions.CheckConfig(ctx);
await _birthdayChecker.CheckBirthdaysAsync();
await ctx.CreateResponseAsync("Ran birthday check manually!", true);
await Functions.UpdateListEmbeds(ctx);
}
[SlashCommand("update", "Updates all lists in the server"), SlashRequireUserPermissions(Permissions.Administrator)]
public static async Task UpdateListCommand(InteractionContext ctx)
{
await Functions.CheckConfig(ctx);
await ctx.CreateResponseAsync("Ran manual update!", true);
await Functions.UpdateListEmbeds(ctx);
}
}
}