forked from Marilyth/MopsBot-2.0
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathProgram.cs
executable file
·113 lines (97 loc) · 4.19 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
using System;
using System.Runtime.CompilerServices;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Threading.Tasks;
using Microsoft.AspNetCore;
using Microsoft.AspNetCore.Hosting;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.Logging;
using Microsoft.Extensions.DependencyInjection;
using Discord;
using Discord.WebSocket;
using Discord.Commands;
using Discord.Rest;
using System.Threading;
using System.Net;
using Newtonsoft.Json;
using Discord.Addons.Interactive;
namespace MopsBot
{
public class Program
{
public static void Main(string[] args)
{
Task.Run(() => BuildWebHost(args).Run());
new Program().Start().GetAwaiter().GetResult();
}
public static DiscordSocketClient Client;
public static DiscordRestClient RestClient;
public static Dictionary<string, string> Config;
public static CommandHandler Handler { get; private set; }
public static ReactionHandler ReactionHandler { get; private set; }
private async Task Start()
{
Client = new DiscordSocketClient(new DiscordSocketConfig()
{
LogLevel = LogSeverity.Info,
//AlwaysDownloadUsers = true
});
RestClient = new DiscordRestClient();
using (StreamReader sr = new StreamReader(new FileStream("mopsdata//Config.json", FileMode.Open)))
Config = JsonConvert.DeserializeObject<Dictionary<string, string>>(sr.ReadToEnd());
await Client.LoginAsync(TokenType.Bot, Config["Discord"]);
await Client.StartAsync();
await RestClient.LoginAsync(TokenType.Bot, Config["Discord"]);
Client.Log += ClientLog;
Client.Ready += onClientReady;
var map = new ServiceCollection().AddSingleton(Client)
// .AddSingleton(new AudioService())
.AddSingleton(new ReliabilityService(Client, ClientLog))
.AddSingleton(new InteractiveService(Client));
var provider = map.BuildServiceProvider();
Handler = new CommandHandler();
await Handler.Install(provider);
ReactionHandler = new ReactionHandler();
ReactionHandler.Install(provider);
await Task.Delay(-1);
}
public static async Task ClientLog(LogMessage msg)
{
await MopsLog(msg, "", msg.Source, -1);
}
public static async Task MopsLog(LogMessage msg, [CallerMemberName] string callerName = "", [CallerFilePath] string callerPath = "", [CallerLineNumber] int callerLine = 0)
{
string message = $"\n[{msg.Severity}] at {DateTime.Now}\nsource: {Path.GetFileNameWithoutExtension(callerPath)}.{callerName}, line: {callerLine}\nmessage: {msg.Message}";
if(msg.Exception != null && !msg.Exception.Message.Contains("The SSL connection could not be established")){
message += $"\nException: {msg.Exception?.Message ?? ""}\nStacktrace: {msg.Exception?.StackTrace ?? ""}";
}
Console.WriteLine(message);
}
private Task onClientReady()
{
Task.Run(() => {
StaticBase.UpdateStatusAsync();
StaticBase.initTracking();
});
return Task.CompletedTask;
}
public static IWebHost BuildWebHost(string[] args) =>
WebHost.CreateDefaultBuilder(args)
.UseUrls("http://0.0.0.0:5000/")
.ConfigureServices(x => x.AddCors(options => options.AddPolicy("AllowAll",
builder =>
{
builder.AllowAnyOrigin()
.AllowAnyHeader()
.AllowAnyMethod()
.AllowCredentials();
})))
.UseKestrel()
.UseContentRoot(Directory.GetCurrentDirectory())
.UseIISIntegration()
.UseStartup<Startup>()
.Build();
}
}