-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
171 lines (139 loc) · 6.21 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
using DSharpPlus;
using DSharpPlus.CommandsNext;
using DSharpPlus.CommandsNext.Exceptions;
using DSharpPlus.Entities;
using DSharpPlus.EventArgs;
using DSharpPlus.Interactivity;
using DSharpPlus.VoiceNext;
using Newtonsoft.Json;
using System;
using System.IO;
using System.Text;
using System.Threading.Tasks;
namespace Ozone
{
public class Program
{
public DiscordClient Client { get; set; }
public VoiceNextClient VoiceCli { get; set; }
public InteractivityModule Interactivity { get; set; }
public CommandsNextModule Commands { get; set; }
public static int NumOfUsers = 0;
public async Task RunBotAsync()
{
var json = "";
using (var fs = File.OpenRead("config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync();
var cfgjson = JsonConvert.DeserializeObject<ConfigJson>(json);
var cfg = new DiscordConfiguration
{
Token = cfgjson.Token,
TokenType = TokenType.Bot,
AutoReconnect = true,
LogLevel = LogLevel.Debug,
UseInternalLogHandler = true
};
this.Client = new DiscordClient(cfg);
this.Client.Ready += this.Client_Ready;
this.Client.GuildAvailable += this.Client_GuildAvailable;
this.Client.ClientErrored += this.Client_ClientError;
var ccfg = new CommandsNextConfiguration
{
StringPrefix = cfgjson.CommandPrefix,
CaseSensitive = false,
EnableDms = true,
EnableMentionPrefix = true
};
this.Client.UseInteractivity(new InteractivityConfiguration
{
PaginationBehaviour = TimeoutBehaviour.Ignore,
PaginationTimeout = TimeSpan.FromDays(7),
Timeout = TimeSpan.FromDays(7)
});
var Voice = new VoiceNextConfiguration
{
VoiceApplication = DSharpPlus.VoiceNext.Codec.VoiceApplication.Music
};
this.Commands = this.Client.UseCommandsNext(ccfg);
this.Client.UseVoiceNext(Voice);
this.Commands.CommandExecuted += this.Commands_CommandExecuted;
this.Commands.CommandErrored += this.Commands_CommandErrored;
this.Commands.RegisterCommands<TopLevelCommands>();
await this.Client.ConnectAsync();
await Task.Delay(-1);
}
public static void Main(string[] args)
{
var prog = new Program();
prog.RunBotAsync().GetAwaiter().GetResult();
}
private Task Client_Ready(ReadyEventArgs e)
{
e.Client.DebugLogger.LogMessage(LogLevel.Info, "Ozone", "Client is ready to process events.", DateTime.Now);
return Task.CompletedTask;
}
private Task Client_GuildAvailable(GuildCreateEventArgs e)
{
e.Client.DebugLogger.LogMessage(LogLevel.Info, "Ozone", $"Guild available: {e.Guild.Name}", DateTime.Now);
NumOfUsers += e.Guild.MemberCount;
return Task.CompletedTask;
}
private Task Client_ClientError(ClientErrorEventArgs e)
{
e.Client.DebugLogger.LogMessage(LogLevel.Error, "Ozone", $"Exception occured: {e.Exception.GetType()}: {e.Exception.Message}", DateTime.Now);
return Task.CompletedTask;
}
private Task Commands_CommandExecuted(CommandExecutionEventArgs e)
{
e.Context.Client.DebugLogger.LogMessage(LogLevel.Info, "Ozone", $"{e.Context.User.Username} successfully executed '{e.Command.QualifiedName}'", DateTime.Now);
Client.UpdateStatusAsync(new DiscordGame($"with {NumOfUsers} users! | .help"));
return Task.CompletedTask;
}
private async Task Commands_CommandErrored(CommandErrorEventArgs e)
{
e.Context.Client.DebugLogger.LogMessage(LogLevel.Error, "Ozone", $"{e.Context.User.Username} tried executing '{e.Command?.QualifiedName ?? "<unknown command>"}' but it errored: {e.Exception.GetType()}: {e.Exception.Message ?? "<no message>"}", DateTime.Now);
if (e.Exception is ChecksFailedException 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)
};
await e.Context.RespondAsync("", embed: embed);
}
else
{
if (
e.Context.Message.Content.Substring(0, 3) == ".yk" ||
e.Context.Message.Content.Substring(0, 3) == ".p " ||
e.Context.Message.Content.Substring(0, 2) == ".-" ||
e.Context.Message.Content.Substring(0, 2) == "._" ||
e.Context.Message.Content.Substring(0, 2) == ".."
)
{
}
else
{
var emoji = DiscordEmoji.FromName(e.Context.Client, ":x:");
var embed = new DiscordEmbedBuilder
{
Title = $"Error executing the \"{e.Context.Message.Content.Substring(1).Split(' ')[0]}\" command.",
Description = $"{emoji} An error occurred:\n`{e.Exception.Message}`",
Color = new DiscordColor(0xFF0000)
};
await e.Context.RespondAsync("", embed: embed);
}
}
}
}
public struct ConfigJson
{
[JsonProperty("token")]
public string Token { get; private set; }
[JsonProperty("prefix")]
public string CommandPrefix { get; private set; }
}
}