Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -2,23 +2,23 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Authors>Velddev, Faith Viola</Authors>
<Authors>Velddev, Faith</Authors>
<Company>Top.gg</Company>
<Product>DiscordBotsList.Api.Adapter.Discord.Net</Product>
<Description>Adapter for Discord.Net</Description>
<Description>Top.gg API adapter for Discord.Net</Description>
<PackageReleaseNotes>Initial release</PackageReleaseNotes>
<GeneratePackageOnBuild>true</GeneratePackageOnBuild>
<Copyright>Mike Veldsink</Copyright>
<PackageTags>discord bots list org wrapper api discord.net</PackageTags>
<PackageTags>discord bots topgg api discord.net</PackageTags>
<RepositoryType>git</RepositoryType>
<RepositoryUrl>https://github.com/DiscordBotList/DBL-dotnet-Library</RepositoryUrl>
<PackageProjectUrl>https://github.com/DiscordBotList/DBL-dotnet-Library</PackageProjectUrl>
<Version>1.5.0</Version>
<RepositoryUrl>https://github.com/Top-gg-Community/dotnet-sdk</RepositoryUrl>
<PackageProjectUrl>https://github.com/Top-gg-Community/dotnet-sdk</PackageProjectUrl>
<Version>1.6.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Discord.Net" Version="2.4.0"/>
<PackageReference Include="Discord.Net" Version="3.17.4"/>
</ItemGroup>

<ItemGroup>
Expand Down
14 changes: 7 additions & 7 deletions DiscordBotsList.Api.Tests/DiscordBotsList.Api.Tests.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -8,22 +8,22 @@
</PropertyGroup>

<ItemGroup>
<None Remove="settings.json"/>
<None Remove="settings.json" />
</ItemGroup>

<ItemGroup>
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.1.20200127.214830"/>
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="16.11.0"/>
<PackageReference Include="xunit" Version="2.4.1"/>
<PackageReference Include="xunit.runner.visualstudio" Version="2.4.3">
<PackageReference Include="JetBrains.DotMemoryUnit" Version="3.2.20220510" />
<PackageReference Include="Microsoft.NET.Test.Sdk" Version="17.13.0" />
<PackageReference Include="xunit" Version="2.9.3" />
<PackageReference Include="xunit.runner.visualstudio" Version="3.0.2">
<PrivateAssets>all</PrivateAssets>
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
</PackageReference>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1"/>
<DotNetCliToolReference Include="dotnet-xunit" Version="2.3.1" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\DiscordBotsList.Api\DiscordBotsList.Api.csproj"/>
<ProjectReference Include="..\DiscordBotsList.Api\DiscordBotsList.Api.csproj" />
</ItemGroup>

</Project>
31 changes: 6 additions & 25 deletions DiscordBotsList.Api.Tests/UnitTests.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using System;
using System.Linq;
using System.Threading.Tasks;
using Xunit;

Expand All @@ -12,10 +11,11 @@ public class Credentials

public static Credentials LoadFromEnv()
{
var cred = new Credentials();
cred.BotId = ulong.Parse(Environment.GetEnvironmentVariable("BOT_ID"));
cred.Token = Environment.GetEnvironmentVariable("API_KEY");
return cred;
return new Credentials()
{
BotId = ulong.Parse(Environment.GetEnvironmentVariable("BOT_ID")),
Token = Environment.GetEnvironmentVariable("API_KEY")
};
}
}

Expand All @@ -30,13 +30,6 @@ public UnitTests()
_api = new AuthDiscordBotListApi(_cred.BotId, _cred.Token);
}

[Fact]
public void GetUserTest()
{
Assert.NotNull(_api.GetMeAsync());
Assert.NotNull(_api.GetUserAsync(_cred.BotId));
}

[Fact]
public async Task HasVotedTestAsync()
{
Expand All @@ -55,16 +48,10 @@ public async Task TaskGetVotersTestAsync()
Assert.NotNull(await _api.GetVotersAsync());
}

[Fact]
public async Task GetUserTestAsync()
{
Assert.NotNull(await _api.GetUserAsync(181514288278536193));
}

[Fact]
public async Task GetBotTestAsync()
{
var botId = 423593006436712458U;
var botId = 1026525568344264724U;
var bot = await _api.GetBotAsync(botId);
Assert.NotNull(bot);
Assert.Equal(botId, bot.Id);
Expand All @@ -83,12 +70,6 @@ public async Task GetUsersGetStatsTest()

Assert.NotNull(bots);
Assert.NotEmpty(bots.Items);

var firstBot = bots.Items.First();

var stats = await firstBot.GetStatsAsync();

Assert.NotNull(stats);
}
}
}
90 changes: 80 additions & 10 deletions DiscordBotsList.Api/AuthenticatedBotListApi.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
using DiscordBotsList.Api.Internal;
using DiscordBotsList.Api.Internal.Queries;
using DiscordBotsList.Api.Objects;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Net.Http;
Expand All @@ -10,18 +12,86 @@

namespace DiscordBotsList.Api
{
public enum SortBotsBy
{
MonthlyPoints,
Id,
Date,
}

public class AuthDiscordBotListApi : DiscordBotListApi
{
private readonly ulong _selfId;
private readonly string _token;

public AuthDiscordBotListApi(ulong selfId, string token)
{
_selfId = selfId;
_token = token;
_httpClient.DefaultRequestHeaders.Authorization = new AuthenticationHeaderValue("Bearer", token);
}

/// <summary>
/// Fetches bots from Top.gg
/// </summary>
/// <param name="count">amount of bots to retrieve (max: 500)</param>
/// <param name="offset">amount of bots to skip</param>
/// <param name="sortBy">sorts results based on their monthly vote count, id, or their submission date</param>
/// <returns>List of Bot Objects</returns>
public async Task<ISearchResult<IDblBot>> GetBotsAsync(int count = 50, int offset = 0, SortBotsBy sortBy = SortBotsBy.MonthlyPoints)
{
var sortByString = sortBy.ToString();
sortByString = char.ToLowerInvariant(sortByString[0]) + sortByString[1..];

var result = await GetAsync<BotListQuery>($"bots?limit={count}&offset={offset}&sort={sortByString}");

foreach (var bot in result.Items) (bot as Bot).api = this;
return result;
}

/// <summary>
/// Template
/// of GetBotAsync for internal usage.
/// </summary>
/// <typeparam name="T">Type of Bot</typeparam>
/// <param name="id">Discord id</param>
/// <returns>Bot object of type T</returns>
internal async Task<T> GetBotAsync<T>(ulong id) where T : Bot
{
var t = await GetAsync<T>($"bots/{id}");
if (t == null) return null;
t.api = this;
return t;
}

/// <summary>
/// Get specific bot by Discord id
/// </summary>
/// <param name="id">Discord id</param>
/// <returns>Bot Object</returns>
public new async Task<IDblBot> GetBotAsync(ulong id)
{
return await GetBotAsync<Bot>(id);
}

/// <summary>
/// Get bot stats
/// </summary>
/// <param name="id">Discord id, no longer needed</param>
/// <returns>IBotStats object related to the bot</returns>
public new async Task<IDblBotStats> GetBotStatsAsync(ulong id = 0)
{
return await GetAsync<BotStatsObject>($"bots/{_selfId}/stats");
}

/// <summary>
/// Get specific user by Discord id
/// </summary>
/// <param name="id">Discord id</param>
/// <returns>User Object</returns>
public new async Task<IDblUser> GetUserAsync(ulong id)
{
return await GetAsync<User>($"users/{id}");
}

/// <summary>
/// Gets your own bot with as an ISelfBot
/// </summary>
Expand All @@ -34,13 +104,13 @@ public async Task<IDblSelfBot> GetMeAsync()
}

/// <summary>
/// Gets all voters that have voted on your bot
/// Max 1000, If you have more, you MUST use WEBHOOKS instead.
/// Fetches unique voters that have voted for your project
/// </summary>
/// <param name="page">The page number, defaults to 1</param>
/// <returns>A list of voters</returns>
public async Task<List<IDblEntity>> GetVotersAsync()
public async Task<List<IDblEntity>> GetVotersAsync(int page = 1)
{
return (await GetVotersAsync<Entity>()).Cast<IDblEntity>().ToList();
return (await GetAsync<List<Entity>>($"bots/{_selfId}/votes?page={Math.Max(page, 1)}")).Cast<IDblEntity>().ToList();
}

/// <summary>
Expand Down Expand Up @@ -83,9 +153,9 @@ await UpdateStatsAsync(new ShardedGuildCountObject
}

/// <summary>
/// returns true if user have voted for the past 12 hours
/// returns true if the user has voted for your project in the past 12 hours
/// </summary>
/// <param name="userId">Amount of days to filter</param>
/// <param name="userId">the user ID</param>
/// <returns>True or False</returns>
public async Task<bool> HasVoted(ulong userId)
{
Expand All @@ -106,9 +176,9 @@ await _httpClient
.PostAsync($"{baseEndpoint}/bots/{_selfId}/stats", httpContent);
}

protected async Task<T> GetAuthorizedAsync<T>(string url)
protected Task<T> GetAuthorizedAsync<T>(string url)
{
return await GetAsync<T>(url);
return GetAsync<T>(url);
}

protected async Task<bool> HasVotedAsync(ulong userId)
Expand Down
41 changes: 14 additions & 27 deletions DiscordBotsList.Api/DiscordBotListApi.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
using DiscordBotsList.Api.Internal;
using DiscordBotsList.Api.Internal.Queries;
using DiscordBotsList.Api.Objects;
using System;
using System.Net.Http;
using System.Net.Http.Json;
using System.Text.Json;
Expand All @@ -12,7 +12,7 @@ public class DiscordBotListApi
{
protected const string baseEndpoint = "https://top.gg/api/";
private readonly JsonSerializerOptions _serializerOptions;
protected HttpClient _httpClient;
protected readonly HttpClient _httpClient;

public DiscordBotListApi()
{
Expand All @@ -27,56 +27,43 @@ public DiscordBotListApi()
/// <param name="count">amount of bots to appear per page (max: 500)</param>
/// <param name="page">current page to query</param>
/// <returns>List of Bot Objects</returns>
public async Task<ISearchResult<IDblBot>> GetBotsAsync(int count = 50, int page = 0)
[Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)]
public Task<ISearchResult<IDblBot>> GetBotsAsync(int count = 50, int page = 0)
{
var result = await GetAsync<BotListQuery>("bots");
foreach (var bot in result.Items) (bot as Bot).api = this;
return result;
return null;
}

/// <summary>
/// Get specific bot by Discord id
/// </summary>
/// <param name="id">Discord id</param>
/// <returns>Bot Object</returns>
public async Task<IDblBot> GetBotAsync(ulong id)
[Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)]
public Task<IDblBot> GetBotAsync(ulong id)
{
return await GetBotAsync<Bot>(id);
return null;
}

/// <summary>
/// Get bot stats
/// </summary>
/// <param name="id">Discord id</param>
/// <returns>IBotStats object related to the bot</returns>
public async Task<IDblBotStats> GetBotStatsAsync(ulong id)
[Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)]
public Task<IDblBotStats> GetBotStatsAsync(ulong id)
{
return await GetAsync<BotStatsObject>($"bots/{id}/stats");
return null;
}

/// <summary>
/// Get specific user by Discord id
/// </summary>
/// <param name="id">Discord id</param>
/// <returns>User Object</returns>
public async Task<IDblUser> GetUserAsync(ulong id)
[Obsolete("This method requires a token to work. Please use the AuthenticatedBotListApi class instead.", true)]
public Task<IDblUser> GetUserAsync(ulong id)
{
return await GetAsync<User>($"users/{id}");
}

/// <summary>
/// Template
/// of GetBotAsync for internal usage.
/// </summary>
/// <typeparam name="T">Type of Bot</typeparam>
/// <param name="id">Discord id</param>
/// <returns>Bot object of type T</returns>
internal async Task<T> GetBotAsync<T>(ulong id) where T : Bot
{
var t = await GetAsync<T>($"bots/{id}");
if (t == null) return null;
t.api = this;
return t;
return null;
}

/// <summary>
Expand Down
6 changes: 3 additions & 3 deletions DiscordBotsList.Api/DiscordBotsList.Api.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

<PropertyGroup>
<TargetFramework>net8.0</TargetFramework>
<Authors>Velddev, Faith Viola</Authors>
<Authors>Velddev, Faith</Authors>
<Company>Top.gg</Company>
<Description>top.gg api wrapper</Description>
<Copyright>Mike Veldsink</Copyright>
Expand All @@ -16,12 +16,12 @@
<AssemblyName>DiscordBotsList.Api</AssemblyName>
<RootNamespace>DiscordBotsList.Api</RootNamespace>
<Product>DiscordBotsList.Api</Product>
<Version>1.5.0</Version>
<Version>1.6.0</Version>
<PackageLicenseFile>LICENSE</PackageLicenseFile>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="System.Text.Json" Version="8.0.0" />
<PackageReference Include="System.Text.Json" Version="9.0.9" />
</ItemGroup>

<ItemGroup>
Expand Down
Loading