Skip to content

Commit

Permalink
[Feature] Add UseExternalApps permission (#2941)
Browse files Browse the repository at this point in the history
  • Loading branch information
Misha-133 authored Jun 10, 2024
1 parent 51f59bf commit f7f29d5
Show file tree
Hide file tree
Showing 6 changed files with 44 additions and 16 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -197,5 +197,10 @@ public enum ChannelPermission : ulong
/// Allows sending polls.
/// </summary>
SendPolls = 1L << 49,

/// <summary>
/// Allows user-installed apps to send public responses.
/// </summary>
UseExternalApps = 1L << 50,
}
}
23 changes: 15 additions & 8 deletions src/Discord.Net.Core/Entities/Permissions/ChannelPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -18,17 +18,17 @@ public struct ChannelPermissions
/// <summary>
/// Gets a <see cref="ChannelPermissions"/> that grants all permissions for text channels.
/// </summary>
public static readonly ChannelPermissions Text = new(0b10_110001_001111_110010_110011_111101_111111_111101_010001);
public static readonly ChannelPermissions Text = new(0b110_110001_001111_110010_110011_111101_111111_111101_010001);

/// <summary>
/// Gets a <see cref="ChannelPermissions"/> that grants all permissions for voice channels.
/// </summary>
public static readonly ChannelPermissions Voice = new(0b11_110001_001010_001010_110011_111101_111111_111101_010001);
public static readonly ChannelPermissions Voice = new(0b111_110001_001010_001010_110011_111101_111111_111101_010001);

/// <summary>
/// Gets a <see cref="ChannelPermissions"/> that grants all permissions for stage channels.
/// </summary>
public static readonly ChannelPermissions Stage = new(0b10_110000_000010_001110_010001_010101_111111_111001_010001);
public static readonly ChannelPermissions Stage = new(0b110_110000_000010_001110_010001_010101_111111_111001_010001);

/// <summary>
/// Gets a <see cref="ChannelPermissions"/> that grants all permissions for category channels.
Expand Down Expand Up @@ -154,6 +154,8 @@ public static ChannelPermissions All(IChannel channel)
public bool SetVoiceChannelStatus => Permissions.GetValue(RawValue, ChannelPermission.SetVoiceChannelStatus);
/// <summary> If <see langword="true"/>, a user can send polls.</summary>
public bool SendPolls => Permissions.GetValue(RawValue, ChannelPermission.SendPolls);
/// <summary> If <see langword="true"/>, a user-installed application can send public responses.</summary>
public bool UserExternalApps => Permissions.GetValue(RawValue, ChannelPermission.UseExternalApps);

/// <summary> Creates a new <see cref="ChannelPermissions"/> with the provided packed value.</summary>
public ChannelPermissions(ulong rawValue) { RawValue = rawValue; }
Expand Down Expand Up @@ -194,7 +196,8 @@ private ChannelPermissions(ulong initialValue,
bool? sendVoiceMessages = null,
bool? useClydeAI = null,
bool? setVoiceChannelStatus = null,
bool? sendPolls = null)
bool? sendPolls = null,
bool? useExternalApps = null)
{
ulong value = initialValue;

Expand Down Expand Up @@ -234,6 +237,7 @@ private ChannelPermissions(ulong initialValue,
Permissions.SetValue(ref value, useClydeAI, ChannelPermission.UseClydeAI);
Permissions.SetValue(ref value, setVoiceChannelStatus, ChannelPermission.SetVoiceChannelStatus);
Permissions.SetValue(ref value, sendPolls, ChannelPermission.SendPolls);
Permissions.SetValue(ref value, useExternalApps, ChannelPermission.UseExternalApps);

RawValue = value;
}
Expand Down Expand Up @@ -275,12 +279,13 @@ public ChannelPermissions(
bool sendVoiceMessages = false,
bool useClydeAI = false,
bool setVoiceChannelStatus = false,
bool sendPolls = false)
bool sendPolls = false,
bool useExternalApps = false)
: this(0, createInstantInvite, manageChannel, addReactions, viewChannel, sendMessages, sendTTSMessages, manageMessages,
embedLinks, attachFiles, readMessageHistory, mentionEveryone, useExternalEmojis, connect,
speak, muteMembers, deafenMembers, moveMembers, useVoiceActivation, prioritySpeaker, stream, manageRoles, manageWebhooks,
useApplicationCommands, requestToSpeak, manageThreads, createPublicThreads, createPrivateThreads, useExternalStickers, sendMessagesInThreads,
startEmbeddedActivities, useSoundboard, createEvents, sendVoiceMessages, useClydeAI, setVoiceChannelStatus, sendPolls)
startEmbeddedActivities, useSoundboard, createEvents, sendVoiceMessages, useClydeAI, setVoiceChannelStatus, sendPolls, useExternalApps)
{ }

/// <summary> Creates a new <see cref="ChannelPermissions"/> from this one, changing the provided non-null permissions.</summary>
Expand Down Expand Up @@ -320,7 +325,8 @@ public ChannelPermissions Modify(
bool? sendVoiceMessages = null,
bool? useClydeAI = null,
bool? setVoiceChannelStatus = null,
bool? sendPolls = null)
bool? sendPolls = null,
bool? useExternalApps = null)
=> new ChannelPermissions(RawValue,
createInstantInvite,
manageChannel,
Expand Down Expand Up @@ -357,7 +363,8 @@ public ChannelPermissions Modify(
sendVoiceMessages,
useClydeAI,
setVoiceChannelStatus,
sendPolls);
sendPolls,
useExternalApps);

public bool Has(ChannelPermission permission) => Permissions.GetValue(RawValue, permission);

Expand Down
5 changes: 5 additions & 0 deletions src/Discord.Net.Core/Entities/Permissions/GuildPermission.cs
Original file line number Diff line number Diff line change
Expand Up @@ -287,5 +287,10 @@ public enum GuildPermission : ulong
/// Allows sending polls.
/// </summary>
SendPolls = 1L << 49,

/// <summary>
/// Allows user-installed apps to send public responses.
/// </summary>
UseExternalApps = 1L << 50,
}
}
17 changes: 12 additions & 5 deletions src/Discord.Net.Core/Entities/Permissions/GuildPermissions.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,8 @@ public struct GuildPermissions
public bool SetVoiceChannelStatus => Permissions.GetValue(RawValue, GuildPermission.SetVoiceChannelStatus);
/// <summary> If <see langword="true"/>, a user can send polls.</summary>
public bool SendPolls => Permissions.GetValue(RawValue, GuildPermission.SendPolls);
/// <summary> If <see langword="true"/>, a user-installed application can send public responses.</summary>
public bool UserExternalApps => Permissions.GetValue(RawValue, GuildPermission.UseExternalApps);


/// <summary> Creates a new <see cref="GuildPermissions"/> with the provided packed value. </summary>
Expand Down Expand Up @@ -174,7 +176,8 @@ private GuildPermissions(ulong initialValue,
bool? useClydeAI = null,
bool? createGuildExpressions = null,
bool? setVoiceChannelStatus = null,
bool? sendPolls = null)
bool? sendPolls = null,
bool? useExternalApps = null)
{
ulong value = initialValue;

Expand Down Expand Up @@ -226,6 +229,7 @@ private GuildPermissions(ulong initialValue,
Permissions.SetValue(ref value, createGuildExpressions, GuildPermission.CreateGuildExpressions);
Permissions.SetValue(ref value, setVoiceChannelStatus, GuildPermission.SetVoiceChannelStatus);
Permissions.SetValue(ref value, sendPolls, GuildPermission.SendPolls);
Permissions.SetValue(ref value, useExternalApps, GuildPermission.UseExternalApps);

RawValue = value;
}
Expand Down Expand Up @@ -279,7 +283,8 @@ public GuildPermissions(
bool useClydeAI = false,
bool createGuildExpressions = false,
bool setVoiceChannelStatus = false,
bool sendPolls = false)
bool sendPolls = false,
bool useExternalApps = false)
: this(0,
createInstantInvite: createInstantInvite,
manageRoles: manageRoles,
Expand Down Expand Up @@ -328,7 +333,8 @@ public GuildPermissions(
useClydeAI: useClydeAI,
createGuildExpressions: createGuildExpressions,
setVoiceChannelStatus: setVoiceChannelStatus,
sendPolls: sendPolls)
sendPolls: sendPolls,
useExternalApps: useExternalApps)
{ }

/// <summary> Creates a new <see cref="GuildPermissions"/> from this one, changing the provided non-null permissions. </summary>
Expand Down Expand Up @@ -380,14 +386,15 @@ public GuildPermissions Modify(
bool? useClydeAI = null,
bool? createGuildExpressions = null,
bool? setVoiceChannelStatus = null,
bool? sendPolls = null)
bool? sendPolls = null,
bool? useExternalApps = null)
=> new GuildPermissions(RawValue, createInstantInvite, kickMembers, banMembers, administrator, manageChannels, manageGuild, addReactions,
viewAuditLog, viewGuildInsights, viewChannel, sendMessages, sendTTSMessages, manageMessages, embedLinks, attachFiles,
readMessageHistory, mentionEveryone, useExternalEmojis, connect, speak, muteMembers, deafenMembers, moveMembers,
useVoiceActivation, prioritySpeaker, stream, changeNickname, manageNicknames, manageRoles, manageWebhooks, manageEmojisAndStickers,
useApplicationCommands, requestToSpeak, manageEvents, manageThreads, createPublicThreads, createPrivateThreads, useExternalStickers, sendMessagesInThreads,
startEmbeddedActivities, moderateMembers, useSoundboard, viewMonetizationAnalytics, sendVoiceMessages, useClydeAI, createGuildExpressions, setVoiceChannelStatus,
sendPolls);
sendPolls, useExternalApps);

/// <summary>
/// Returns a value that indicates if a specific <see cref="GuildPermission"/> is enabled
Expand Down
6 changes: 4 additions & 2 deletions test/Discord.Net.Tests.Unit/ChannelPermissionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ IEnumerable<ulong> GetTestValues()
}

/// <summary>
/// Tests the behavior of the <see cref="Discord.ChannelPermissions"/> constructor for each
/// Tests the behavior of the <see cref="ChannelPermissions"/> constructor for each
/// of it's flags.
/// </summary>
[Fact]
Expand Down Expand Up @@ -96,10 +96,11 @@ void AssertFlag(Func<ChannelPermissions> cstr, ChannelPermission flag)
AssertFlag(() => new ChannelPermissions(useClydeAI: true), ChannelPermission.UseClydeAI);
AssertFlag(() => new ChannelPermissions(setVoiceChannelStatus: true), ChannelPermission.SetVoiceChannelStatus);
AssertFlag(() => new ChannelPermissions(sendPolls: true), ChannelPermission.SendPolls);
AssertFlag(() => new ChannelPermissions(useExternalApps: true), ChannelPermission.UseExternalApps);
}

/// <summary>
/// Tests the behavior of <see cref="Discord.ChannelPermissions.Modify"/>
/// Tests the behavior of <see cref="ChannelPermissions.Modify"/>
/// with each of the parameters.
/// </summary>
[Fact]
Expand Down Expand Up @@ -164,6 +165,7 @@ void AssertUtil(ChannelPermission permission,
AssertUtil(ChannelPermission.UseClydeAI, x => x.UseClydeAI, (p, enable) => p.Modify(useClydeAI: enable));
AssertUtil(ChannelPermission.SetVoiceChannelStatus, x => x.SetVoiceChannelStatus, (p, enable) => p.Modify(setVoiceChannelStatus: enable));
AssertUtil(ChannelPermission.SendPolls, x => x.SendPolls, (p, enable) => p.Modify(sendPolls: enable));
AssertUtil(ChannelPermission.UseExternalApps, x => x.UserExternalApps, (p, enable) => p.Modify(useExternalApps: enable));
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion test/Discord.Net.Tests.Unit/GuildPermissionsTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -107,10 +107,11 @@ void AssertFlag(Func<GuildPermissions> cstr, GuildPermission flag)
AssertFlag(() => new GuildPermissions(createGuildExpressions: true), GuildPermission.CreateGuildExpressions);
AssertFlag(() => new GuildPermissions(setVoiceChannelStatus: true), GuildPermission.SetVoiceChannelStatus);
AssertFlag(() => new GuildPermissions(sendPolls: true), GuildPermission.SendPolls);
AssertFlag(() => new GuildPermissions(useExternalApps: true), GuildPermission.UseExternalApps);
}

/// <summary>
/// Tests the behavior of <see cref="Discord.GuildPermissions.Modify(bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?, bool?)"/>
/// Tests the behavior of <see cref="GuildPermissions.Modify"/>
/// with each of the parameters.
/// </summary>
[Fact]
Expand Down Expand Up @@ -192,6 +193,7 @@ void AssertUtil(GuildPermission permission,
AssertUtil(GuildPermission.CreateGuildExpressions, x => x.CreateGuildExpressions, (p, enable) => p.Modify(createGuildExpressions: enable));
AssertUtil(GuildPermission.SetVoiceChannelStatus, x => x.SetVoiceChannelStatus, (p, enable) => p.Modify(setVoiceChannelStatus: enable));
AssertUtil(GuildPermission.SendPolls, x => x.SendPolls, (p, enable) => p.Modify(sendPolls: enable));
AssertUtil(GuildPermission.UseExternalApps, x => x.UserExternalApps, (p, enable) => p.Modify(useExternalApps: enable));
}
}
}

0 comments on commit f7f29d5

Please sign in to comment.