-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
122 lines (105 loc) · 4.14 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
using Discord;
using Discord.Commands;
using Discord.Interactions;
using Discord.WebSocket;
using Lavalink4NET.DiscordNet;
using Lavalink4NET;
using Microsoft.Extensions.DependencyInjection;
using Newtonsoft.Json;
using System;
using System.Threading.Tasks;
using TidesBotDotNet.Interfaces;
using TidesBotDotNet.Services;
using Lavalink4NET.Tracking;
namespace TidesBotDotNet
{
public class Program
{
private BotDefinition botDefinition;
private GuildsDefinition guildsDefinition;
private string token;
public IServiceProvider BuildServiceProvider() => new ServiceCollection()
.AddSingleton(x => new DiscordSocketClient(new DiscordSocketConfig
{
GatewayIntents = GatewayIntents.AllUnprivileged | GatewayIntents.GuildMembers | GatewayIntents.MessageContent,
LogGatewayIntentWarnings = false,
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Debug,
MessageCacheSize = 100
}))
.AddSingleton(botDefinition)
.AddSingleton(guildsDefinition)
.AddSingleton<IAudioService, LavalinkNode>()
.AddSingleton<IDiscordClientWrapper, DiscordClientWrapper>()
.AddSingleton(new LavalinkNodeOptions {
RestUri = "http://localhost:2333/",
WebSocketUri = "ws://localhost:2333/",
Password = "youshallnotpass"
})
.AddSingleton(new InactivityTrackingOptions{
DisconnectDelay = TimeSpan.FromSeconds(10),
PollInterval = TimeSpan.FromSeconds(4),
TrackInactivity = true
})
.AddSingleton<InactivityTrackingService>()
.AddSingleton<InteractionService>()
.AddSingleton<Fergun.Interactive.InteractiveService>()
.AddSingleton<CommandHandler>()
.AddSingleton<ReactionRoleService>()
.AddSingleton<AutoRolesService>()
.AddSingleton<VxTwitterService>()
.AddSingleton<TwitchService>()
.AddSingleton<StreamRoleService>()
.AddSingleton<DJService>()
.BuildServiceProvider();
private IServiceProvider provider;
public static void Main(string[] args) => new Program().MainAsync().GetAwaiter().GetResult();
public async Task MainAsync()
{
guildsDefinition = new GuildsDefinition();
if (!LoadBotDefinition())
{
throw new Exception("No token provided.");
}
using IServiceScope serviceScope = BuildServiceProvider().CreateScope();
provider = serviceScope.ServiceProvider;
var client = provider.GetRequiredService<DiscordSocketClient>();
client.Log += Log;
client.Ready += OnReadyAsync;
await provider.GetRequiredService<CommandHandler>().InstallCommandsAsync();
await client.LoginAsync(TokenType.Bot, token);
await client.StartAsync();
await client.SetGameAsync(botDefinition.GetCurrent().status, null, ActivityType.Playing);
// Block this task until the program is closed.
await Task.Delay(-1);
}
private bool LoadBotDefinition()
{
string bd = SaveLoadService.Load("botdefinition.json");
if (bd != null)
{
botDefinition = JsonConvert.DeserializeObject<BotDefinition>(bd);
try
{
token = botDefinition.GetCurrent().token;
return true;
}
catch
{
return false;
}
}
return false;
}
private Task Log(LogMessage message)
{
Console.WriteLine(message.ToString());
return Task.CompletedTask;
}
private async Task OnReadyAsync()
{
await provider.GetRequiredService<InteractionService>().RegisterCommandsGloballyAsync(true);
await provider.GetRequiredService<IAudioService>().InitializeAsync();
}
}
}