This repository has been archived by the owner on Aug 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
153 lines (136 loc) · 7.72 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 Discord;
using Discord.Addons.Interactive;
using Discord.WebSocket;
using FezBotRedux.Common.Extensions;
using FezBotRedux.Common.Models;
using FezBotRedux.Common.Types;
using FezBotRedux.Common.Utility;
using FezBotRedux.Services;
using Microsoft.Extensions.DependencyInjection;
using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
namespace FezBotRedux {
internal class Program {
private static void Main() => new Program().Run().GetAwaiter().GetResult();
private DiscordSocketClient _client;
private IServiceProvider _services;
private Timer _playingTimer = null;
private AutoResetEvent _autoEvent = null;
private static async Task PrintInfoAsync() {
var art = new[] {
@"███████╗███████╗███████╗██████╗ ██████╗ ████████╗██████╗ ███████╗██████╗ ██╗ ██╗██╗ ██╗",
@"██╔════╝██╔════╝╚══███╔╝██╔══██╗██╔═══██╗╚══██╔══╝██╔══██╗██╔════╝██╔══██╗██║ ██║╚██╗██╔╝",
@"█████╗ █████╗ ███╔╝ ██████╔╝██║ ██║ ██║ ██████╔╝█████╗ ██║ ██║██║ ██║ ╚███╔╝ ",
@"██╔══╝ ██╔══╝ ███╔╝ ██╔══██╗██║ ██║ ██║ ██╔══██╗██╔══╝ ██║ ██║██║ ██║ ██╔██╗ ",
@"██║ ███████╗███████╗██████╔╝╚██████╔╝ ██║ ██║ ██║███████╗██████╔╝╚██████╔╝██╔╝ ██╗",
@"╚═╝ ╚══════╝╚══════╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝ ╚═╝╚══════╝╚═════╝ ╚═════╝ ╚═╝ ╚═╝",
@" "
};
foreach (var line in art)
await NeoConsole.NewLineArt(line, ConsoleColor.DarkMagenta);
await NeoConsole.Append("\n +-------------------------------------------------------------------------+", ConsoleColor.Gray);
await NeoConsole.Append("\n Source Code(Github Repo): https://github.com/EchelonDev/NeoKapcho/ ", ConsoleColor.Yellow);
await NeoConsole.Append("\n Build with love by 48 | Powered by SQLite and EFCore ", ConsoleColor.Red);
await NeoConsole.Append("\n +-------------------------------------------------------------------------+", ConsoleColor.Gray);
}
private async Task Run() {
Console.OutputEncoding = Encoding.Unicode;
await PrintInfoAsync();
using (var db = new NeoContext()) {
db.Database.EnsureCreated();
}
EnsureConfigExists();
_client = new DiscordSocketClient(new DiscordSocketConfig {
LogLevel = LogSeverity.Info,
MessageCacheSize = 5000
});
await _client.LoginAsync(TokenType.Bot, Configuration.Load().Token);
await NeoConsole.Append("Logging in...", ConsoleColor.Cyan);
await _client.StartAsync();
await NeoConsole.NewLine("Success...", ConsoleColor.Cyan);
_services = new ServiceCollection()
.AddSingleton(_client)
.AddSingleton<CommandHandler>()
.AddSingleton<InteractiveService>()
.BuildServiceProvider();
await NeoConsole.NewLine("Activating Services...\nDone.", ConsoleColor.Cyan);
await _services.GetRequiredService<CommandHandler>().Install(_client, _services);
await NeoConsole.NewLine("Command Handler starting...\nDone.\n", ConsoleColor.Cyan);
_client.Log += Log;
_client.Ready += _client_Ready;
await Task.Delay(-1);
}
private Task _client_Ready() {
Task.Run(() => {
foreach (var guild in _client.Guilds) {
using (var db = new NeoContext()) {
if (db.Guilds.Any(x => x.Id == guild.Id))
continue;
var newobj = new Guild {
Id = guild.Id,
Prefix = null
};
db.Guilds.Add(newobj);
db.SaveChanges();
Console.WriteLine($"Guild Added : {guild.Name}");
}
}
});
Task.Run(async () => {
using (var db = new NeoContext()) {
foreach (var hub in db.NeoHubSettings) {
var m = await (_client.GetChannel(hub.ChannelId) as ITextChannel)
.GetMessageAsync(hub.MsgId);
await (m as IUserMessage).ModifyAsync(x => x.Embed = NeoEmbeds.Information(_client));
}
}
});
_autoEvent = new AutoResetEvent(false);
_playingTimer = new Timer(ChangePlayingAsync, _autoEvent, 0, 1000 * 60 * 5);
Task.Run(async () => {
using (var db = new NeoContext()) {
if (!db.Playings.Any())
return;
var game = await db.Playings.AsAsyncEnumerable().OrderBy(o => Guid.NewGuid())
.FirstOrDefaultAsync();
await _client.SetGameAsync(game.Name);
}
});
return Task.CompletedTask;
}
private async void ChangePlayingAsync(object stateInfo) {
using (var db = new NeoContext()) {
if (!db.Playings.Any())
return;
var game = await db.Playings.AsAsyncEnumerable().OrderBy(o => Guid.NewGuid())
.FirstOrDefaultAsync();
await _client.SetGameAsync(game.Name);
}
}
private static Task Log(LogMessage l) {
Task.Run(async ()
=> await NeoConsole.Log(l.Severity, l.Source, l.Exception?.ToString() ?? l.Message));
return Task.CompletedTask;
}
private static async void EnsureConfigExists() {
if (!Directory.Exists(Path.Combine(AppContext.BaseDirectory, "data")))
Directory.CreateDirectory(Path.Combine(AppContext.BaseDirectory, "data"));
var loc = Path.Combine(AppContext.BaseDirectory, "data/configuration.json");
if (!File.Exists(loc) || Configuration.Load().Token == null) {
await NeoConsole.NewLine("Configuration not found...\nCreating...", ConsoleColor.Cyan);
var config = new Configuration();
await NeoConsole.NewLine("Token :", ConsoleColor.DarkCyan);
config.Token = Console.ReadLine();
await NeoConsole.NewLine("Default Prefix :", ConsoleColor.Cyan);
config.Prefix = Console.ReadLine();
config.Save();
} else {
await NeoConsole.NewLine("Configuration found...\nLoading...", ConsoleColor.Cyan);
}
}
}
}