├── .gitignore ├── DiscordBotsList.Api ├── Internal │ ├── HasVotedObject.cs │ ├── BotReviews.cs │ ├── Entity.cs │ ├── User.cs │ ├── Queries │ │ └── BotListQuery.cs │ ├── SelfBot.cs │ ├── GuildCountObject.cs │ └── Bot.cs ├── Objects │ ├── WeekendObject.cs │ ├── IBotStats.cs │ ├── IAdapter.cs │ ├── IDblUser.cs │ ├── SocialConnections.cs │ ├── IDblEntity.cs │ ├── ISearchResult.cs │ ├── ApiRequest.cs │ ├── IDblBot.cs │ └── SmallWidgetOptions.cs ├── Serialization │ └── ULongToStringConverter.cs ├── DiscordBotsList.Api.csproj ├── DiscordBotListApi.cs └── AuthenticatedBotListApi.cs ├── DiscordBotsList.Api.Tests ├── .runsettings.template ├── DiscordBotsList.Api.Tests.csproj └── UnitTests.cs ├── LICENSE ├── DiscordBotsList.Api.Adapter.Discord.Net ├── Utils │ └── DiscordNetDblUtils.cs ├── DiscordBotsList.Api.Adapter.Discord.Net.csproj ├── SubmissionAdapter.cs └── DiscordNetDiscordBotsListApi.cs ├── README.md └── DiscordBotsList.Api.sln /.gitignore: -------------------------------------------------------------------------------- 1 | *.user 2 | *.json 3 | */.vs 4 | .vs/ 5 | */obj 6 | */bin 7 | .runsettings 8 | */.idea -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/HasVotedObject.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace DiscordBotsList.Api.Internal 4 | { 5 | internal class HasVotedObject 6 | { 7 | [JsonPropertyName("voted")] public int? HasVoted { get; set; } 8 | } 9 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/WeekendObject.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace DiscordBotsList.Api.Objects 4 | { 5 | public class WeekendObject 6 | { 7 | [JsonPropertyName("is_weekend")] 8 | public bool Weekend { get; set; } 9 | } 10 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/IBotStats.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DiscordBotsList.Api.Objects 4 | { 5 | public interface IDblBotStats 6 | { 7 | int GuildCount { get; } 8 | 9 | IReadOnlyList Shards { get; } 10 | 11 | int ShardCount { get; } 12 | } 13 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/IAdapter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | 4 | namespace DiscordBotsList.Api.Objects 5 | { 6 | public interface IAdapter 7 | { 8 | event Action Log; 9 | 10 | Task RunAsync(); 11 | 12 | void Start(); 13 | 14 | void Stop(); 15 | } 16 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/BotReviews.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace DiscordBotsList.Api.Internal 4 | { 5 | public class BotReviews 6 | { 7 | [JsonPropertyName("averageScore")] 8 | public double AverageScore { get; internal set; } 9 | 10 | [JsonPropertyName("count")] 11 | public int Count { get; internal set; } 12 | } 13 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api.Tests/.runsettings.template: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | 5 | 6 | 7 | MY_API_KEY 8 | 123456 9 | 10 | 11 | -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/IDblUser.cs: -------------------------------------------------------------------------------- 1 | namespace DiscordBotsList.Api.Objects 2 | { 3 | public interface IDblUser : IDblEntity 4 | { 5 | string Biography { get; } 6 | 7 | string BannerUrl { get; } 8 | 9 | SocialConnections Connections { get; } 10 | 11 | string Color { get; } 12 | 13 | bool IsSupporter { get; } 14 | 15 | bool IsCertified { get; } 16 | 17 | bool IsModerator { get; } 18 | 19 | bool IsWebModerator { get; } 20 | 21 | bool IsAdmin { get; } 22 | } 23 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/SocialConnections.cs: -------------------------------------------------------------------------------- 1 | using System.Text.Json.Serialization; 2 | 3 | namespace DiscordBotsList.Api.Objects 4 | { 5 | public class SocialConnections 6 | { 7 | [JsonPropertyName("youtube")] public string YouTubeChannelId { get; internal set; } 8 | 9 | [JsonPropertyName("reddit")] public string RedditName { get; internal set; } 10 | 11 | [JsonPropertyName("twitter")] public string TwitterName { get; internal set; } 12 | 13 | [JsonPropertyName("instagram")] public string InstagramName { get; internal set; } 14 | 15 | [JsonPropertyName("github")] public string GitHubName { get; internal set; } 16 | } 17 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/IDblEntity.cs: -------------------------------------------------------------------------------- 1 | namespace DiscordBotsList.Api.Objects 2 | { 3 | public interface IDblEntity 4 | { 5 | /// 6 | /// Id 7 | /// 8 | ulong Id { get; } 9 | 10 | /// 11 | /// Username 12 | /// 13 | string Username { get; } 14 | 15 | /// 16 | /// Discriminator, the XXXX#1234 part 17 | /// 18 | string Discriminator { get; } 19 | 20 | /// 21 | /// Avatar url, or default avatar if none found. 22 | /// 23 | string AvatarUrl { get; } 24 | } 25 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/Entity.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace DiscordBotsList.Api.Internal 5 | { 6 | public class Entity : IDblEntity 7 | { 8 | [JsonPropertyName("avatar")] public string Avatar { get; set; } 9 | 10 | [JsonPropertyName("defAvatar")] public string DefaultAvatar { get; set; } 11 | 12 | public string AvatarUrl => Avatar; 13 | 14 | [JsonPropertyName("id")] 15 | [JsonConverter(typeof(ULongToStringConverter))] 16 | public ulong Id { get; set; } 17 | 18 | [JsonPropertyName("username")] public string Username { get; set; } 19 | 20 | [JsonPropertyName("discriminator")] public string Discriminator { get; set; } 21 | 22 | public override string ToString() 23 | { 24 | return $"{Username}#{Discriminator}"; 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/ISearchResult.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DiscordBotsList.Api.Objects 4 | { 5 | public interface ISearchResult 6 | { 7 | /// 8 | /// Items returned from search 9 | /// 10 | List Items { get; } 11 | 12 | /// 13 | /// The current page you've navigated 14 | /// 15 | int CurrentPage { get; } 16 | 17 | /// 18 | /// Set items per page 19 | /// 20 | int ItemsPerPage { get; } 21 | 22 | /// 23 | /// Total amount of items found in the search 24 | /// 25 | int TotalItems { get; } 26 | 27 | /// 28 | /// Total amount of pages found in the search 29 | /// 30 | int TotalPages { get; } 31 | } 32 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Serialization/ULongToStringConverter.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Text.Json; 3 | using System.Text.Json.Serialization; 4 | 5 | namespace DiscordBotsList.Api.Internal 6 | { 7 | /// 8 | /// Converts API responses from strings to longs and vice versa. 9 | /// 10 | internal class ULongToStringConverter : JsonConverter 11 | { 12 | public override ulong Read(ref Utf8JsonReader reader, Type typeToConvert, JsonSerializerOptions options) 13 | { 14 | if (reader.TokenType == JsonTokenType.String && ulong.TryParse(reader.GetString(), out var value)) 15 | { 16 | return value; 17 | } 18 | 19 | throw new InvalidOperationException(); 20 | } 21 | 22 | public override void Write(Utf8JsonWriter writer, ulong value, JsonSerializerOptions options) 23 | { 24 | writer.WriteStringValue(value.ToString()); 25 | } 26 | } 27 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/User.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System.Text.Json.Serialization; 3 | 4 | namespace DiscordBotsList.Api.Internal 5 | { 6 | public class User : Entity, IDblUser 7 | { 8 | [JsonPropertyName("social")] public SocialConnections Social { get; set; } 9 | 10 | [JsonPropertyName("bio")] public string Biography { get; set; } 11 | 12 | [JsonPropertyName("banner")] public string BannerUrl { get; set; } 13 | 14 | [JsonPropertyName("color")] public string Color { get; set; } 15 | 16 | [JsonPropertyName("supporter")] public bool IsSupporter { get; set; } 17 | 18 | [JsonPropertyName("certifiedDev")] public bool IsCertified { get; set; } 19 | 20 | [JsonPropertyName("mod")] public bool IsModerator { get; set; } 21 | 22 | [JsonPropertyName("webMod")] public bool IsWebModerator { get; set; } 23 | 24 | [JsonPropertyName("admin")] public bool IsAdmin { get; set; } 25 | 26 | public SocialConnections Connections => Social; 27 | } 28 | } -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2019-2025 Discord Bots 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/Queries/BotListQuery.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Linq; 5 | using System.Text.Json.Serialization; 6 | 7 | namespace DiscordBotsList.Api.Internal.Queries 8 | { 9 | internal class BotListQuery : ISearchResult 10 | { 11 | [JsonPropertyName("results")] public List results { get; set; } 12 | 13 | [JsonPropertyName("limit")] public int limit { get; set; } 14 | 15 | [JsonPropertyName("offset")] public int? offset { get; set; } 16 | 17 | [JsonPropertyName("count")] public int count { get; set; } 18 | 19 | [JsonPropertyName("total")] public int total { get; set; } 20 | 21 | public List Items => results 22 | .Cast() 23 | .ToList(); 24 | 25 | public int CurrentPage => (int)Math.Ceiling((double)(offset ?? 0) / limit); 26 | 27 | public int ItemsPerPage => limit; 28 | 29 | public int TotalItems => total; 30 | 31 | public int TotalPages => (int)Math.Ceiling((double)limit / count); 32 | } 33 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api.Tests/DiscordBotsList.Api.Tests.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | 8.0.0 6 | 7 | false 8 | 9 | 10 | 11 | 12 | 13 | 14 | 15 | 16 | 17 | 18 | 19 | all 20 | runtime; build; native; contentfiles; analyzers; buildtransitive 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | -------------------------------------------------------------------------------- /DiscordBotsList.Api.Adapter.Discord.Net/Utils/DiscordNetDblUtils.cs: -------------------------------------------------------------------------------- 1 | using Discord.WebSocket; 2 | 3 | namespace DiscordBotsList.Api.Adapter.Discord.Net.Utils 4 | { 5 | public static class DiscordNetDblUtils 6 | { 7 | /// 8 | /// Creates a DiscordBotsList Api 9 | /// 10 | /// your client 11 | /// Your DiscordBotsList token 12 | /// A new instance of a DblApi 13 | public static DiscordNetDblApi CreateDblApi(this DiscordSocketClient client, string dblToken) 14 | { 15 | return new DiscordNetDblApi(client, dblToken); 16 | } 17 | 18 | /// 19 | /// Creates a DiscordBotsList Api 20 | /// 21 | /// your client 22 | /// Your DiscordBotsList token 23 | /// A new instance of a DblApi 24 | public static ShardedDiscordNetDblApi CreateDblApi(this DiscordShardedClient client, string dblToken) 25 | { 26 | return new ShardedDiscordNetDblApi(client, dblToken); 27 | } 28 | } 29 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/ApiRequest.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Net; 3 | 4 | namespace DiscordBotsList.Api.Objects 5 | { 6 | public class ApiResult 7 | { 8 | private ApiResult() 9 | { 10 | } 11 | 12 | public T Value { get; private set; } 13 | 14 | /// 15 | /// The error reason for this API request, if any. 16 | /// 17 | public string ErrorReason { get; private set; } 18 | 19 | public bool IsSuccess { get; private set; } 20 | 21 | internal static ApiResult FromSuccess(T value) 22 | { 23 | return new ApiResult { Value = value, IsSuccess = true }; 24 | } 25 | 26 | internal static ApiResult FromError(Exception ex) 27 | { 28 | return new ApiResult { Value = default, ErrorReason = ex.Message, IsSuccess = false }; 29 | } 30 | 31 | internal static ApiResult 32 | FromHttpError( 33 | HttpStatusCode statusCode) // This could be altered to collect an object that provides more information 34 | { 35 | return new ApiResult { Value = default, ErrorReason = statusCode.ToString(), IsSuccess = false }; 36 | } 37 | } 38 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/SelfBot.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | 5 | namespace DiscordBotsList.Api.Internal 6 | { 7 | internal class SelfBot : Bot, IDblSelfBot 8 | { 9 | public async Task> GetVotersAsync(int page = 1) 10 | { 11 | return await ((AuthDiscordBotListApi)api).GetVotersAsync(page); 12 | } 13 | 14 | public async Task HasVotedAsync(ulong userId) 15 | { 16 | return await ((AuthDiscordBotListApi)api).HasVoted(userId); 17 | } 18 | 19 | public async Task IsWeekendAsync() 20 | { 21 | return await ((AuthDiscordBotListApi)api).IsWeekendAsync(); 22 | } 23 | 24 | public async Task UpdateStatsAsync(int guildCount) 25 | { 26 | await ((AuthDiscordBotListApi)api).UpdateStats(guildCount); 27 | } 28 | 29 | public async Task UpdateStatsAsync(int[] shards) 30 | { 31 | await ((AuthDiscordBotListApi)api).UpdateStats(0, shards.Length, shards); 32 | } 33 | 34 | public async Task UpdateStatsAsync(int shardCount, int totalShards, params int[] shards) 35 | { 36 | await ((AuthDiscordBotListApi)api).UpdateStats(shardCount, totalShards, shards); 37 | } 38 | } 39 | } -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DBL-dotnet-Library 2 | top.gg botlist wrapper 3 | 4 | ## Usage 5 | ### Unauthorized api usage 6 | #### Setting up 7 | ```cs 8 | DiscordBotListApi DblApi = new DiscordBotListApi(); 9 | ``` 10 | 11 | #### Getting bots 12 | ```cs 13 | // discord id 14 | IBot bot = DblApi.GetBotAsync(160105994217586689); 15 | ``` 16 | 17 | #### Getting users 18 | ```cs 19 | // discord id 20 | IUser bot = DblApi.GetUserAsync(121919449996460033); 21 | ``` 22 | 23 | ### Authorized api usage 24 | #### Setting up 25 | ```cs 26 | AuthDiscordBotListApi DblApi = new AuthDiscordBotListApi(BOT_DISCORD_ID, YOUR_TOKEN); 27 | ``` 28 | 29 | #### Updating stats 30 | ```cs 31 | IDblSelfBot me = await DblApi.GetMeAsync(); 32 | // Update stats sharded indexShard shardCount shards 33 | await me.UpdateStatsAsync(24, 50, new[] { 12, 421, 62, 241, 524, 534 }); 34 | 35 | // Update stats guildCount 36 | await me.UpdateStatsAsync(2133); 37 | ``` 38 | 39 | #### Widgets 40 | ```cs 41 | string widgetUrl = new SmallWidgetOptions() 42 | .SetType(WidgetType.OWNER) 43 | .SetLeftColor(255, 255, 255); 44 | .Build(160105994217586689); 45 | ``` 46 | 47 | Generates ![](https://top.gg/api/widget/status/160105994217586689.svg?leftcolor=FFFFFF) 48 | 49 | ### Download 50 | #### Nuget 51 | If you're using Nuget you can use find it with the ID `DiscordBotsList.Api` or use 52 | > Install-Package DiscordBotsList.Api 53 | -------------------------------------------------------------------------------- /DiscordBotsList.Api.Adapter.Discord.Net/DiscordBotsList.Api.Adapter.Discord.Net.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | Velddev, Faith 6 | Top.gg 7 | DiscordBotsList.Api.Adapter.Discord.Net 8 | Top.gg API adapter for Discord.Net 9 | Initial release 10 | true 11 | Mike Veldsink 12 | discord bots topgg api discord.net 13 | git 14 | https://github.com/Top-gg-Community/dotnet-sdk 15 | https://github.com/Top-gg-Community/dotnet-sdk 16 | 1.6.0 17 | LICENSE 18 | 19 | 20 | 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | 30 | True 31 | 32 | 33 | 34 | 35 | 36 | -------------------------------------------------------------------------------- /DiscordBotsList.Api/DiscordBotsList.Api.csproj: -------------------------------------------------------------------------------- 1 | 2 | 3 | 4 | net8.0 5 | Velddev, Faith 6 | Top.gg 7 | top.gg api wrapper 8 | Mike Veldsink 9 | https://github.com/DiscordBotList/DBL-dotnet-Library 10 | https://github.com/DiscordBotList/DBL-dotnet-Library 11 | git 12 | discord bots list org wrapper api 13 | update to net 8 14 | false 15 | true 16 | DiscordBotsList.Api 17 | DiscordBotsList.Api 18 | DiscordBotsList.Api 19 | 1.6.0 20 | LICENSE 21 | 22 | 23 | 24 | 25 | 26 | 27 | 28 | 29 | True 30 | 31 | 32 | 33 | 34 | 35 | -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/GuildCountObject.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System.Collections.Generic; 3 | using System.Linq; 4 | using System.Text.Json.Serialization; 5 | 6 | namespace DiscordBotsList.Api.Internal 7 | { 8 | internal class GuildCountObject 9 | { 10 | [JsonPropertyName("server_count")] internal int guildCount; 11 | 12 | public GuildCountObject(int count) 13 | { 14 | guildCount = count; 15 | } 16 | } 17 | 18 | internal class BotStatsObject : ShardedObject, IDblBotStats 19 | { 20 | [JsonPropertyName("server_count")] internal int guildCount { get; set; } 21 | public int GuildCount => guildCount; 22 | 23 | public IReadOnlyList Shards => shards.ToList(); 24 | 25 | public int ShardCount => shardCount; 26 | } 27 | 28 | internal class ShardedObject 29 | { 30 | [JsonPropertyName("shards")] internal int[] shards { get; set; } 31 | 32 | [JsonPropertyName("shard_count")] internal int shardCount { get; set; } 33 | } 34 | 35 | internal class ShardedGuildCountObject 36 | { 37 | [JsonPropertyName("shards")] public int[] Shards { get; set; } 38 | 39 | [JsonPropertyName("shard_id")] public int ShardId { get; set; } 40 | 41 | [JsonPropertyName("shard_count")] public int ShardCount { get; set; } 42 | 43 | [JsonPropertyName("server_count")] public int GuildCount { get; set; } 44 | } 45 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/IDblBot.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Collections.Generic; 3 | using System.Threading.Tasks; 4 | using DiscordBotsList.Api.Internal; 5 | 6 | namespace DiscordBotsList.Api.Objects 7 | { 8 | public interface IDblBot : IDblEntity 9 | { 10 | ulong ClientId { get; } 11 | 12 | string VanityTag { get; } 13 | 14 | string PrefixUsed { get; } 15 | 16 | string ShortDescription { get; } 17 | 18 | string LongDescription { get; } 19 | 20 | List Tags { get; } 21 | 22 | string WebsiteUrl { get; } 23 | 24 | string SupportUrl { get; } 25 | 26 | string GithubUrl { get; } 27 | 28 | List OwnerIds { get; } 29 | 30 | string InviteUrl { get; } 31 | 32 | DateTime SubmittedAt { get; } 33 | 34 | bool IsCertified { get; } 35 | 36 | string VanityUrl { get; } 37 | 38 | int Points { get; } 39 | 40 | int MonthlyPoints { get; } 41 | 42 | BotReviews Reviews { get; } 43 | 44 | Task GetStatsAsync(); 45 | } 46 | 47 | public interface IDblSelfBot : IDblBot 48 | { 49 | Task> GetVotersAsync(int page = 1); 50 | 51 | Task HasVotedAsync(ulong userId); 52 | 53 | Task IsWeekendAsync(); 54 | 55 | Task UpdateStatsAsync(int guildCount); 56 | 57 | Task UpdateStatsAsync(int[] shards); 58 | 59 | Task UpdateStatsAsync(int shardCount, int totalShards, params int[] shards); 60 | } 61 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api.Tests/UnitTests.cs: -------------------------------------------------------------------------------- 1 | using System; 2 | using System.Threading.Tasks; 3 | using Xunit; 4 | 5 | namespace DiscordBotsList.Api.Tests 6 | { 7 | public class Credentials 8 | { 9 | public ulong BotId { get; set; } 10 | public string Token { get; set; } 11 | 12 | public static Credentials LoadFromEnv() 13 | { 14 | return new Credentials() 15 | { 16 | BotId = ulong.Parse(Environment.GetEnvironmentVariable("BOT_ID")), 17 | Token = Environment.GetEnvironmentVariable("API_KEY") 18 | }; 19 | } 20 | } 21 | 22 | public class UnitTests 23 | { 24 | private readonly AuthDiscordBotListApi _api; 25 | private readonly Credentials _cred; 26 | 27 | public UnitTests() 28 | { 29 | _cred = Credentials.LoadFromEnv(); 30 | _api = new AuthDiscordBotListApi(_cred.BotId, _cred.Token); 31 | } 32 | 33 | [Fact] 34 | public async Task HasVotedTestAsync() 35 | { 36 | Assert.False(await _api.HasVoted(0)); 37 | } 38 | 39 | [Fact] 40 | public async Task TaskIsWeekendTestAsync() 41 | { 42 | await _api.IsWeekendAsync(); 43 | } 44 | 45 | [Fact] 46 | public async Task TaskGetVotersTestAsync() 47 | { 48 | Assert.NotNull(await _api.GetVotersAsync()); 49 | } 50 | 51 | [Fact] 52 | public async Task GetBotTestAsync() 53 | { 54 | var botId = 1026525568344264724U; 55 | var bot = await _api.GetBotAsync(botId); 56 | Assert.NotNull(bot); 57 | Assert.Equal(botId, bot.Id); 58 | } 59 | 60 | [Fact] 61 | public async Task GetMeTestAsync() 62 | { 63 | Assert.NotNull(await _api.GetMeAsync()); 64 | } 65 | 66 | [Fact] 67 | public async Task GetUsersGetStatsTest() 68 | { 69 | var bots = await _api.GetBotsAsync(); 70 | 71 | Assert.NotNull(bots); 72 | Assert.NotEmpty(bots.Items); 73 | } 74 | } 75 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api.sln: -------------------------------------------------------------------------------- 1 | 2 | Microsoft Visual Studio Solution File, Format Version 12.00 3 | # Visual Studio 15 4 | VisualStudioVersion = 15.0.27130.2020 5 | MinimumVisualStudioVersion = 10.0.40219.1 6 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotsList.Api", "DiscordBotsList.Api\DiscordBotsList.Api.csproj", "{B20E0634-86B7-4392-A40D-93B368A68158}" 7 | EndProject 8 | Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "DiscordBotsList.Api.Tests", "DiscordBotsList.Api.Tests\DiscordBotsList.Api.Tests.csproj", "{4A37AC0E-B9C5-49DB-B8C2-D701A37CA771}" 9 | EndProject 10 | Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "DiscordBotsList.Api.Adapter.Discord.Net", "DiscordBotsList.Api.Adapter.Discord.Net\DiscordBotsList.Api.Adapter.Discord.Net.csproj", "{0E4A5566-863E-402A-9359-49FC7900D4E8}" 11 | EndProject 12 | Global 13 | GlobalSection(SolutionConfigurationPlatforms) = preSolution 14 | Debug|Any CPU = Debug|Any CPU 15 | Release|Any CPU = Release|Any CPU 16 | EndGlobalSection 17 | GlobalSection(ProjectConfigurationPlatforms) = postSolution 18 | {B20E0634-86B7-4392-A40D-93B368A68158}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 19 | {B20E0634-86B7-4392-A40D-93B368A68158}.Debug|Any CPU.Build.0 = Debug|Any CPU 20 | {B20E0634-86B7-4392-A40D-93B368A68158}.Release|Any CPU.ActiveCfg = Release|Any CPU 21 | {B20E0634-86B7-4392-A40D-93B368A68158}.Release|Any CPU.Build.0 = Release|Any CPU 22 | {4A37AC0E-B9C5-49DB-B8C2-D701A37CA771}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 23 | {4A37AC0E-B9C5-49DB-B8C2-D701A37CA771}.Debug|Any CPU.Build.0 = Debug|Any CPU 24 | {4A37AC0E-B9C5-49DB-B8C2-D701A37CA771}.Release|Any CPU.ActiveCfg = Release|Any CPU 25 | {4A37AC0E-B9C5-49DB-B8C2-D701A37CA771}.Release|Any CPU.Build.0 = Release|Any CPU 26 | {0E4A5566-863E-402A-9359-49FC7900D4E8}.Debug|Any CPU.ActiveCfg = Debug|Any CPU 27 | {0E4A5566-863E-402A-9359-49FC7900D4E8}.Debug|Any CPU.Build.0 = Debug|Any CPU 28 | {0E4A5566-863E-402A-9359-49FC7900D4E8}.Release|Any CPU.ActiveCfg = Release|Any CPU 29 | {0E4A5566-863E-402A-9359-49FC7900D4E8}.Release|Any CPU.Build.0 = Release|Any CPU 30 | EndGlobalSection 31 | GlobalSection(SolutionProperties) = preSolution 32 | HideSolutionNode = FALSE 33 | EndGlobalSection 34 | GlobalSection(ExtensibilityGlobals) = postSolution 35 | SolutionGuid = {B3317C2A-4530-4F3A-9A45-8D8DA228E8B5} 36 | EndGlobalSection 37 | EndGlobal 38 | -------------------------------------------------------------------------------- /DiscordBotsList.Api.Adapter.Discord.Net/SubmissionAdapter.cs: -------------------------------------------------------------------------------- 1 | using Discord; 2 | using Discord.WebSocket; 3 | using DiscordBotsList.Api.Objects; 4 | using System; 5 | using System.Linq; 6 | using System.Threading.Tasks; 7 | 8 | namespace DiscordBotsList.Api.Adapter.Discord.Net 9 | { 10 | internal class SubmissionAdapter : IAdapter 11 | { 12 | protected AuthDiscordBotListApi api; 13 | protected IDiscordClient client; 14 | 15 | protected DateTime lastTimeUpdated; 16 | protected TimeSpan updateTime; 17 | 18 | public SubmissionAdapter(AuthDiscordBotListApi api, IDiscordClient client, TimeSpan updateTime) 19 | { 20 | this.client = client; 21 | this.updateTime = updateTime; 22 | } 23 | 24 | public event Action Log; 25 | 26 | public virtual async Task RunAsync() 27 | { 28 | if (DateTime.Now > lastTimeUpdated + updateTime) 29 | { 30 | await api.UpdateStats( 31 | (await client.GetGuildsAsync()).Count 32 | ); 33 | 34 | lastTimeUpdated = DateTime.Now; 35 | SendLog("Submitted stats to Top.gg!"); 36 | } 37 | } 38 | 39 | public virtual void Start() 40 | { 41 | } 42 | 43 | public virtual void Stop() 44 | { 45 | throw new NotImplementedException(); 46 | } 47 | 48 | protected void SendLog(string msg) 49 | { 50 | Log?.Invoke(msg); 51 | } 52 | } 53 | 54 | internal class ShardedSubmissionAdapter : SubmissionAdapter, IAdapter 55 | { 56 | public ShardedSubmissionAdapter(AuthDiscordBotListApi api, DiscordShardedClient client, TimeSpan updateTime) 57 | : base(api, client, updateTime) 58 | { 59 | } 60 | 61 | public override async Task RunAsync() 62 | { 63 | if (DateTime.Now > lastTimeUpdated + updateTime) 64 | { 65 | await api.UpdateStats( 66 | 0, 67 | (client as DiscordShardedClient).Shards.Count, 68 | (client as DiscordShardedClient).Shards.Select(x => x.Guilds.Count).ToArray() 69 | ); 70 | 71 | lastTimeUpdated = DateTime.Now; 72 | SendLog("Sent stats to Top.gg!"); 73 | } 74 | } 75 | } 76 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api.Adapter.Discord.Net/DiscordNetDiscordBotsListApi.cs: -------------------------------------------------------------------------------- 1 | using Discord; 2 | using Discord.WebSocket; 3 | using DiscordBotsList.Api.Objects; 4 | using System; 5 | using System.Threading.Tasks; 6 | 7 | namespace DiscordBotsList.Api.Adapter.Discord.Net 8 | { 9 | public static class DiscordNetDblUtils 10 | { 11 | public static DiscordNetDblApi CreateDblApi(this DiscordSocketClient client, string dblToken) 12 | { 13 | return new DiscordNetDblApi(client, dblToken); 14 | } 15 | 16 | public static ShardedDiscordNetDblApi CreateDblApi(this DiscordShardedClient client, string dblToken) 17 | { 18 | return new ShardedDiscordNetDblApi(client, dblToken); 19 | } 20 | } 21 | 22 | public class DiscordNetDblApi : AuthDiscordBotListApi 23 | { 24 | protected IDiscordClient client; 25 | 26 | public DiscordNetDblApi(IDiscordClient client, string dblToken) : base(client.CurrentUser.Id, dblToken) 27 | { 28 | this.client = client; 29 | } 30 | 31 | public async Task GetBotAsync(IUser user) 32 | { 33 | return await GetBotAsync(user.Id); 34 | } 35 | 36 | public async Task GetUserAsync(IUser user) 37 | { 38 | return await GetUserAsync(user.Id); 39 | } 40 | 41 | /// 42 | /// Creates an IAdapter that updates your servercount on RunAsync(). 43 | /// 44 | /// Your already connected client 45 | /// 46 | /// Timespan for when you want to submit guildcount, leave null if you want it every JoinedGuild 47 | /// event 48 | /// 49 | /// an IAdapter that updates your servercount on RunAsync(), does not automatically do it yet. 50 | /// 51 | public virtual IAdapter CreateListener(TimeSpan? updateTime = null) 52 | { 53 | return new SubmissionAdapter(this, client, updateTime ?? TimeSpan.Zero); 54 | } 55 | } 56 | 57 | public class ShardedDiscordNetDblApi : DiscordNetDblApi 58 | { 59 | public ShardedDiscordNetDblApi(DiscordShardedClient client, string dblToken) : base(client, dblToken) 60 | { 61 | } 62 | 63 | /// 64 | /// Creates an IAdapter that updates your servercount on RunAsync(). 65 | /// 66 | /// Your already connected client 67 | /// 68 | /// Timespan for when you want to submit guildcount, leave null if you want it every JoinedGuild 69 | /// event 70 | /// 71 | /// an IAdapter that updates your servercount on RunAsync(), does not automatically do it yet. 72 | /// 73 | public override IAdapter CreateListener(TimeSpan? updateTime = null) 74 | { 75 | return new ShardedSubmissionAdapter(this, client as DiscordShardedClient, updateTime ?? TimeSpan.Zero); 76 | } 77 | } 78 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Internal/Bot.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Objects; 2 | using System; 3 | using System.Collections.Generic; 4 | using System.Text.Json.Serialization; 5 | using System.Threading.Tasks; 6 | 7 | namespace DiscordBotsList.Api.Internal 8 | { 9 | public class Bot : Entity, IDblBot 10 | { 11 | internal DiscordBotListApi api; 12 | 13 | [JsonPropertyName("clientid")] 14 | [JsonConverter(typeof(ULongToStringConverter))] 15 | public ulong clientId { get; set; } 16 | 17 | [JsonPropertyName("prefix")] public string prefix { get; set; } 18 | 19 | [JsonPropertyName("shortdesc")] public string shortDescription { get; set; } 20 | 21 | [JsonPropertyName("longdesc")] public string longDescription { get; set; } 22 | 23 | [JsonPropertyName("tags")] public List tags { get; set; } 24 | 25 | [JsonPropertyName("website")] public string websiteUrl { get; set; } 26 | 27 | [JsonPropertyName("support")] 28 | public string supportUrl { get; set; } 29 | 30 | [Obsolete("Actually refers to the entire support invite URL, not just its invite code. Use SupportUrl instead.")] 31 | public string SupportInviteCode => supportUrl; 32 | 33 | [JsonPropertyName("github")] public string githubUrl { get; set; } 34 | 35 | [JsonPropertyName("owners")] public List owners { get; set; } 36 | 37 | [JsonPropertyName("invite")] public string customInvite { get; set; } 38 | 39 | [Obsolete("Actually refers to when the bot was submitted. Use submittedAt instead.")] 40 | public DateTime approvedAt => submittedAt; 41 | 42 | [JsonPropertyName("date")] public DateTime submittedAt { get; set; } 43 | 44 | [JsonPropertyName("certifiedBot")] public bool certified { get; set; } 45 | 46 | [JsonPropertyName("vanity")] public string vanity { get; set; } 47 | 48 | [JsonPropertyName("points")] public int points { get; set; } 49 | 50 | [JsonPropertyName("monthlyPoints")] public int monthlyPoints { get; set; } 51 | 52 | [JsonPropertyName("reviews")] 53 | public BotReviews reviews { get; set; } 54 | 55 | public ulong ClientId => clientId; 56 | 57 | public string VanityTag => vanity; 58 | 59 | [Obsolete("Actually refers to when the bot was submitted. Use SubmittedAt instead.")] 60 | public DateTime ApprovedAt => submittedAt; 61 | 62 | public DateTime SubmittedAt => submittedAt; 63 | 64 | public string GithubUrl => githubUrl; 65 | 66 | public string InviteUrl => customInvite ?? $"https://discord.com/oauth2/authorize?&client_id={Id}&scope=bot"; 67 | 68 | public bool IsCertified => certified; 69 | 70 | public string LongDescription => longDescription; 71 | 72 | public string PrefixUsed => prefix; 73 | 74 | public List OwnerIds => owners; 75 | 76 | public int Points => points; 77 | 78 | public int MonthlyPoints => monthlyPoints; 79 | 80 | public string ShortDescription => shortDescription; 81 | 82 | public List Tags => tags; 83 | 84 | public string SupportUrl => supportUrl; 85 | 86 | public string VanityUrl => "https://top.gg/bot/" + vanity; 87 | 88 | public string WebsiteUrl => websiteUrl; 89 | 90 | public BotReviews Reviews => reviews; 91 | 92 | public async Task GetStatsAsync() 93 | { 94 | return await ((AuthDiscordBotListApi)api).GetBotStatsAsync(Id); 95 | } 96 | } 97 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/DiscordBotListApi.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Internal; 2 | using DiscordBotsList.Api.Objects; 3 | using System; 4 | using System.Net.Http; 5 | using System.Net.Http.Json; 6 | using System.Text.Json; 7 | using System.Threading.Tasks; 8 | 9 | namespace DiscordBotsList.Api 10 | { 11 | public class DiscordBotListApi 12 | { 13 | protected const string baseEndpoint = "https://top.gg/api/"; 14 | private readonly JsonSerializerOptions _serializerOptions; 15 | protected readonly HttpClient _httpClient; 16 | 17 | public DiscordBotListApi() 18 | { 19 | _httpClient = new HttpClient(); 20 | _serializerOptions = new JsonSerializerOptions(); 21 | _serializerOptions.Converters.Add(new ULongToStringConverter()); 22 | } 23 | 24 | /// 25 | /// Gets bots from botlist 26 | /// 27 | /// amount of bots to appear per page (max: 500) 28 | /// current page to query 29 | /// List of Bot Objects 30 | [Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)] 31 | public Task> GetBotsAsync(int count = 50, int page = 0) 32 | { 33 | return null; 34 | } 35 | 36 | /// 37 | /// Get specific bot by Discord id 38 | /// 39 | /// Discord id 40 | /// Bot Object 41 | [Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)] 42 | public Task GetBotAsync(ulong id) 43 | { 44 | return null; 45 | } 46 | 47 | /// 48 | /// Get bot stats 49 | /// 50 | /// Discord id 51 | /// IBotStats object related to the bot 52 | [Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)] 53 | public Task GetBotStatsAsync(ulong id) 54 | { 55 | return null; 56 | } 57 | 58 | /// 59 | /// Get specific user by Discord id 60 | /// 61 | /// Discord id 62 | /// User Object 63 | [Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)] 64 | public Task GetUserAsync(ulong id) 65 | { 66 | return null; 67 | } 68 | 69 | /// 70 | /// Gets and parses objects 71 | /// 72 | /// Type to parse to 73 | /// Url to get from 74 | /// Object of type T 75 | protected async Task GetAsync(string url) 76 | { 77 | var t = await _httpClient.GetAsync(baseEndpoint + url); 78 | var payload = await t.Content.ReadAsStringAsync(); 79 | var o = JsonSerializer.Deserialize(payload, _serializerOptions); 80 | var result = t.IsSuccessStatusCode 81 | ? ApiResult.FromSuccess(await t.Content.ReadFromJsonAsync(_serializerOptions)) 82 | : ApiResult.FromHttpError(t.StatusCode); 83 | return result.Value; 84 | } 85 | 86 | /// 87 | /// returns true if voting multiplier = x2 88 | /// 89 | /// True or False 90 | public async Task IsWeekendAsync() 91 | { 92 | return (await GetAsync("weekend")).Weekend; 93 | } 94 | } 95 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/Objects/SmallWidgetOptions.cs: -------------------------------------------------------------------------------- 1 | using System.Collections.Generic; 2 | 3 | namespace DiscordBotsList.Api.Objects 4 | { 5 | public enum WidgetType 6 | { 7 | STATUS, 8 | SERVERS, 9 | LIB, 10 | UPVOTES, 11 | OWNER 12 | } 13 | 14 | public class SmallWidgetOptions 15 | { 16 | private int? AvatarBackgroundColor; 17 | private int? LeftColor; 18 | private int? LeftTextColor; 19 | private int? RightColor; 20 | private int? RightTextColor; 21 | private WidgetType Type; 22 | 23 | public SmallWidgetOptions SetType(WidgetType t) 24 | { 25 | Type = t; 26 | return this; 27 | } 28 | 29 | public SmallWidgetOptions SetAvatarBackgroundColor(int r, int g, int b) 30 | { 31 | AvatarBackgroundColor = Utils.FromColor(r, g, b); 32 | return this; 33 | } 34 | 35 | public SmallWidgetOptions SetLeftColor(int r, int g, int b) 36 | { 37 | LeftColor = Utils.FromColor(r, g, b); 38 | return this; 39 | } 40 | 41 | public SmallWidgetOptions SetRightColor(int r, int g, int b) 42 | { 43 | RightColor = Utils.FromColor(r, g, b); 44 | return this; 45 | } 46 | 47 | public SmallWidgetOptions SetLeftTextColor(int r, int g, int b) 48 | { 49 | LeftTextColor = Utils.FromColor(r, g, b); 50 | return this; 51 | } 52 | 53 | public SmallWidgetOptions SetRightTextColor(int r, int g, int b) 54 | { 55 | RightTextColor = Utils.FromColor(r, g, b); 56 | return this; 57 | } 58 | 59 | /// 60 | /// Builds and returns a value. 61 | /// 62 | /// Id of the bot 63 | /// Widget url 64 | public string Build(ulong botId) 65 | { 66 | var query = $"https://top.gg/api/widget/{Type.ToString().ToLower()}/{botId}.svg"; 67 | 68 | var args = new List(); 69 | 70 | if (AvatarBackgroundColor != null) 71 | args.Add($"avatarbg={AvatarBackgroundColor.Value.ToString("X")}"); 72 | 73 | if (LeftColor != null) 74 | args.Add($"leftcolor={LeftColor.Value.ToString("X")}"); 75 | 76 | if (RightColor != null) 77 | args.Add($"rightcolor={RightColor.Value.ToString("X")}"); 78 | 79 | if (LeftTextColor != null) 80 | args.Add($"lefttextcolor={LeftTextColor.Value.ToString("X")}"); 81 | 82 | if (RightTextColor != null) 83 | args.Add($"righttextcolor={RightTextColor.Value.ToString("X")}"); 84 | 85 | return Utils.CreateQuery(query, args.ToArray()); 86 | } 87 | } 88 | 89 | public class LargeWidgetOptions 90 | { 91 | private int? CertifiedColor; 92 | private int? DataColor; 93 | private int? HighlightColor; 94 | private int? LabelColor; 95 | private int? MiddleColor; 96 | private int? TopColor; 97 | private int? UsernameColor; 98 | 99 | public LargeWidgetOptions SetTopColor(int r, int g, int b) 100 | { 101 | TopColor = Utils.FromColor(r, g, b); 102 | return this; 103 | } 104 | 105 | public LargeWidgetOptions SetMiddleColor(int r, int g, int b) 106 | { 107 | MiddleColor = Utils.FromColor(r, g, b); 108 | return this; 109 | } 110 | 111 | public LargeWidgetOptions SetUsernameColor(int r, int g, int b) 112 | { 113 | UsernameColor = Utils.FromColor(r, g, b); 114 | return this; 115 | } 116 | 117 | public LargeWidgetOptions SetCertifiedColor(int r, int g, int b) 118 | { 119 | CertifiedColor = Utils.FromColor(r, g, b); 120 | return this; 121 | } 122 | 123 | public LargeWidgetOptions SetDataColor(int r, int g, int b) 124 | { 125 | DataColor = Utils.FromColor(r, g, b); 126 | return this; 127 | } 128 | 129 | public LargeWidgetOptions SetLabelColor(int r, int g, int b) 130 | { 131 | LabelColor = Utils.FromColor(r, g, b); 132 | return this; 133 | } 134 | 135 | public LargeWidgetOptions SetHighlightColor(int r, int g, int b) 136 | { 137 | HighlightColor = Utils.FromColor(r, g, b); 138 | return this; 139 | } 140 | 141 | /// 142 | /// Builds and returns a value. 143 | /// 144 | /// Id of the bot 145 | /// Widget url 146 | public string Build(ulong botId) 147 | { 148 | var query = $"https://top.gg/api/widget/{botId}.svg"; 149 | 150 | var args = new List(); 151 | 152 | if (TopColor != null) 153 | args.Add($"topcolor={TopColor.Value.ToString("X")}"); 154 | 155 | if (MiddleColor != null) 156 | args.Add($"middlecolor={MiddleColor.Value.ToString("X")}"); 157 | 158 | if (UsernameColor != null) 159 | args.Add($"usernamecolor={UsernameColor.Value.ToString("X")}"); 160 | 161 | if (CertifiedColor != null) 162 | args.Add($"certifiedcolor={CertifiedColor.Value.ToString("X")}"); 163 | 164 | if (DataColor != null) 165 | args.Add($"datacolor={DataColor.Value.ToString("X")}"); 166 | 167 | if (LabelColor != null) 168 | args.Add($"labelcolor={LabelColor.Value.ToString("X")}"); 169 | 170 | if (HighlightColor != null) 171 | args.Add($"highlightcolor={HighlightColor.Value.ToString("X")}"); 172 | 173 | return Utils.CreateQuery(query, args.ToArray()); 174 | } 175 | } 176 | 177 | internal static class Utils 178 | { 179 | public static int FromColor(float r, float g, float b) 180 | { 181 | return FromColor((int)(r * 255), (int)(g * 255), (int)(b * 255)); 182 | } 183 | 184 | public static int FromColor(int r, int g, int b) 185 | { 186 | return (255 << 24) | ((byte)r << 16) | ((byte)g << 8) | ((byte)b << 0); 187 | } 188 | 189 | /// 190 | /// Creates rest parameters 191 | /// 192 | /// url 193 | /// arguments 194 | /// baseUrl?argument[0]&argument[1]&... 195 | public static string CreateQuery(string baseUrl, params string[] args) 196 | { 197 | if (args.Length > 0) return $"{baseUrl}?{string.Join("&", args)}"; 198 | 199 | return baseUrl; 200 | } 201 | } 202 | } -------------------------------------------------------------------------------- /DiscordBotsList.Api/AuthenticatedBotListApi.cs: -------------------------------------------------------------------------------- 1 | using DiscordBotsList.Api.Internal; 2 | using DiscordBotsList.Api.Internal.Queries; 3 | using DiscordBotsList.Api.Objects; 4 | using System; 5 | using System.Collections.Generic; 6 | using System.Linq; 7 | using System.Net.Http; 8 | using System.Net.Http.Headers; 9 | using System.Text; 10 | using System.Text.Json; 11 | using System.Threading.Tasks; 12 | 13 | namespace DiscordBotsList.Api 14 | { 15 | public enum SortBotsBy 16 | { 17 | MonthlyPoints, 18 | Id, 19 | Date, 20 | } 21 | 22 | public class AuthDiscordBotListApi : DiscordBotListApi 23 | { 24 | private readonly ulong _selfId; 25 | 26 | public AuthDiscordBotListApi(ulong selfId, string token) 27 | { 28 | _selfId = selfId; 29 | _httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token); 30 | } 31 | 32 | /// 33 | /// Fetches bots from Top.gg 34 | /// 35 | /// amount of bots to retrieve (max: 500) 36 | /// amount of bots to skip 37 | /// sorts results based on their monthly vote count, id, or their submission date 38 | /// List of Bot Objects 39 | public async Task> GetBotsAsync(int count = 50, int offset = 0, SortBotsBy sortBy = SortBotsBy.MonthlyPoints) 40 | { 41 | var sortByString = sortBy.ToString(); 42 | sortByString = char.ToLowerInvariant(sortByString[0]) + sortByString[1..]; 43 | 44 | var result = await GetAsync($"bots?limit={count}&offset={offset}&sort={sortByString}"); 45 | 46 | foreach (var bot in result.Items) (bot as Bot).api = this; 47 | return result; 48 | } 49 | 50 | /// 51 | /// Template 52 | /// of GetBotAsync for internal usage. 53 | /// 54 | /// Type of Bot 55 | /// Discord id 56 | /// Bot object of type T 57 | internal async Task GetBotAsync(ulong id) where T : Bot 58 | { 59 | var t = await GetAsync($"bots/{id}"); 60 | if (t == null) return null; 61 | t.api = this; 62 | return t; 63 | } 64 | 65 | /// 66 | /// Get specific bot by Discord id 67 | /// 68 | /// Discord id 69 | /// Bot Object 70 | public new async Task GetBotAsync(ulong id) 71 | { 72 | return await GetBotAsync(id); 73 | } 74 | 75 | /// 76 | /// Get bot stats 77 | /// 78 | /// Discord id, no longer needed 79 | /// IBotStats object related to the bot 80 | public new async Task GetBotStatsAsync(ulong id = 0) 81 | { 82 | return await GetAsync($"bots/{_selfId}/stats"); 83 | } 84 | 85 | /// 86 | /// Get specific user by Discord id 87 | /// 88 | /// Discord id 89 | /// User Object 90 | public new async Task GetUserAsync(ulong id) 91 | { 92 | return await GetAsync($"users/{id}"); 93 | } 94 | 95 | /// 96 | /// Gets your own bot with as an ISelfBot 97 | /// 98 | /// your own bot with as an ISelfBot 99 | public async Task GetMeAsync() 100 | { 101 | var bot = await GetBotAsync(_selfId); 102 | bot.api = this; 103 | return bot; 104 | } 105 | 106 | /// 107 | /// Fetches unique voters that have voted for your project 108 | /// 109 | /// The page number, defaults to 1 110 | /// A list of voters 111 | public async Task> GetVotersAsync(int page = 1) 112 | { 113 | return (await GetAsync>($"bots/{_selfId}/votes?page={Math.Max(page, 1)}")).Cast().ToList(); 114 | } 115 | 116 | /// 117 | /// Update your stats unsharded 118 | /// 119 | /// count of guilds 120 | public async Task UpdateStats(int guildCount) 121 | { 122 | await UpdateStatsAsync(new GuildCountObject(guildCount)); 123 | } 124 | 125 | /// 126 | /// Update your stats sharded 127 | /// 128 | /// Begin shard id 129 | /// Total shards 130 | /// Guild count per shards 131 | public async Task UpdateStats(int shardId, int shardCount, params int[] shards) 132 | { 133 | await UpdateStatsAsync(new ShardedGuildCountObject 134 | { 135 | ShardId = shardId, 136 | ShardCount = shardCount, 137 | Shards = shards 138 | }); 139 | } 140 | 141 | /// 142 | /// Update your stats sharded 143 | /// 144 | /// count of guilds 145 | /// Total shards 146 | public async Task UpdateStats(int guildCount, int shardCount) 147 | { 148 | await UpdateStatsAsync(new ShardedGuildCountObject 149 | { 150 | ShardCount = shardCount, 151 | GuildCount = guildCount 152 | }); 153 | } 154 | 155 | /// 156 | /// returns true if the user has voted for your project in the past 12 hours 157 | /// 158 | /// the user ID 159 | /// True or False 160 | public async Task HasVoted(ulong userId) 161 | { 162 | return await HasVotedAsync(userId); 163 | } 164 | 165 | protected async Task> GetVotersAsync() 166 | { 167 | var query = $"bots/{_selfId}/votes"; 168 | return await GetAuthorizedAsync>(Utils.CreateQuery(query)); 169 | } 170 | 171 | protected async Task UpdateStatsAsync(object statsObject) 172 | { 173 | var json = JsonSerializer.Serialize(statsObject); 174 | var httpContent = new StringContent(json, Encoding.UTF8, "application/json"); 175 | await _httpClient 176 | .PostAsync($"{baseEndpoint}/bots/{_selfId}/stats", httpContent); 177 | } 178 | 179 | protected Task GetAuthorizedAsync(string url) 180 | { 181 | return GetAsync(url); 182 | } 183 | 184 | protected async Task HasVotedAsync(ulong userId) 185 | { 186 | var url = $"bots/{_selfId}/check?userId={userId}"; 187 | return (await GetAsync(url)).HasVoted.GetValueOrDefault(0) == 1; 188 | } 189 | } 190 | } --------------------------------------------------------------------------------