-
Notifications
You must be signed in to change notification settings - Fork 0
/
Program.cs
116 lines (89 loc) · 3.53 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
using System;
using System.IO;
using Discord.WebSocket;
using System.Threading.Tasks;
using Discord;
using Discord.Addons.Interactive;
using Discord.Commands;
using Warframebot.Core;
using Microsoft.Extensions.DependencyInjection;
using Warframebot.Handler;
using Warframebot.Storage;
namespace Warframebot
{
public class Program
{
static void Main(string[] args)
=> new Program().StartAsync().GetAwaiter().GetResult();
public async Task StartAsync()
{
var cmdConfig = new CommandServiceConfig
{
DefaultRunMode = RunMode.Async
};
var services = new ServiceCollection()
//.AddSingleton<DiscordSocketClient>()
.AddSingleton(new CommandService(cmdConfig))
.AddSingleton<CommandHandler>()
.AddSingleton<InteractiveService>()
.AddSingleton(new DiscordSocketClient(new DiscordSocketConfig
{
MessageCacheSize = 20,
AlwaysDownloadUsers = true,
LogLevel = LogSeverity.Verbose
}))
.BuildServiceProvider();
var client = services.GetRequiredService<DiscordSocketClient>();
client.Log += Log;
thestart:
var thetoken = Config.Bot.testtoken;
var gameInfo = "Warframe Info Bot";
if (string.IsNullOrEmpty(thetoken))
{
Console.WriteLine($"No Token found, check config. Bot will close own its own or close the window!");
await Task.Delay(6000000);
return;
}
try
{
await client.LoginAsync(TokenType.Bot, thetoken);
await client.StartAsync();
}
catch (Exception e)
{
Console.WriteLine($"Connection Failed error {e}...Will retry in 5 secs.");
for (int i = 0; i < 5; i++)
{
Console.WriteLine($"Retrying in {5 - i}");
await Task.Delay(1000);
}
goto thestart;
}
await services.GetRequiredService<CommandHandler>().InitializeAsync();
client.Ready += RepeatingTimer.StartTimer;
Global.Client = client;
await client.SetGameAsync(gameInfo);
await Task.Delay(-1);
}
private IServiceProvider ConfigureServices()
{
return new ServiceCollection()
.AddSingleton<CommandService>()
.AddSingleton<CommandHandler>()
.AddSingleton<InteractiveService>()
.BuildServiceProvider();
}
private async Task Log(LogMessage msg)
{
Console.WriteLine(msg.Message);
var fileName = $"{DateTime.Today.Day}-{DateTime.Today.Month}-{DateTime.Today.Year}.log";
var folder = Constants.LogFolder;
if (!Directory.Exists(folder))
Directory.CreateDirectory(folder);
StreamWriter sw = File.AppendText($"{folder}/{fileName}");
sw.WriteLine($"{DateTime.Today.Day}-{DateTime.Today.Month}-{DateTime.Today.Hour}-{DateTime.Today.Minute}" + msg);
sw.Close();
await Task.CompletedTask;
}
}
}