This repository has been archived by the owner on Mar 17, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
153 lines (117 loc) · 5.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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
using System;
using System.Threading.Tasks;
using Microsoft.Extensions.Logging;
using DSharpPlus;
using DSharpPlus.EventArgs;
using DSharpPlus.Entities;
using DSharpPlus.Net;
using DSharpPlus.Lavalink;
using System.Linq;
using DSharpPlus.SlashCommands;
using DSharpPlus.SlashCommands.EventArgs;
using TomatenMusic.Commands;
using Newtonsoft.Json;
using System.IO;
using System.Text;
using Microsoft.Extensions.DependencyInjection;
using TomatenMusic.Music;
using SpotifyAPI.Web.Auth;
using SpotifyAPI.Web;
using DSharpPlus.Exceptions;
namespace TomatenMusic
{
class Program
{
public static DiscordClient Discord { get; private set; }
public static Spotify spotify { get; private set; }
static void Main(string[] args)
{
new Program().InitBotAsync(args).ConfigureAwait(false).GetAwaiter().GetResult();
}
public struct ConfigJson
{
[JsonProperty("TOKEN")]
public string Token { get; private set; }
[JsonProperty("LavaLinkPassword")]
public string LavaLinkPassword { get; private set; }
[JsonProperty("SpotifyClientId")]
public string SpotifyClientId { get; private set; }
[JsonProperty("SpotifyClientSecret")]
public string SpotifyClientSecret { get; private set; }
[JsonProperty("YoutubeApiKey")]
public string YoutubeAPIKey { get; private set; }
}
public static ConfigJson config;
private async Task InitBotAsync(string[] args)
{
await initJson();
Discord = new DiscordClient(new DiscordConfiguration
{
TokenType = TokenType.Bot,
Token = config.Token,
MinimumLogLevel = LogLevel.Debug,
Intents = DiscordIntents.All
});
var lavaEndPoint = new ConnectionEndpoint
{
Hostname = "116.202.92.16",
Port = 2333
};
var lavalinkConfig = new LavalinkConfiguration
{
Password = config.LavaLinkPassword,
RestEndpoint = lavaEndPoint,
SocketEndpoint = lavaEndPoint
};
var lavalink = Discord.UseLavalink();
var slash = Discord.UseSlashCommands();
spotify = new Spotify(SpotifyClientConfig.CreateDefault().WithAuthenticator(new ClientCredentialsAuthenticator(config.SpotifyClientId, config.SpotifyClientSecret)));
Discord.Ready += Discord_Ready;
//Discord.GetSlashCommands().RegisterCommands<MusicCommands>(835089895092387872);
//Discord.GetSlashCommands().RegisterCommands<PlayCommandGroup>(835089895092387872);
Discord.GetSlashCommands().RegisterCommands<MusicCommands>(888493810554900491);
Discord.GetSlashCommands().RegisterCommands<PlayCommandGroup>(888493810554900491);
slash.SlashCommandInvoked += Slash_SlashCommandInvoked;
slash.SlashCommandErrored += Slash_SlashCommandErrored;
Discord.ClientErrored += Discord_ClientErrored;
await Discord.ConnectAsync();
await lavalink.ConnectAsync(lavalinkConfig);
await Task.Delay(-1);
}
private Task Discord_ClientErrored(DiscordClient sender, ClientErrorEventArgs e)
{
Discord.Logger.LogDebug("Event {0} errored with Exception {3}", e.EventName, e.Exception);
if (e.Exception is NotFoundException)
Discord.Logger.LogDebug($"{ ((NotFoundException)e.Exception).JsonMessage }");
if (e.Exception is BadRequestException)
Discord.Logger.LogDebug($"{ ((BadRequestException)e.Exception).Errors }");
return Task.CompletedTask;
}
private async Task initJson()
{
var json = "";
using (var fs = File.OpenRead("config.json"))
using (var sr = new StreamReader(fs, new UTF8Encoding(false)))
json = await sr.ReadToEndAsync();
config = JsonConvert.DeserializeObject<ConfigJson>(json);
}
private Task Slash_SlashCommandErrored(SlashCommandsExtension sender, SlashCommandErrorEventArgs e)
{
Discord.Logger.LogDebug("Command {0} invoked by {1} on Guild {2} with Exception {3}", e.Context.CommandName, e.Context.Member, e.Context.Guild, e.Exception);
if (e.Exception is NotFoundException)
Discord.Logger.LogDebug($"{ ((NotFoundException)e.Exception).JsonMessage }");
if (e.Exception is BadRequestException)
Discord.Logger.LogDebug($"{ ((BadRequestException)e.Exception).JsonMessage }");
return Task.CompletedTask;
}
private Task Slash_SlashCommandInvoked(SlashCommandsExtension sender, DSharpPlus.SlashCommands.EventArgs.SlashCommandInvokedEventArgs e)
{
Discord.Logger.LogDebug("Command {0} invoked by {1} on Guild {2}", e.Context.CommandName, e.Context.Member, e.Context.Guild);
return Task.CompletedTask;
}
private async Task Discord_Ready(DiscordClient sender, ReadyEventArgs e)
{
await Discord.UpdateStatusAsync(new DiscordActivity("/ commands!", ActivityType.Watching), UserStatus.Online);
}
}
}