-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBot.cs
150 lines (123 loc) · 5.6 KB
/
Bot.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
// Copyright [2021] [NotSoNitro]
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Attributes;
using DSharpPlus.CommandsNext.Exceptions;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Exceptions;
using DSharpPlus.Interactivity;
using DSharpPlus.Interactivity.Extensions;
using DSharpPlus.Lavalink;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Logging;
using Newtonsoft.Json;
using Rem.Commands;
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Reflection;
using System.Text;
using System.Threading.Tasks;
namespace Rem
{
public class Bot
{
public DiscordClient Client { get; private set; }
public InteractivityExtension Interactivity { get; private set; }
public CommandsNextExtension Commands { get; private set; }
public object SocketErrored { get; private set; }
public EventId RemBotEventId { get; private set; }
public async Task RunAsync()
{
var json = string.Empty;
using (var fs = File.OpenRead("config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync().ConfigureAwait(false);
var configJson = JsonConvert.DeserializeObject<ConfigJson>(json);
var config = new DiscordConfiguration
{
Token = configJson.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
MinimumLogLevel = LogLevel.Debug
};
Client = new DiscordClient(config);
var commandsConfig = new CommandsNextConfiguration
{
StringPrefixes = new string[] { configJson.Prefix },
EnableDms = false,
EnableMentionPrefix = false,
CaseSensitive = false
};
Commands = Client.UseCommandsNext(commandsConfig);
Client.SocketErrored += Discord_SocketError;
Client.GuildAvailable += Discord_GuildAvailable;
Client.GuildCreated += Discord_GuildCreated;
Client.GuildDownloadCompleted += Discord_GuildDownloadCompleted;
Commands.CommandErrored += OnError;
Commands.CommandExecuted += OnExecute;
Commands.RegisterCommands<NormalCommands>();
Commands.RegisterCommands<FunCommands>();
Commands.RegisterCommands<AdminCommands>();
Commands.RegisterCommands<BotOwnerCommands>();
await Client.ConnectAsync();
await Task.Delay(-1);
}
private Task Discord_GuildAvailable(DiscordClient client, GuildCreateEventArgs e)
{
client.Logger.LogInformation(RemBotEventId, "Guild available: '{0}'", e.Guild.Name);
return Task.CompletedTask;
}
private Task Discord_GuildCreated(DiscordClient client, GuildCreateEventArgs e)
{
client.Logger.LogInformation(RemBotEventId, "Guild created: '{0}'", e.Guild.Name);
return Task.CompletedTask;
}
private Task Discord_SocketError(DiscordClient client, SocketErrorEventArgs e)
{
var ex = e.Exception is AggregateException ae ? ae.InnerException : e.Exception;
client.Logger.LogError(RemBotEventId, ex, "WebSocket threw an exception");
return Task.CompletedTask;
}
private Task Discord_GuildDownloadCompleted(DiscordClient client, GuildDownloadCompletedEventArgs e)
{
client.Logger.LogDebug(RemBotEventId, "Guild download completed");
return Task.CompletedTask;
}
private async Task OnError(object sender, CommandErrorEventArgs log)
{
log.Context.Client.Logger.Log(LogLevel.Error, $"[Rem]: {log.Context.User.Username} tried executing '{log.Command?.QualifiedName ?? "<unknown command>"}' but it errored: {log.Exception.GetType()}: {log.Exception.Message ?? "<no message>"}");
if (log.Exception is ChecksFailedException)
{
await log.Context.RespondAsync("You Do Not Have Permission To Run This Command.").ConfigureAwait(false);
}
else if (log.Exception is ArgumentException)
{
await log.Context.RespondAsync($"Invalid Argument. \nMessage: `{log.Exception.Message}`").ConfigureAwait(false);
}
else if (log.Exception is MissingFieldException)
{
await log.Context.RespondAsync("Argument Does Not Exist.").ConfigureAwait(false);
}
else if (log.Exception is CommandNotFoundException)
{
await log.Context.RespondAsync("Invalid Command.").ConfigureAwait(false);
}
else if (log.Exception is BadRequestException)
{
await log.Context.RespondAsync($"Command Failed. \nError: `{log.Exception.Message}`").ConfigureAwait(false);
}
else if (log.Exception is UnauthorizedException)
{
await log.Context.RespondAsync($"I Am Unauthorized To Do That.").ConfigureAwait(false);
}
}
private Task OnExecute(object sender, CommandExecutionEventArgs log)
{
log.Context.Client.Logger.LogInformation($"[Rem]: {log.Context.User.Username} successfully executed '{log.Command.QualifiedName}'");
return Task.CompletedTask;
}
}
}