Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
21 commits
Select commit Hold shift + click to select a range
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
89 changes: 89 additions & 0 deletions DiscordBotsList.Api.Adapter.Discord.Net/Adapter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
#nullable enable

using DiscordBotsList.Api.Objects;
using System;
using System.Threading;
using System.Threading.Tasks;

namespace DiscordBotsList.Api.Adapter.Discord.Net
{
public class Adapter : IAdapter
{
public event Action<Exception?> Posted = _ => { };
private readonly TimeSpan updateTime;

private CancellationTokenSource? cancellationTokenSource;

public Adapter(TimeSpan updateTime)
{
if (updateTime < TimeSpan.FromMinutes(15))
{
updateTime = TimeSpan.FromMinutes(15);
}

this.updateTime = updateTime;
cancellationTokenSource = null;
}

public virtual Task RunAsync()
{
throw new NotImplementedException();
}

public bool IsRunning()
{
return cancellationTokenSource != null;
}

public void Start()
{
if (IsRunning())
{
return;
}

cancellationTokenSource = new CancellationTokenSource();

Task.Run(async () =>
{
while (!cancellationTokenSource.Token.IsCancellationRequested)
{
try
{
await RunAsync();
}
catch (Exception err)
{
cancellationTokenSource.Cancel();
cancellationTokenSource = null;

Posted?.Invoke(err);
break;
}

Posted?.Invoke(null);

await Task.Delay(updateTime, cancellationTokenSource.Token);
}
}, cancellationTokenSource.Token);
}

public void Stop()
{
if (IsRunning())
{
cancellationTokenSource!.Cancel();
cancellationTokenSource = null;
}
}

public async Task StopAsync()
{
if (IsRunning())
{
await cancellationTokenSource!.CancelAsync();
cancellationTokenSource = null;
}
}
}
}
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>2.0.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
Original file line number Diff line number Diff line change
@@ -1,8 +1,6 @@
using Discord;
using Discord.WebSocket;
using DiscordBotsList.Api.Objects;
using System;
using System.Threading.Tasks;

namespace DiscordBotsList.Api.Adapter.Discord.Net
{
Expand All @@ -12,14 +10,9 @@ public static DiscordNetDblApi CreateDblApi(this DiscordSocketClient client, str
{
return new DiscordNetDblApi(client, dblToken);
}

public static ShardedDiscordNetDblApi CreateDblApi(this DiscordShardedClient client, string dblToken)
{
return new ShardedDiscordNetDblApi(client, dblToken);
}
}

public class DiscordNetDblApi : AuthDiscordBotListApi
public class DiscordNetDblApi : DiscordBotListApi
{
protected IDiscordClient client;

Expand All @@ -28,51 +21,17 @@ public DiscordNetDblApi(IDiscordClient client, string dblToken) : base(client.Cu
this.client = client;
}

public async Task<IDblBot> GetBotAsync(IUser user)
{
return await GetBotAsync(user.Id);
}

public async Task<IDblUser> GetUserAsync(IUser user)
{
return await GetUserAsync(user.Id);
}

/// <summary>
/// Creates an IAdapter that updates your servercount on RunAsync().
/// </summary>
/// <param name="client">Your already connected client</param>
/// <param name="updateTime">
/// Timespan for when you want to submit guildcount, leave null if you want it every JoinedGuild
/// event
/// </param>
/// <returns>an IAdapter that updates your servercount on RunAsync(), does not automatically do it yet.</returns>
/// <seealso cref="ListenAsync()" />
public virtual IAdapter CreateListener(TimeSpan? updateTime = null)
{
return new SubmissionAdapter(this, client, updateTime ?? TimeSpan.Zero);
}
}

public class ShardedDiscordNetDblApi : DiscordNetDblApi
{
public ShardedDiscordNetDblApi(DiscordShardedClient client, string dblToken) : base(client, dblToken)
{
}

/// <summary>
/// Creates an IAdapter that updates your servercount on RunAsync().
/// Creates a SubmissionAdapter that updates your servercount on RunAsync().
/// </summary>
/// <param name="client">Your already connected client</param>
/// <param name="updateTime">
/// Timespan for when you want to submit guildcount, leave null if you want it every JoinedGuild
/// event
/// Timespan for when you want to submit guildcount, must be at least 15 minutes
/// </param>
/// <returns>an IAdapter that updates your servercount on RunAsync(), does not automatically do it yet.</returns>
/// <returns>A SubmissionAdapter that updates your servercount on RunAsync().</returns>
/// <seealso cref="ListenAsync()" />
public override IAdapter CreateListener(TimeSpan? updateTime = null)
public SubmissionAdapter CreateListener(TimeSpan? updateTime = null)
{
return new ShardedSubmissionAdapter(this, client as DiscordShardedClient, updateTime ?? TimeSpan.Zero);
return new SubmissionAdapter(this, client, updateTime ?? TimeSpan.FromMinutes(15));
}
}
}
65 changes: 7 additions & 58 deletions DiscordBotsList.Api.Adapter.Discord.Net/SubmissionAdapter.cs
Original file line number Diff line number Diff line change
@@ -1,76 +1,25 @@
using Discord;
using Discord.WebSocket;
using DiscordBotsList.Api.Objects;
using System;
using System.Linq;
using System.Threading;
using System.Threading.Tasks;

namespace DiscordBotsList.Api.Adapter.Discord.Net
{
internal class SubmissionAdapter : IAdapter
public class SubmissionAdapter : Adapter
{
protected AuthDiscordBotListApi api;
protected IDiscordClient client;
private readonly DiscordBotListApi api;
private readonly IDiscordClient client;

protected DateTime lastTimeUpdated;
protected TimeSpan updateTime;

public SubmissionAdapter(AuthDiscordBotListApi api, IDiscordClient client, TimeSpan updateTime)
public SubmissionAdapter(DiscordBotListApi api, IDiscordClient client, TimeSpan updateTime) : base(updateTime)
{
this.api = api;
this.client = client;
this.updateTime = updateTime;
}

public event Action<string> Log;

public virtual async Task RunAsync()
{
if (DateTime.Now > lastTimeUpdated + updateTime)
{
await api.UpdateStats(
(await client.GetGuildsAsync()).Count
);

lastTimeUpdated = DateTime.Now;
SendLog("Submitted stats to Top.gg!");
}
}

public virtual void Start()
{
}

public virtual void Stop()
{
throw new NotImplementedException();
}

protected void SendLog(string msg)
{
Log?.Invoke(msg);
}
}

internal class ShardedSubmissionAdapter : SubmissionAdapter, IAdapter
{
public ShardedSubmissionAdapter(AuthDiscordBotListApi api, DiscordShardedClient client, TimeSpan updateTime)
: base(api, client, updateTime)
{
}

public override async Task RunAsync()
{
if (DateTime.Now > lastTimeUpdated + updateTime)
{
await api.UpdateStats(
0,
(client as DiscordShardedClient).Shards.Count,
(client as DiscordShardedClient).Shards.Select(x => x.Guilds.Count).ToArray()
);

lastTimeUpdated = DateTime.Now;
SendLog("Sent stats to Top.gg!");
}
await api.UpdateBotServerCountAsync((await client.GetGuildsAsync()).Count);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -14,16 +14,5 @@ public static DiscordNetDblApi CreateDblApi(this DiscordSocketClient client, str
{
return new DiscordNetDblApi(client, dblToken);
}

/// <summary>
/// Creates a DiscordBotsList Api
/// </summary>
/// <param name="client">your client</param>
/// <param name="dblToken">Your DiscordBotsList token</param>
/// <returns>A new instance of a DblApi</returns>
public static ShardedDiscordNetDblApi CreateDblApi(this DiscordShardedClient client, string dblToken)
{
return new ShardedDiscordNetDblApi(client, dblToken);
}
}
}
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>
Loading