-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
144 lines (119 loc) · 5.07 KB
/
Program.cs
File metadata and controls
144 lines (119 loc) · 5.07 KB
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
using Crumblin__Bot.Commands;
using Discord;
using Discord.Net;
using Discord.WebSocket;
using Quartz;
using Quartz.Impl;
namespace Crumblin__Bot
{
internal class Program
{
private const string TOKEN_FILENAME = "token.txt";
private List<IGuildCommand> GuildCommands;
private DiscordSocketClient _client;
// Do this so we can be in an async context
public static Task Main(string[] args) => new Program().MainAsync();
public Program()
{
// Get all implemented IGuildCommand classes
var type = typeof(IGuildCommand);
var types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(p => type.IsAssignableFrom(p) && !p.IsInterface);
// Instantiate the GuildCommands
GuildCommands = types.Select(t => Activator.CreateInstance(t) as IGuildCommand).ToList();
}
public async Task MainAsync()
{
// Create a new DiscordClient and subscribe to
// our events.
DiscordSocketConfig config = new()
{
UseInteractionSnowflakeDate = false
};
_client = new DiscordSocketClient(config);
_client.SlashCommandExecuted += Client_SlashCommandHandler;
_client.Ready += Client_Ready;
_client.Log += Log;
// Load token from file
var token = File.ReadAllText(TOKEN_FILENAME);
// Log into the Discord bot and start.
await _client.LoginAsync(TokenType.Bot, token);
await _client.StartAsync();
// Block this task until the program is closed.
await Task.Delay(Timeout.Infinite);
}
private async Task PostMenuWeekly()
{
// Grab the Scheduler instance from the Factory
StdSchedulerFactory factory = new StdSchedulerFactory();
IScheduler scheduler = await factory.GetScheduler();
// Create a new Job to post cookie updates.
IJobDetail job = JobBuilder.Create<MenuJob>()
.WithIdentity(name: "cookieUpdates", group: "baseGroup")
.Build();
// Pass the Discord client to the Job we created
job.JobDataMap.Put("client", _client);
// Create a cronTrigger to trigger the job
// Every monday at 10.
ITrigger trigger = TriggerBuilder.Create()
.WithIdentity("trigger1", "baseGroup")
.WithSchedule(CronScheduleBuilder.CronSchedule("\r\n0 0 10 ? * MON *"))
.Build();
// Schedule the job and start it.
await scheduler.ScheduleJob(job, trigger);
await scheduler.Start();
}
private async Task Client_SlashCommandHandler(SocketSlashCommand command)
{
// Get the relevant command interface
IGuildCommand commandType = GuildCommands.FirstOrDefault(x => x.CommandName == command.Data.Name);
// If the command exists, execute it.
if (commandType != null)
{
Task.Run(async () => commandType.HandleCommand(command));
}
}
private async Task Client_Ready()
{
// Get all the guilds we are currently connected to.
Task.Run(async () => {
foreach (var guild in _client.Guilds)
{
// Pull all of the commands so we can create a SlashCommand for each
foreach (IGuildCommand command in GuildCommands)
{
// Create a guild command
SlashCommandBuilder guildCommand = new SlashCommandBuilder();
// Assign properties to it
guildCommand.WithName(command.CommandName);
guildCommand.WithDescription(command.CommandDescription);
SlashCommandBuilder globalCommand = new SlashCommandBuilder();
// Assign properties to it
globalCommand.WithName(command.CommandName);
globalCommand.WithDescription(command.CommandDescription);
// Try to build and create application command
try
{
await guild.CreateApplicationCommandAsync(guildCommand.Build());
await _client.CreateGlobalApplicationCommandAsync(globalCommand.Build());
}
catch (ApplicationCommandException exception)
{
// Print the exception to the console
Log(new LogMessage(LogSeverity.Error, command.CommandName, exception.Message, exception));
}
}
}
// Schedule weekly cookie updates to
// Guilds that are subscribed them.
PostMenuWeekly();
});
}
private Task Log(LogMessage msg)
{
Console.WriteLine(msg.ToString());
return Task.CompletedTask;
}
}
}