Browse Source

Complete merge of upstream/dev

pull/1958/head
quin lynch 4 years ago
parent
commit
babda495ad
5 changed files with 4 additions and 219 deletions
  1. +4
    -1
      src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj
  2. +0
    -42
      src/Discord.Net.WebSocket/Entities/Invites/ISocketInvite.cs
  3. +0
    -47
      src/Discord.Net.WebSocket/Entities/Invites/InviteCache.cs
  4. +0
    -112
      src/Discord.Net.WebSocket/Entities/Invites/SocketGuildInvite.cs
  5. +0
    -17
      src/Discord.Net.WebSocket/Entities/Invites/SocketInviteHelper.cs

+ 4
- 1
src/Discord.Net.WebSocket/Discord.Net.WebSocket.csproj View File

@@ -1,6 +1,6 @@
<Project Sdk="Microsoft.NET.Sdk">
<Import Project="../../Discord.Net.targets" />
<Import Project="../../StyleAnalyzer.targets"/>
<Import Project="../../StyleAnalyzer.targets" />
<PropertyGroup>
<AssemblyName>Discord.Net.WebSocket</AssemblyName>
<RootNamespace>Discord.WebSocket</RootNamespace>
@@ -13,4 +13,7 @@
<ProjectReference Include="..\Discord.Net.Core\Discord.Net.Core.csproj" />
<ProjectReference Include="..\Discord.Net.Rest\Discord.Net.Rest.csproj" />
</ItemGroup>
<ItemGroup>
<Folder Include="Entities\Invites\" />
</ItemGroup>
</Project>

+ 0
- 42
src/Discord.Net.WebSocket/Entities/Invites/ISocketInvite.cs View File

@@ -1,42 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.WebSocket
{
public interface ISocketInvite
{
/// <summary>
/// Gets the unique identifier for this invite.
/// </summary>
/// <returns>
/// A string containing the invite code (e.g. <c>FTqNnyS</c>).
/// </returns>
string Code { get; }
/// <summary>
/// Gets the URL used to accept this invite
/// </summary>
/// <returns>
/// A string containing the full invite URL (e.g. <c>https://discord.gg/FTqNnyS</c>).
/// </returns>
string Url { get; }

/// <summary>
/// Gets the channel this invite is linked to.
/// </summary>
/// <returns>
/// A generic channel that the invite points to.
/// </returns>
SocketGuildChannel Channel { get; }
/// <summary>
/// Gets the guild this invite is linked to.
/// </summary>
/// <returns>
/// A guild object representing the guild that the invite points to.
/// </returns>
SocketGuild Guild { get; }
}
}

+ 0
- 47
src/Discord.Net.WebSocket/Entities/Invites/InviteCache.cs View File

@@ -1,47 +0,0 @@
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.WebSocket
{
internal class InviteCache
{
private readonly ConcurrentDictionary<string, SocketGuildInvite> _invites;
private readonly ConcurrentQueue<string> _queue;
private static int _size;

public InviteCache(DiscordSocketClient client)
{
//NOTE:
//This should be an option in the client config. default for now is 20 invites per guild
_size = client.Guilds.Count * 20;

_invites = new ConcurrentDictionary<string, SocketGuildInvite>();
_queue = new ConcurrentQueue<string>();
}
public void Add(SocketGuildInvite invite)
{
if(_invites.TryAdd(invite.Code, invite))
{
_queue.Enqueue(invite.Code);

while (_queue.Count > _size && _queue.TryDequeue(out string invCode))
_invites.TryRemove(invCode, out _);
}
}
public SocketGuildInvite Remove(string inviteCode)
{
_invites.TryRemove(inviteCode, out SocketGuildInvite inv);
return inv;
}
public SocketGuildInvite Get(string inviteCode)
{
if(_invites.TryGetValue(inviteCode, out SocketGuildInvite inv))
return inv;
return null;
}
}
}

+ 0
- 112
src/Discord.Net.WebSocket/Entities/Invites/SocketGuildInvite.cs View File

@@ -1,112 +0,0 @@
using Discord.Rest;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Runtime.Serialization.Formatters;
using System.Text;
using System.Threading.Tasks;
using InviteUpdate = Discord.API.Gateway.InviteCreatedEvent;

namespace Discord.WebSocket
{
/// <summary>
/// Represents a guild invite
/// </summary>
public class SocketGuildInvite : SocketEntity<string>, ISocketInvite
{
public string Code { get; private set; }
public string Url => $"{DiscordConfig.InviteUrl}{Code}";
public SocketGuildChannel Channel { get; private set; }
public SocketGuild Guild { get; private set; }
/// <summary>
/// Gets the unique invite code
/// <returns>
/// Returns the unique invite code
/// </returns>
/// </summary>
public string Id => Code;
/// <summary>
/// Gets the user who created the invite
/// <returns>
/// Returns the user who created the invite
/// </returns>
/// </summary>
public SocketGuildUser Inviter { get; private set; }
/// <summary>
/// Gets the maximum number of times the invite can be used, if there is no limit then the value will be 0
/// <returns>
/// Returns the maximum number of times the invite can be used, if there is no limit then the value will be 0
/// </returns>
/// </summary>
public int? MaxUses { get; private set; }
/// <summary>
/// Gets whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role)
/// <returns>
/// Returns whether or not the invite is temporary (invited users will be kicked on disconnect unless they're assigned a role)
/// </returns>
/// </summary>
public bool Temporary { get; private set; }
/// <summary>
/// Gets the time at which the invite was created
/// <returns>
/// Returns the time at which the invite was created
/// </returns>
/// </summary>
public DateTimeOffset? CreatedAt { get; private set; }
/// <summary>
/// Gets how long the invite is valid for
/// <returns>
/// Returns how long the invite is valid for (in seconds)
/// </returns>
/// </summary>
public TimeSpan? MaxAge { get; private set; }

internal SocketGuildInvite(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, RestInviteMetadata rest) : base(_client, inviteCode)
{
Code = inviteCode;
Guild = guild;
Channel = channel;
CreatedAt = rest.CreatedAt;
Temporary = rest.IsTemporary;
MaxUses = rest.MaxUses;
Inviter = guild.GetUser(rest.Inviter.Id);
if (rest.MaxAge.HasValue)
MaxAge = TimeSpan.FromSeconds(rest.MaxAge.Value);
}
internal SocketGuildInvite(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, InviteUpdate Update) : base(_client, inviteCode)
{
Code = inviteCode;
Guild = guild;
Channel = channel;

if (Update.RawTimestamp.IsSpecified)
CreatedAt = Update.RawTimestamp.Value;
else
CreatedAt = DateTimeOffset.Now;

if (Update.inviter.IsSpecified)
Inviter = guild.GetUser(Update.inviter.Value.Id);

Temporary = Update.TempInvite;
MaxUses = Update.MaxUsers;
MaxAge = TimeSpan.FromSeconds(Update.RawAge);
}
internal static SocketGuildInvite Create(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, InviteUpdate Update)
{
var invite = new SocketGuildInvite(_client, guild, channel, inviteCode, Update);
return invite;
}
internal static SocketGuildInvite CreateFromRest(DiscordSocketClient _client, SocketGuild guild, SocketGuildChannel channel, string inviteCode, RestInviteMetadata rest)
{
var invite = new SocketGuildInvite(_client, guild, channel, inviteCode, rest);
return invite;
}
/// <summary>
/// Deletes the invite
/// </summary>
/// <param name="options"></param>
/// <returns></returns>
public Task DeleteAsync(RequestOptions options = null)
=> SocketInviteHelper.DeleteAsync(this, Discord, options);
}
}

+ 0
- 17
src/Discord.Net.WebSocket/Entities/Invites/SocketInviteHelper.cs View File

@@ -1,17 +0,0 @@
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace Discord.WebSocket
{
internal class SocketInviteHelper
{
public static async Task DeleteAsync(ISocketInvite invite, BaseSocketClient client,
RequestOptions options)
{
await client.ApiClient.DeleteInviteAsync(invite.Code, options).ConfigureAwait(false);
}
}
}

Loading…
Cancel
Save