Browse Source

Merge pull request #56 from Lirusaito/master

Support Role Mentions
pull/57/head
RogueException 9 years ago
parent
commit
f74a393581
5 changed files with 39 additions and 25 deletions
  1. +2
    -0
      src/Discord.Net/API/Client/Common/Role.cs
  2. +2
    -0
      src/Discord.Net/API/Client/Rest/UpdateRole.cs
  3. +24
    -20
      src/Discord.Net/Models/Message.cs
  4. +8
    -3
      src/Discord.Net/Models/Role.cs
  5. +3
    -2
      src/Discord.Net/Models/Server.cs

+ 2
- 0
src/Discord.Net/API/Client/Common/Role.cs View File

@@ -19,5 +19,7 @@ namespace Discord.API.Client
public uint? Color { get; set; }
[JsonProperty("managed")]
public bool? Managed { get; set; }
[JsonProperty("mentionable")]
public bool? Mentionable { get; set; }
}
}

+ 2
- 0
src/Discord.Net/API/Client/Rest/UpdateRole.cs View File

@@ -18,6 +18,8 @@ namespace Discord.API.Client.Rest
public uint Permissions { get; set; }
[JsonProperty("hoist")]
public bool IsHoisted { get; set; }
[JsonProperty("mentionable")]
public bool IsMentionable { get; set; }
[JsonProperty("color")]
public uint Color { get; set; }



+ 24
- 20
src/Discord.Net/Models/Message.cs View File

@@ -25,7 +25,7 @@ namespace Discord

private static readonly Regex _userRegex = new Regex(@"<@[0-9]+>");
private static readonly Regex _channelRegex = new Regex(@"<#[0-9]+>");
private static readonly Regex _roleRegex = new Regex(@"@everyone");
private static readonly Regex _roleRegex = new Regex(@"<@&[0-9]+>");
private static readonly Attachment[] _initialAttachments = new Attachment[0];
private static readonly Embed[] _initialEmbeds = new Embed[0];

@@ -68,28 +68,36 @@ namespace Discord
return e.Value; //Channel not found or parse failed
}));
}
/*internal static string CleanRoleMentions(User user, Channel channel, string text, List<Role> roles = null)
internal static string CleanRoleMentions(Channel channel, string text, List<Role> roles = null)
{
var server = channel.Server;
if (server == null) return text;

return _roleRegex.Replace(text, new MatchEvaluator(e =>
return _roleRegex.Replace(text, new MatchEvaluator(e =>
{
if (roles != null && user.GetPermissions(channel).MentionEveryone)
roles.Add(server.EveryoneRole);
return e.Value;
ulong id;
if (e.Value.Substring(3, e.Value.Length - 4).TryToId(out id))
{
var role = server.GetRole(id);
if (role != null)
{
if (roles != null)
roles.Add(role);
return "@" + role.Name;
}
}
return e.Value; //Role not found or parse failed
}));
}*/
}
//TODO: Move this somewhere
private static string Resolve(Channel channel, string text)
{
if (text == null) throw new ArgumentNullException(nameof(text));

var client = channel.Client;
text = CleanUserMentions(channel, text);
text = CleanChannelMentions(channel, text);
//text = CleanRoleMentions(Channel, text);
text = CleanRoleMentions(channel, text);
return text;
}

@@ -281,32 +289,28 @@ namespace Discord
.Where(x => x != null)
.ToArray();
}
if (model.IsMentioningEveryone != null)
{
if (model.IsMentioningEveryone.Value && User != null && User.GetPermissions(channel).MentionEveryone)
MentionedRoles = new Role[] { Server.EveryoneRole };
else
MentionedRoles = new Role[0];
}

if (model.Content != null)
{
string text = model.Content;
RawText = text;

//var mentionedUsers = new List<User>();
var mentionedChannels = new List<Channel>();
//var mentionedRoles = new List<Role>();
var mentionedRoles = new List<Role>();
text = CleanUserMentions(Channel, text/*, mentionedUsers*/);
if (server != null)
{
text = CleanChannelMentions(Channel, text, mentionedChannels);
//text = CleanRoleMentions(_client, User, channel, text, mentionedRoles);
text = CleanRoleMentions(Channel, text, mentionedRoles);
if (model.IsMentioningEveryone != null && model.IsMentioningEveryone.Value
&& User != null && User.GetPermissions(channel).MentionEveryone)
mentionedRoles.Add(Server.EveryoneRole);
}
Text = text;

//MentionedUsers = mentionedUsers;
MentionedChannels = mentionedChannels;
//MentionedRoles = mentionedRoles;
MentionedRoles = mentionedRoles;
}
}



+ 8
- 3
src/Discord.Net/Models/Role.cs View File

@@ -28,6 +28,8 @@ namespace Discord
public int Position { get; private set; }
/// <summary> Gets whether this role is managed by server (e.g. for Twitch integration) </summary>
public bool IsManaged { get; private set; }
/// <summary> Gets whether this role is mentionable by anyone. </summary>
public bool IsMentionable { get; private set; }
/// <summary> Gets the the permissions given to this role. </summary>
public ServerPermissions Permissions { get; private set; }
/// <summary> Gets the color of this role. </summary>
@@ -41,7 +43,7 @@ namespace Discord
public IEnumerable<User> Members => IsEveryone ? Server.Users : Server.Users.Where(x => x.HasRole(this));

/// <summary> Gets the string used to mention this role. </summary>
public string Mention => IsEveryone ? "@everyone" : "";
public string Mention => IsEveryone ? "@everyone" : IsMentionable ? $"<@&{Id}>" : "";

internal Role(ulong id, Server server)
{
@@ -60,6 +62,8 @@ namespace Discord
IsHoisted = model.Hoist.Value;
if (model.Managed != null)
IsManaged = model.Managed.Value;
if (model.Mentionable != null)
IsMentionable = model.Mentionable.Value;
if (model.Position != null && !IsEveryone)
Position = model.Position.Value;
if (model.Color != null)
@@ -75,14 +79,15 @@ namespace Discord
}
}
public async Task Edit(string name = null, ServerPermissions? permissions = null, Color color = null, bool? isHoisted = null, int? position = null)
public async Task Edit(string name = null, ServerPermissions? permissions = null, Color color = null, bool? isHoisted = null, int? position = null, bool? isMentionable = null)
{
var updateRequest = new UpdateRoleRequest(Server.Id, Id)
{
Name = name ?? Name,
Permissions = (permissions ?? Permissions).RawValue,
Color = (color ?? Color).RawValue,
IsHoisted = isHoisted ?? IsHoisted
IsHoisted = isHoisted ?? IsHoisted,
IsMentionable = isMentionable ?? IsMentionable
};

var updateResponse = await Client.ClientAPI.Send(updateRequest).ConfigureAwait(false);


+ 3
- 2
src/Discord.Net/Models/Server.cs View File

@@ -366,7 +366,7 @@ namespace Discord
}

/// <summary> Creates a new role. </summary>
public async Task<Role> CreateRole(string name, ServerPermissions? permissions = null, Color color = null, bool isHoisted = false)
public async Task<Role> CreateRole(string name, ServerPermissions? permissions = null, Color color = null, bool isHoisted = false, bool isMentionable = false)
{
if (name == null) throw new ArgumentNullException(nameof(name));

@@ -380,7 +380,8 @@ namespace Discord
Name = name,
Permissions = (permissions ?? role.Permissions).RawValue,
Color = (color ?? Color.Default).RawValue,
IsHoisted = isHoisted
IsHoisted = isHoisted,
IsMentionable = isMentionable
};
var editResponse = await Client.ClientAPI.Send(editRequest).ConfigureAwait(false);
role.Update(editResponse, true);


Loading…
Cancel
Save