-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathListClient.cs
More file actions
154 lines (134 loc) · 5.49 KB
/
Copy pathListClient.cs
File metadata and controls
154 lines (134 loc) · 5.49 KB
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
154
using Discord.WebSocket;
using Newtonsoft.Json;
using System;
using System.Net.Http;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Timers;
namespace BotListAPI
{
/// <summary> BotListAPI client to post server count </summary>
/// <remarks>
/// Docs: <see href="https://docs.fluxpoint.dev/botlistapi/"/>
/// </remarks>
public class ListClient
{
public readonly ListConfig Config = new ListConfig();
/// <summary> Discord client can be normal or sharded </summary>
public readonly BaseSocketClient Discord;
private readonly Timer Timer;
/// <summary> Log event for when a log message is triggered </summary>
public Action<LogType, string> MessageLog;
private HttpClient Http;
/// <summary> Log type for auto posting </summary>
public LogType LogType = LogType.Info;
public ListClient(BaseSocketClient client, ListConfig config)
{
if (config == null)
throw new ArgumentException("The list config cannot be null");
Discord = client;
Config = config;
if (Config.DiscordBotListv2 != "" && !Config.DiscordBotListv2.StartsWith("Bot "))
Config.DiscordBotListv2 = "Bot " + Config.DiscordBotListv2;
Timer = new Timer
{
Interval = 600000,
Enabled = false
};
Timer.Elapsed += Timer_Elapsed;
}
/// <summary> Disable all auto posting </summary>
public bool Disabled = false;
/// <summary> Start posting server count every 10 minutes </summary>
public void Start()
{
if (!Timer.Enabled)
{
Timer.Start();
Log(LogType.None, LogType.None, "Enabled auto posting server count");
}
}
/// <summary> Stop auto posting server count </summary>
public void Stop()
{
if (Timer.Enabled)
{
Timer.Stop();
Log(LogType.None, LogType.None, "Disabled auto posting server count");
}
}
/// <summary> Post server count every 10 minutes </summary>
private void Timer_Elapsed(object sender, ElapsedEventArgs e)
{
if (Discord != null && !Disabled)
_ = SendBotBlock(LogType);
}
public readonly string Version = "5.5.3";
/// <summary> Post server count to BotBlock.org </summary>
public async Task SendBotBlock(LogType type)
{
bool isError = false;
if (Http == null)
{
if (Discord == null)
{
Log(type, LogType.Error, "Cannot post server count, Discord client is null");
isError = true;
}
else if (Discord.CurrentUser == null)
{
Log(type, LogType.Error, "Cannot post server count, CurrentUser is null");
isError = true;
}
Http = new HttpClient();
Http.DefaultRequestHeaders.Accept.Add(new MediaTypeWithQualityHeaderValue("application/json"));
Http.DefaultRequestHeaders.Add("User-Agent", $"BotListAPI {Version} - {Discord.CurrentUser.Username}#{Discord.CurrentUser.Discriminator}");
}
if (!isError)
{
ListJson Json = Config.GetJson();
Json.SetCount(Discord);
string JsonString = JsonConvert.SerializeObject(Json);
try
{
StringContent Content = new StringContent(JsonString, Encoding.UTF8, "application/json");
Content.Headers.ContentType = new MediaTypeHeaderValue("application/json");
HttpResponseMessage Res = await Http.PostAsync("https://botblock.org/api/count", Content);
if (Res.IsSuccessStatusCode)
{
Log(type, LogType.Info, $"Successfully posted server count to BotBlock");
Log(type, LogType.Debug, "Request response in JSON\n" + JsonConvert.SerializeObject(Res, Formatting.Indented));
}
else
{
Log(type, LogType.Error, $"Error could not post server count to BotBlock, {(int)Res.StatusCode} {Res.ReasonPhrase}");
Log(type, LogType.Debug, "Request response in JSON\n" + JsonConvert.SerializeObject(Res, Formatting.Indented));
}
}
catch (Exception ex)
{
Log(type, LogType.Error, $"Error could not post server count to BotBlock, {ex.Message}");
Log(type, LogType.Debug, "Exception\n" + ex.ToString());
}
}
}
internal void Log(LogType type, LogType Match, string text)
{
MessageLog?.Invoke(type, text);
if (type >= Match)
Console.WriteLine("[BotListAPI] " + text);
}
}
public enum LogType
{
/// <summary> Dont log anything to console </summary>
None,
/// <summary> Only log errors </summary>
Error,
/// <summary> Log success and errors </summary>
Info,
/// <summary> Log everything including request responses in json to console </summary>
Debug
}
}