|
@@ -1,127 +1,133 @@ |
|
|
using Newtonsoft.Json; |
|
|
|
|
|
using System; |
|
|
|
|
|
|
|
|
using System; |
|
|
|
|
|
using System.IO; |
|
|
using System.Reflection; |
|
|
using System.Reflection; |
|
|
using System.Text; |
|
|
using System.Text; |
|
|
|
|
|
|
|
|
namespace Discord |
|
|
namespace Discord |
|
|
{ |
|
|
|
|
|
public enum LogSeverity : byte |
|
|
|
|
|
{ |
|
|
|
|
|
Error = 1, |
|
|
|
|
|
Warning = 2, |
|
|
|
|
|
Info = 3, |
|
|
|
|
|
Verbose = 4, |
|
|
|
|
|
Debug = 5 |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class DiscordConfig : Config<DiscordConfig> |
|
|
|
|
|
|
|
|
{ |
|
|
|
|
|
public class DiscordConfigBuilder |
|
|
{ |
|
|
{ |
|
|
public const int MaxMessageSize = 2000; |
|
|
|
|
|
|
|
|
|
|
|
public const string LibName = "Discord.Net"; |
|
|
|
|
|
public static string LibVersion => typeof(DiscordConfig).GetTypeInfo().Assembly.GetName().Version.ToString(3); |
|
|
|
|
|
public const string LibUrl = "https://github.com/RogueException/Discord.Net"; |
|
|
|
|
|
|
|
|
|
|
|
public const string ClientAPIUrl = "https://discordapp.com/api/"; |
|
|
|
|
|
public const string StatusAPIUrl = "https://srhpyqt94yxb.statuspage.io/api/v2/"; //"https://status.discordapp.com/api/v2/"; |
|
|
|
|
|
//public const string CDNUrl = "https://cdn.discordapp.com/"; |
|
|
|
|
|
public const string InviteUrl = "https://discord.gg/"; |
|
|
|
|
|
|
|
|
|
|
|
//Global |
|
|
//Global |
|
|
|
|
|
|
|
|
/// <summary> Gets or sets name of your application, used both for the token cache directory and user agent. </summary> |
|
|
/// <summary> Gets or sets name of your application, used both for the token cache directory and user agent. </summary> |
|
|
public string AppName { get { return _appName; } set { SetValue(ref _appName, value); UpdateUserAgent(); } } |
|
|
|
|
|
private string _appName = null; |
|
|
|
|
|
|
|
|
public string AppName { get; set; } = null; |
|
|
/// <summary> Gets or sets url to your application, used in the user agent. </summary> |
|
|
/// <summary> Gets or sets url to your application, used in the user agent. </summary> |
|
|
public string AppUrl { get { return _appUrl; } set { SetValue(ref _appUrl, value); UpdateUserAgent(); } } |
|
|
|
|
|
private string _appUrl = null; |
|
|
|
|
|
|
|
|
public string AppUrl { get; set; } = null; |
|
|
/// <summary> Gets or sets the version of your application, used in the user agent. </summary> |
|
|
/// <summary> Gets or sets the version of your application, used in the user agent. </summary> |
|
|
public string AppVersion { get { return _appVersion; } set { SetValue(ref _appVersion, value); UpdateUserAgent(); } } |
|
|
|
|
|
private string _appVersion = null; |
|
|
|
|
|
|
|
|
public string AppVersion { get; set; } = null; |
|
|
|
|
|
|
|
|
/// <summary> Gets or sets the minimum log level severity that will be sent to the LogMessage event. Warning: setting this to debug will really hurt performance but should help investigate any internal issues. </summary> |
|
|
/// <summary> Gets or sets the minimum log level severity that will be sent to the LogMessage event. Warning: setting this to debug will really hurt performance but should help investigate any internal issues. </summary> |
|
|
public LogSeverity LogLevel { get { return _logLevel; } set { SetValue(ref _logLevel, value); } } |
|
|
|
|
|
private LogSeverity _logLevel = LogSeverity.Info; |
|
|
|
|
|
|
|
|
public LogSeverity LogLevel { get; set; } = LogSeverity.Info; |
|
|
/// <summary> Enables or disables the default event logger. </summary> |
|
|
/// <summary> Enables or disables the default event logger. </summary> |
|
|
public bool LogEvents { get { return _logEvents; } set { SetValue(ref _logEvents, value); } } |
|
|
|
|
|
private bool _logEvents = true; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets the user agent used when connecting to Discord. </summary> |
|
|
|
|
|
public string UserAgent { get; private set; } |
|
|
|
|
|
|
|
|
|
|
|
//Rest |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets or sets the max time (in milliseconds) to wait for an API request to complete. </summary> |
|
|
|
|
|
public int RestTimeout { get { return _restTimeout; } set { SetValue(ref _restTimeout, value); } } |
|
|
|
|
|
private int _restTimeout = 10000; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Enables or disables the internal message queue. This will allow SendMessage/EditMessage to return immediately and handle messages internally. </summary> |
|
|
|
|
|
public bool UseMessageQueue { get { return _useMessageQueue; } set { SetValue(ref _useMessageQueue, value); } } |
|
|
|
|
|
private bool _useMessageQueue = true; |
|
|
|
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait when the message queue is empty before checking again. </summary> |
|
|
|
|
|
public int MessageQueueInterval { get { return _messageQueueInterval; } set { SetValue(ref _messageQueueInterval, value); } } |
|
|
|
|
|
private int _messageQueueInterval = 100; |
|
|
|
|
|
|
|
|
public bool LogEvents { get; set; } = true; |
|
|
|
|
|
|
|
|
//WebSocket |
|
|
//WebSocket |
|
|
|
|
|
|
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait for the websocket to connect and initialize. </summary> |
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait for the websocket to connect and initialize. </summary> |
|
|
public int ConnectionTimeout { get { return _connectionTimeout; } set { SetValue(ref _connectionTimeout, value); } } |
|
|
|
|
|
private int _connectionTimeout = 30000; |
|
|
|
|
|
|
|
|
public int ConnectionTimeout { get; set; } = 30000; |
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait after an unexpected disconnect before reconnecting. </summary> |
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait after an unexpected disconnect before reconnecting. </summary> |
|
|
public int ReconnectDelay { get { return _reconnectDelay; } set { SetValue(ref _reconnectDelay, value); } } |
|
|
|
|
|
private int _reconnectDelay = 1000; |
|
|
|
|
|
|
|
|
public int ReconnectDelay { get; set; } = 1000; |
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait after an reconnect fails before retrying. </summary> |
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait after an reconnect fails before retrying. </summary> |
|
|
public int FailedReconnectDelay { get { return _failedReconnectDelay; } set { SetValue(ref _failedReconnectDelay, value); } } |
|
|
|
|
|
private int _failedReconnectDelay = 15000; |
|
|
|
|
|
|
|
|
|
|
|
/// <summary> Gets or sets the time (in milliseconds) to wait when the websocket's message queue is empty before checking again. </summary> |
|
|
|
|
|
public int WebSocketInterval { get { return _webSocketInterval; } set { SetValue(ref _webSocketInterval, value); } } |
|
|
|
|
|
private int _webSocketInterval = 100; |
|
|
|
|
|
|
|
|
public int FailedReconnectDelay { get; set; } = 15000; |
|
|
|
|
|
|
|
|
//Performance |
|
|
//Performance |
|
|
|
|
|
|
|
|
/// <summary> Cache an encrypted login token to temp dir after success login. </summary> |
|
|
|
|
|
public bool CacheToken { get { return _cacheToken; } set { SetValue(ref _cacheToken, value); } } |
|
|
|
|
|
private bool _cacheToken = true; |
|
|
|
|
|
|
|
|
/// <summary> Gets or sets whether an encrypted login token should be saved to temp dir after successful login. </summary> |
|
|
|
|
|
public bool CacheToken { get; set; } = true; |
|
|
/// <summary> Gets or sets whether Discord should send information about offline users, for servers with more than 100 users. </summary> |
|
|
/// <summary> Gets or sets whether Discord should send information about offline users, for servers with more than 100 users. </summary> |
|
|
public bool UseLargeThreshold { get { return _useLargeThreshold; } set { SetValue(ref _useLargeThreshold, value); } } |
|
|
|
|
|
private bool _useLargeThreshold = false; |
|
|
|
|
|
|
|
|
public bool UseLargeThreshold { get; set; } = false; |
|
|
/// <summary> Gets or sets the number of messages per channel that should be kept in cache. Setting this to zero disables the message cache entirely. </summary> |
|
|
/// <summary> Gets or sets the number of messages per channel that should be kept in cache. Setting this to zero disables the message cache entirely. </summary> |
|
|
public int MessageCacheSize { get { return _messageCacheSize; } set { SetValue(ref _messageCacheSize, value); } } |
|
|
|
|
|
private int _messageCacheSize = 100; |
|
|
|
|
|
|
|
|
public int MessageCacheSize { get; set; } = 100; |
|
|
/// <summary> Gets or sets whether the permissions cache should be used. This makes operations such as User.GetPermissions(Channel), User.ServerPermissions and Channel.Members </summary> |
|
|
/// <summary> Gets or sets whether the permissions cache should be used. This makes operations such as User.GetPermissions(Channel), User.ServerPermissions and Channel.Members </summary> |
|
|
public bool UsePermissionsCache { get { return _usePermissionsCache; } set { SetValue(ref _usePermissionsCache, value); } } |
|
|
|
|
|
private bool _usePermissionsCache = true; |
|
|
|
|
|
|
|
|
public bool UsePermissionsCache { get; set; } = true; |
|
|
/// <summary> Gets or sets whether the a copy of a model is generated on an update event to allow a user to check which properties changed. </summary> |
|
|
/// <summary> Gets or sets whether the a copy of a model is generated on an update event to allow a user to check which properties changed. </summary> |
|
|
public bool EnablePreUpdateEvents { get { return _enablePreUpdateEvents; } set { SetValue(ref _enablePreUpdateEvents, value); } } |
|
|
|
|
|
private bool _enablePreUpdateEvents = true; |
|
|
|
|
|
|
|
|
public bool EnablePreUpdateEvents { get; set; } = true; |
|
|
|
|
|
|
|
|
|
|
|
//Events |
|
|
|
|
|
|
|
|
public DiscordConfig() |
|
|
|
|
|
|
|
|
/// <summary> Gets or sets a handler for all log messages. </summary> |
|
|
|
|
|
public EventHandler<LogMessageEventArgs> LogHandler { get; set; } |
|
|
|
|
|
|
|
|
|
|
|
public DiscordConfig Build() => new DiscordConfig(this); |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
public class DiscordConfig |
|
|
|
|
|
{ |
|
|
|
|
|
public const int MaxMessageSize = 2000; |
|
|
|
|
|
internal const int RestTimeout = 10000; |
|
|
|
|
|
internal const int MessageQueueInterval = 100; |
|
|
|
|
|
internal const int WebSocketQueueInterval = 100; |
|
|
|
|
|
|
|
|
|
|
|
public const string LibName = "Discord.Net"; |
|
|
|
|
|
public static string LibVersion => typeof(DiscordConfigBuilder).GetTypeInfo().Assembly.GetName().Version.ToString(3); |
|
|
|
|
|
public const string LibUrl = "https://github.com/RogueException/Discord.Net"; |
|
|
|
|
|
|
|
|
|
|
|
public const string ClientAPIUrl = "https://discordapp.com/api/"; |
|
|
|
|
|
public const string StatusAPIUrl = "https://srhpyqt94yxb.statuspage.io/api/v2/"; //"https://status.discordapp.com/api/v2/"; |
|
|
|
|
|
public const string CDNUrl = "https://cdn.discordapp.com/"; |
|
|
|
|
|
public const string InviteUrl = "https://discord.gg/"; |
|
|
|
|
|
|
|
|
|
|
|
public LogSeverity LogLevel { get; } |
|
|
|
|
|
public bool LogEvents { get; } |
|
|
|
|
|
|
|
|
|
|
|
public int ConnectionTimeout { get; } |
|
|
|
|
|
public int ReconnectDelay { get; } |
|
|
|
|
|
public int FailedReconnectDelay { get; } |
|
|
|
|
|
|
|
|
|
|
|
public bool UseLargeThreshold { get; } |
|
|
|
|
|
public int MessageCacheSize { get; } |
|
|
|
|
|
public bool UsePermissionsCache { get; } |
|
|
|
|
|
public bool EnablePreUpdateEvents { get; } |
|
|
|
|
|
|
|
|
|
|
|
public string UserAgent { get; } |
|
|
|
|
|
public string CacheDir { get; } |
|
|
|
|
|
|
|
|
|
|
|
internal DiscordConfig(DiscordConfigBuilder builder) |
|
|
{ |
|
|
{ |
|
|
UpdateUserAgent(); |
|
|
|
|
|
|
|
|
LogLevel = builder.LogLevel; |
|
|
|
|
|
LogEvents = builder.LogEvents; |
|
|
|
|
|
|
|
|
|
|
|
ConnectionTimeout = builder.ConnectionTimeout; |
|
|
|
|
|
ReconnectDelay = builder.ReconnectDelay; |
|
|
|
|
|
FailedReconnectDelay = builder.FailedReconnectDelay; |
|
|
|
|
|
|
|
|
|
|
|
UseLargeThreshold = builder.UseLargeThreshold; |
|
|
|
|
|
MessageCacheSize = builder.MessageCacheSize; |
|
|
|
|
|
UsePermissionsCache = builder.UsePermissionsCache; |
|
|
|
|
|
EnablePreUpdateEvents = builder.EnablePreUpdateEvents; |
|
|
|
|
|
|
|
|
|
|
|
UserAgent = GetUserAgent(builder); |
|
|
|
|
|
CacheDir = GetCacheDir(builder); |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
private void UpdateUserAgent() |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
private string GetUserAgent(DiscordConfigBuilder builder) |
|
|
{ |
|
|
{ |
|
|
StringBuilder builder = new StringBuilder(); |
|
|
|
|
|
if (!string.IsNullOrEmpty(_appName)) |
|
|
|
|
|
|
|
|
StringBuilder sb = new StringBuilder(); |
|
|
|
|
|
if (!string.IsNullOrEmpty(builder.AppName)) |
|
|
{ |
|
|
{ |
|
|
builder.Append(_appName); |
|
|
|
|
|
if (!string.IsNullOrEmpty(_appVersion)) |
|
|
|
|
|
|
|
|
sb.Append(builder.AppName); |
|
|
|
|
|
if (!string.IsNullOrEmpty(builder.AppVersion)) |
|
|
{ |
|
|
{ |
|
|
builder.Append('/'); |
|
|
|
|
|
builder.Append(_appVersion); |
|
|
|
|
|
|
|
|
sb.Append('/'); |
|
|
|
|
|
sb.Append(builder.AppVersion); |
|
|
} |
|
|
} |
|
|
if (!string.IsNullOrEmpty(_appUrl)) |
|
|
|
|
|
|
|
|
if (!string.IsNullOrEmpty(builder.AppUrl)) |
|
|
{ |
|
|
{ |
|
|
builder.Append(" ("); |
|
|
|
|
|
builder.Append(_appUrl); |
|
|
|
|
|
builder.Append(')'); |
|
|
|
|
|
|
|
|
sb.Append(" ("); |
|
|
|
|
|
sb.Append(builder.AppUrl); |
|
|
|
|
|
sb.Append(')'); |
|
|
} |
|
|
} |
|
|
builder.Append(' '); |
|
|
|
|
|
|
|
|
sb.Append(' '); |
|
|
} |
|
|
} |
|
|
builder.Append($"DiscordBot ({LibUrl}, v{LibVersion})"); |
|
|
|
|
|
UserAgent = builder.ToString(); |
|
|
|
|
|
|
|
|
sb.Append($"DiscordBot ({LibUrl}, v{LibVersion})"); |
|
|
|
|
|
return sb.ToString(); |
|
|
|
|
|
} |
|
|
|
|
|
private string GetCacheDir(DiscordConfigBuilder builder) |
|
|
|
|
|
{ |
|
|
|
|
|
if (builder.CacheToken) |
|
|
|
|
|
return Path.Combine(Path.GetTempPath(), builder.AppName ?? "Discord.Net"); |
|
|
|
|
|
else |
|
|
|
|
|
return null; |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
} |