-
-
Notifications
You must be signed in to change notification settings - Fork 11
/
Program.cs
129 lines (108 loc) · 4.31 KB
/
Program.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
using DSharpPlus;
using DSharpPlus.CommandsNext.Exceptions;
using DSharpPlus.CommandsNext;
using DSharpPlus.Entities;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Enums;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.SlashCommands;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using DSharpPlus.SlashCommands.EventArgs;
using Microsoft.Extensions.Logging;
namespace PoisnCopy;
internal class Program
{
public readonly EventId BotEventId = new(42, "PoisnCopy");
private CancellationTokenSource _cts { get; set; }
private IConfigurationRoot _config;
private DiscordClient _discord;
private InteractivityExtension _interactivity;
private static async Task Main(string[] args) => await new Program().InitBot(args);
private async Task InitBot(string[] args)
{
try
{
Console.WriteLine("[info] Welcome to my bot!");
_cts = new CancellationTokenSource();
Console.WriteLine("[info] Loading config file..");
_config = new ConfigurationBuilder()
.SetBasePath(Directory.GetCurrentDirectory())
.AddJsonFile("config.json", optional: false, reloadOnChange: true)
.Build();
Console.WriteLine("[info] Creating discord client..");
_discord = new DiscordClient(
new DiscordConfiguration
{
Token = _config.GetValue<string>("discord:token"),
TokenType = TokenType.Bot,
Intents = DiscordIntents.AllUnprivileged | DiscordIntents.MessageContents
}
);
_interactivity = _discord.UseInteractivity(
new InteractivityConfiguration()
{
PaginationBehaviour = PaginationBehaviour.WrapAround,
PaginationDeletion = PaginationDeletion.DeleteMessage,
Timeout = TimeSpan.FromSeconds(30)
}
);
var services = BuildServices();
var slash = _discord.UseSlashCommands(
new SlashCommandsConfiguration { Services = services }
);
slash.SlashCommandErrored += Commands_CommandErrored;
slash.RegisterCommands<SlashCommands>();
Console.WriteLine("[info] Loading command modules..");
await RunAsync(args);
}
catch (Exception ex)
{
Console.Error.WriteLine(ex.ToString());
}
}
private async Task RunAsync(string[] args)
{
// Connect to discord's service
Console.WriteLine("Connecting..");
await _discord.ConnectAsync(new DiscordActivity { Name = "ko-fi.com/poisnfang", });
Console.WriteLine("Connected!");
var connections = _discord.Guilds.Count;
Console.WriteLine($"I am running on {connections} servers");
// Keep the bot running until the cancellation token requests we stop
while (!_cts.IsCancellationRequested)
await Task.Delay(TimeSpan.FromMinutes(1));
}
private ServiceProvider BuildServices()
{
var deps = new ServiceCollection();
deps.AddSingleton(_interactivity)
.AddSingleton(_cts)
.AddSingleton(_config)
.AddSingleton(_discord);
return deps.BuildServiceProvider();
}
private async Task Commands_CommandErrored(
SlashCommandsExtension sender,
SlashCommandErrorEventArgs e
)
{
e.Context.Client.Logger.LogError(
BotEventId,
$"{e.Context.User.Username} tried executing '{e.Context.CommandName ?? "<unknown command>"}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no message>"}",
DateTime.Now
);
if (e.Exception is SlashExecutionChecksFailedException ex)
{
var emoji = DiscordEmoji.FromName(e.Context.Client, ":no_entry:");
var embed = new DiscordEmbedBuilder
{
Title = "Access denied",
Description =
$"{emoji} You do not have the permissions required to execute this command.",
Color = new DiscordColor(0xFF0000) // red
};
await e.Context.CreateResponseAsync(embed);
}
}
}