Browse Source

Removed Discord reference from User

tags/1.0-rc
RogueException 9 years ago
parent
commit
ffad33153c
17 changed files with 83 additions and 59 deletions
  1. +6
    -6
      src/Discord.Net/Entities/Channels/DMChannel.cs
  2. +6
    -6
      src/Discord.Net/Entities/Channels/TextChannel.cs
  3. +4
    -4
      src/Discord.Net/Entities/Guilds/Guild.cs
  4. +1
    -1
      src/Discord.Net/Entities/Guilds/GuildIntegration.cs
  5. +1
    -1
      src/Discord.Net/Entities/Invites/InviteMetadata.cs
  6. +1
    -1
      src/Discord.Net/Entities/Messages/Message.cs
  7. +1
    -1
      src/Discord.Net/Entities/Users/Game.cs
  8. +8
    -2
      src/Discord.Net/Entities/Users/GuildUser.cs
  9. +3
    -0
      src/Discord.Net/Entities/Users/IGuildUser.cs
  10. +1
    -1
      src/Discord.Net/Entities/Users/IPresence.cs
  11. +0
    -3
      src/Discord.Net/Entities/Users/IUser.cs
  12. +1
    -1
      src/Discord.Net/Entities/Users/SelfUser.cs
  13. +4
    -12
      src/Discord.Net/Entities/Users/User.cs
  14. +3
    -3
      src/Discord.Net/Entities/WebSocket/CachedGuildUser.cs
  15. +14
    -15
      src/Discord.Net/Entities/WebSocket/CachedPublicUser.cs
  16. +2
    -2
      src/Discord.Net/Entities/WebSocket/MessageCache.cs
  17. +27
    -0
      src/Discord.Net/Format.cs

+ 6
- 6
src/Discord.Net/Entities/Channels/DMChannel.cs View File

@@ -70,7 +70,7 @@ namespace Discord
{
var args = new CreateMessageParams { Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.CreateDMMessageAsync(Id, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
public async Task<IMessage> SendFileAsync(string filePath, string text, bool isTTS)
{
@@ -79,33 +79,33 @@ namespace Discord
{
var args = new UploadFileParams { Filename = filename, Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.UploadDMFileAsync(Id, file, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
}
public async Task<IMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
{
var args = new UploadFileParams { Filename = filename, Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.UploadDMFileAsync(Id, stream, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
public virtual async Task<IMessage> GetMessageAsync(ulong id)
{
var model = await Discord.ApiClient.GetChannelMessageAsync(Id, id).ConfigureAwait(false);
if (model != null)
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
return null;
}
public virtual async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
{
var args = new GetChannelMessagesParams { Limit = limit };
var models = await Discord.ApiClient.GetChannelMessagesAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new Message(this, new User(Discord, x.Author.Value), x)).ToImmutableArray();
return models.Select(x => new Message(this, new User(x.Author.Value), x)).ToImmutableArray();
}
public virtual async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
{
var args = new GetChannelMessagesParams { Limit = limit };
var models = await Discord.ApiClient.GetChannelMessagesAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new Message(this, new User(Discord, x.Author.Value), x)).ToImmutableArray();
return models.Select(x => new Message(this, new User(x.Author.Value), x)).ToImmutableArray();
}
public async Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
{


+ 6
- 6
src/Discord.Net/Entities/Channels/TextChannel.cs View File

@@ -62,7 +62,7 @@ namespace Discord
{
var args = new CreateMessageParams { Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.CreateMessageAsync(Guild.Id, Id, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
public async Task<IMessage> SendFileAsync(string filePath, string text, bool isTTS)
{
@@ -71,33 +71,33 @@ namespace Discord
{
var args = new UploadFileParams { Filename = filename, Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.UploadFileAsync(Guild.Id, Id, file, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
}
public async Task<IMessage> SendFileAsync(Stream stream, string filename, string text, bool isTTS)
{
var args = new UploadFileParams { Filename = filename, Content = text, IsTTS = isTTS };
var model = await Discord.ApiClient.UploadFileAsync(Guild.Id, Id, stream, args).ConfigureAwait(false);
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
}
public virtual async Task<IMessage> GetMessageAsync(ulong id)
{
var model = await Discord.ApiClient.GetChannelMessageAsync(Id, id).ConfigureAwait(false);
if (model != null)
return new Message(this, new User(Discord, model.Author.Value), model);
return new Message(this, new User(model.Author.Value), model);
return null;
}
public virtual async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(int limit)
{
var args = new GetChannelMessagesParams { Limit = limit };
var models = await Discord.ApiClient.GetChannelMessagesAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new Message(this, new User(Discord, x.Author.Value), x)).ToImmutableArray();
return models.Select(x => new Message(this, new User(x.Author.Value), x)).ToImmutableArray();
}
public virtual async Task<IReadOnlyCollection<IMessage>> GetMessagesAsync(ulong fromMessageId, Direction dir, int limit)
{
var args = new GetChannelMessagesParams { Limit = limit };
var models = await Discord.ApiClient.GetChannelMessagesAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new Message(this, new User(Discord, x.Author.Value), x)).ToImmutableArray();
return models.Select(x => new Message(this, new User(x.Author.Value), x)).ToImmutableArray();
}
public async Task DeleteMessagesAsync(IEnumerable<IMessage> messages)
{


+ 4
- 4
src/Discord.Net/Entities/Guilds/Guild.cs View File

@@ -149,7 +149,7 @@ namespace Discord
public async Task<IReadOnlyCollection<IUser>> GetBansAsync()
{
var models = await Discord.ApiClient.GetGuildBansAsync(Id).ConfigureAwait(false);
return models.Select(x => new User(Discord, x)).ToImmutableArray();
return models.Select(x => new User(x)).ToImmutableArray();
}
public Task AddBanAsync(IUser user, int pruneDays = 0) => AddBanAsync(user, pruneDays);
public async Task AddBanAsync(ulong userId, int pruneDays = 0)
@@ -254,7 +254,7 @@ namespace Discord
{
var model = await Discord.ApiClient.GetGuildMemberAsync(Id, id).ConfigureAwait(false);
if (model != null)
return new GuildUser(this, new User(Discord, model.User), model);
return new GuildUser(this, new User(model.User), model);
return null;
}
public virtual async Task<IGuildUser> GetCurrentUserAsync()
@@ -266,13 +266,13 @@ namespace Discord
{
var args = new GetGuildMembersParams();
var models = await Discord.ApiClient.GetGuildMembersAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new GuildUser(this, new User(Discord, x.User), x)).ToImmutableArray();
return models.Select(x => new GuildUser(this, new User(x.User), x)).ToImmutableArray();
}
public virtual async Task<IReadOnlyCollection<IGuildUser>> GetUsersAsync(int limit, int offset)
{
var args = new GetGuildMembersParams { Limit = limit, Offset = offset };
var models = await Discord.ApiClient.GetGuildMembersAsync(Id, args).ConfigureAwait(false);
return models.Select(x => new GuildUser(this, new User(Discord, x.User), x)).ToImmutableArray();
return models.Select(x => new GuildUser(this, new User(x.User), x)).ToImmutableArray();
}
public async Task<int> PruneUsersAsync(int days = 30, bool simulate = false)
{


+ 1
- 1
src/Discord.Net/Entities/Guilds/GuildIntegration.cs View File

@@ -46,7 +46,7 @@ namespace Discord
_syncedAtTicks = model.SyncedAt.UtcTicks;

Role = Guild.GetRole(model.RoleId);
User = new User(Discord, model.User);
User = new User(model.User);
}
public async Task DeleteAsync()


+ 1
- 1
src/Discord.Net/Entities/Invites/InviteMetadata.cs View File

@@ -25,7 +25,7 @@ namespace Discord
{
if (source == UpdateSource.Rest && IsAttached) return;

Inviter = new User(Discord, model.Inviter);
Inviter = new User(model.Inviter);
IsRevoked = model.Revoked;
IsTemporary = model.Temporary;
MaxAge = model.MaxAge != 0 ? model.MaxAge : (int?)null;


+ 1
- 1
src/Discord.Net/Entities/Messages/Message.cs View File

@@ -99,7 +99,7 @@ namespace Discord
{
var mentions = new User[value.Length];
for (int i = 0; i < value.Length; i++)
mentions[i] = new User(discord, value[i]);
mentions[i] = new User(value[i]);
MentionedUsers = ImmutableArray.Create(mentions);
}
else


+ 1
- 1
src/Discord.Net/Entities/Users/Game.cs View File

@@ -4,7 +4,7 @@ using Model = Discord.API.Game;
namespace Discord
{
[DebuggerDisplay(@"{DebuggerDisplay,nq}")]
public struct Game
public class Game
{
public string Name { get; }
public string StreamUrl { get; }


+ 8
- 2
src/Discord.Net/Entities/Users/GuildUser.cs View File

@@ -34,7 +34,7 @@ namespace Discord
public string Mention => User.Mention;
public string Username => User.Username;
public virtual UserStatus Status => User.Status;
public virtual Game? Game => User.Game;
public virtual Game Game => User.Game;

public DiscordClient Discord => Guild.Discord;
public DateTimeOffset? JoinedAt => DateTimeUtils.FromTicks(_joinedAtTicks);
@@ -149,8 +149,14 @@ namespace Discord
if (channel == null) throw new ArgumentNullException(nameof(channel));
return new ChannelPermissions(Permissions.ResolveChannel(this, channel, GuildPermissions.RawValue));
}
public async Task<IDMChannel> CreateDMChannelAsync()
{
var args = new CreateDMChannelParams { RecipientId = Id };
var model = await Discord.ApiClient.CreateDMChannelAsync(args).ConfigureAwait(false);

public Task<IDMChannel> CreateDMChannelAsync() => User.CreateDMChannelAsync();
return new DMChannel(Discord, User, model);
}

IGuild IGuildUser.Guild => Guild;
IReadOnlyCollection<IRole> IGuildUser.Roles => Roles;


+ 3
- 0
src/Discord.Net/Entities/Users/IGuildUser.cs View File

@@ -31,5 +31,8 @@ namespace Discord
Task KickAsync();
/// <summary> Modifies this user's properties in this guild. </summary>
Task ModifyAsync(Action<ModifyGuildMemberParams> func);

/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> CreateDMChannelAsync();
}
}

+ 1
- 1
src/Discord.Net/Entities/Users/IPresence.cs View File

@@ -3,7 +3,7 @@
public interface IPresence
{
/// <summary> Gets the game this user is currently playing, if any. </summary>
Game? Game { get; }
Game Game { get; }
/// <summary> Gets the current status of this user. </summary>
UserStatus Status { get; }
}

+ 0
- 3
src/Discord.Net/Entities/Users/IUser.cs View File

@@ -12,8 +12,5 @@ namespace Discord
bool IsBot { get; }
/// <summary> Gets the username for this user. </summary>
string Username { get; }
/// <summary> Returns a private message channel to this user, creating one if it does not already exist. </summary>
Task<IDMChannel> CreateDMChannelAsync();
}
}

+ 1
- 1
src/Discord.Net/Entities/Users/SelfUser.cs View File

@@ -11,7 +11,7 @@ namespace Discord
public bool IsVerified { get; private set; }

public SelfUser(DiscordClient discord, Model model)
: base(discord, model)
: base(model)
{
}
public override void Update(Model model, UpdateSource source)


+ 4
- 12
src/Discord.Net/Entities/Users/User.cs View File

@@ -1,4 +1,5 @@
using Discord.API.Rest;
using System;
using System.Diagnostics;
using System.Threading.Tasks;
using Model = Discord.API.User;
@@ -14,19 +15,18 @@ namespace Discord
public bool IsBot { get; private set; }
public string Username { get; private set; }

public override DiscordClient Discord { get; }
public override DiscordClient Discord { get { throw new NotSupportedException(); } }

public string AvatarUrl => API.CDN.GetUserAvatarUrl(Id, _avatarId);
public string Discriminator => _discriminator.ToString("D4");
public string Mention => MentionUtils.Mention(this, false);
public string NicknameMention => MentionUtils.Mention(this, true);
public virtual Game? Game => null;
public virtual Game Game => null;
public virtual UserStatus Status => UserStatus.Unknown;

public User(DiscordClient discord, Model model)
public User(Model model)
: base(model.Id)
{
Discord = discord;
Update(model, UpdateSource.Creation);
}
public virtual void Update(Model model, UpdateSource source)
@@ -39,14 +39,6 @@ namespace Discord
Username = model.Username;
}

public async Task<IDMChannel> CreateDMChannelAsync()
{
var args = new CreateDMChannelParams { RecipientId = Id };
var model = await Discord.ApiClient.CreateDMChannelAsync(args).ConfigureAwait(false);

return new DMChannel(Discord, this, model);
}

public override string ToString() => $"{Username}#{Discriminator}";
private string DebuggerDisplay => $"{Username}#{Discriminator} ({Id})";
}


+ 3
- 3
src/Discord.Net/Entities/WebSocket/CachedGuildUser.cs View File

@@ -5,14 +5,14 @@ namespace Discord
{
internal class CachedGuildUser : GuildUser, ICachedUser
{
private Game? _game;
private Game _game;
private UserStatus _status;

public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public new CachedGuild Guild => base.Guild as CachedGuild;
public new CachedPublicUser User => base.User as CachedPublicUser;

public override Game? Game => _game;
public override Game Game => _game;
public override UserStatus Status => _status;

public VoiceState? VoiceState => Guild.GetVoiceState(Id);
@@ -35,7 +35,7 @@ namespace Discord
base.Update(model, source);

_status = model.Status;
_game = model.Game != null ? new Game(model.Game) : (Game?)null;
_game = model.Game != null ? new Game(model.Game) : (Game)null;
}

public CachedGuildUser Clone() => MemberwiseClone() as CachedGuildUser;


+ 14
- 15
src/Discord.Net/Entities/WebSocket/CachedPublicUser.cs View File

@@ -1,5 +1,4 @@
using Discord.Data;
using ChannelModel = Discord.API.Channel;
using ChannelModel = Discord.API.Channel;
using Model = Discord.API.User;
using PresenceModel = Discord.API.Presence;

@@ -8,25 +7,25 @@ namespace Discord
internal class CachedPublicUser : User, ICachedUser
{
private int _references;
private Game? _game;
private UserStatus _status;
//private Game? _game;
//private UserStatus _status;

public CachedDMChannel DMChannel { get; private set; }

public new DiscordSocketClient Discord => base.Discord as DiscordSocketClient;
public override UserStatus Status => _status;
public override Game? Game => _game;
public override UserStatus Status => UserStatus.Unknown;// _status;
public override Game Game => null; //_game;

public CachedPublicUser(DiscordSocketClient discord, Model model)
: base(discord, model)
public CachedPublicUser(Model model)
: base(model)
{
}

public CachedDMChannel AddDMChannel(ChannelModel model)
public CachedDMChannel AddDMChannel(DiscordSocketClient discord, ChannelModel model)
{
lock (this)
{
var channel = new CachedDMChannel(Discord, this, model);
var channel = new CachedDMChannel(discord, this, model);
DMChannel = channel;
return channel;
}
@@ -49,10 +48,10 @@ namespace Discord
{
if (source == UpdateSource.Rest) return;

var game = model.Game != null ? new Game(model.Game) : (Game?)null;
//var game = model.Game != null ? new Game(model.Game) : (Game)null;

_status = model.Status;
_game = game;
//_status = model.Status;
//_game = game;
}

public void AddRef()
@@ -60,12 +59,12 @@ namespace Discord
lock (this)
_references++;
}
public void RemoveRef()
public void RemoveRef(DiscordSocketClient discord)
{
lock (this)
{
if (--_references == 0 && DMChannel == null)
Discord.RemoveUser(Id);
discord.RemoveUser(Id);
}
}



+ 2
- 2
src/Discord.Net/Entities/WebSocket/MessageCache.cs View File

@@ -88,7 +88,7 @@ namespace Discord
return msg;
var model = await _discord.ApiClient.GetChannelMessageAsync(_channel.Id, id).ConfigureAwait(false);
if (model != null)
return new CachedMessage(_channel, new User(_discord, model.Author.Value), model);
return new CachedMessage(_channel, new User(model.Author.Value), model);
return null;
}
public async Task<IReadOnlyCollection<CachedMessage>> DownloadAsync(ulong? fromId, Direction dir, int limit)
@@ -123,7 +123,7 @@ namespace Discord
IUser user = _channel.GetUser(x.Author.Value.Id, true);
if (user == null)
{
var newUser = new User(_channel.Discord, x.Author.Value);
var newUser = new User(x.Author.Value);
if (guild != null)
user = new GuildUser(guild, newUser);
else


+ 27
- 0
src/Discord.Net/Format.cs View File

@@ -0,0 +1,27 @@
namespace Discord
{
namespace Discord
{
public static class Format
{
/// <summary> Returns a markdown-formatted string with bold formatting. </summary>
public static string Bold(string text) => $"**{text}**";
/// <summary> Returns a markdown-formatted string with italics formatting. </summary>
public static string Italics(string text) => $"*{text}*";
/// <summary> Returns a markdown-formatted string with underline formatting. </summary>
public static string Underline(string text) => $"__{text}__";
/// <summary> Returns a markdown-formatted string with strikethrough formatting. </summary>
public static string Strikethrough(string text) => $"~~{text}~~";

/// <summary> Returns a markdown-formatted string with strikeout formatting. </summary>
public static string Code(string text, string language = null)
{
if (language != null || text.Contains("\n"))
return $"```{language ?? ""}\n{text}\n```";
else
return $"`{text}`";
}
}
}

}

Loading…
Cancel
Save