This repository has been archived by the owner on May 18, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
Program.cs
100 lines (78 loc) · 3.74 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
using System;
using System.Threading.Tasks;
using Discord;
using Discord.Commands;
using Discord.WebSocket;
namespace notetoself_mongo
{
class Program {
NotesManager Notes;
DiscordShardedClient Client;
CommandService Command;
static void Main(string[] args) => new Program().Execute().GetAwaiter().GetResult();
async Task Execute() {
Console.WriteLine("Starting Note to Self Bot v1.1 ...");
MasterConfig Config = MasterConfig.Load();
if (Config == null) Config = MasterConfig.Setup();
Config.Save();
Notes = new NotesManager(Config.MongoUser, Config.MongoPass);
DiscordSocketConfig ClientConfig = new DiscordSocketConfig() {
DefaultRetryMode = RetryMode.AlwaysFail,
LogLevel = LogSeverity.Info
};
CommandServiceConfig CommandConfig = new CommandServiceConfig() { DefaultRunMode = RunMode.Async };
Client = new DiscordShardedClient(ClientConfig);
Command = new CommandService(CommandConfig);
await InitializeCommands();
Client.Log += (msg) => Log(msg);
await Client.LoginAsync(TokenType.Bot, Config.Token);
await Client.StartAsync();
await Task.Delay(-1);
}
async Task InitializeCommands() {
Client.MessageReceived += ExecuteCommandHandler;
await Command.AddModulesAsync(System.Reflection.Assembly.GetEntryAssembly());
}
async Task ExecuteCommandHandler(SocketMessage MessageParam) {
await Task.Factory.StartNew(() => ExecuteCommand(MessageParam));
}
async Task ExecuteCommand(SocketMessage MessageParam) {
SocketUserMessage Message = (SocketUserMessage)MessageParam;
if (Message == null) return;
int ArgPos = 0;
if (!(Message.HasStringPrefix("notetoself! ", ref ArgPos) || Message.HasStringPrefix("nts! ", ref ArgPos) || Message.HasMentionPrefix(Client.CurrentUser, ref ArgPos))) return;
if (Message.Author.IsBot || Message.Author.IsWebhook) return;
IGuildUser User = (IGuildUser)Message.Author;
IGuildUser Self = await User.Guild.GetCurrentUserAsync();
IGuildChannel Channel = (IGuildChannel)Message.Channel;
ChannelPermissions Perms = Self.GetPermissions(Channel);
if (!Perms.EmbedLinks) {
await Message.Channel.SendMessageAsync("I'm sorry but I require embeds to be enabled before I can start posting to chat.");
return;
}
NoteContext Context = new NoteContext(Client, Message, Notes);
var Result = await Command.ExecuteAsync(Context, ArgPos);
if (!Result.IsSuccess) {
string Error = $"Command Error: {Result.ErrorReason}";
Console.WriteLine(Error);
switch (Result.Error) {
case CommandError.BadArgCount:
await Message.Channel.SendMessageAsync(Error);
break;
case CommandError.UnmetPrecondition:
await Message.Channel.SendMessageAsync(Error);
break;
case CommandError.Unsuccessful:
await Message.Channel.SendMessageAsync(Error);
break;
default:
Console.WriteLine(Error);
break;
}
}
}
async Task Log(LogMessage Message) {
await Task.Factory.StartNew(() => Console.WriteLine($"[ {DateTime.Now.ToString("HH:mm:ss")} ] <=> {Message.ToString()}"));
}
}
}