Skip to content

Commit

Permalink
Merge pull request #85 from alopezlago/alopezlago/public_3_8_1
Browse files Browse the repository at this point in the history
Merged PR 160: v3.8.1 - Bugfix for teams appearing in games when they had no permissions to it
  • Loading branch information
alopezlago authored Feb 13, 2021
2 parents 36aae7c + 198161d commit 64c173e
Show file tree
Hide file tree
Showing 7 changed files with 330 additions and 30 deletions.
7 changes: 6 additions & 1 deletion QuizBowlDiscordScoreTracker/Commands/AdminCommandHandler.cs
Original file line number Diff line number Diff line change
Expand Up @@ -356,6 +356,11 @@ public async Task UnpairChannelAsync(ITextChannel textChannel)
Justification = "Discord.Net can't parse the argument directly as a URI")]
private async Task SetRostersFromRolesForSheets(string sheetsUrl, GoogleSheetsType type)
{
if (!(this.Context.Channel is IGuildChannel guildChannel))
{
return;
}

if (!Uri.TryCreate(sheetsUrl, UriKind.Absolute, out Uri sheetsUri))
{
await this.Context.Channel.SendMessageAsync(
Expand All @@ -379,7 +384,7 @@ await this.Context.Channel.SendMessageAsync(
return;
}

ITeamManager teamManager = new ByRoleTeamManager(this.Context.Guild, teamRolePrefix);
ITeamManager teamManager = new ByRoleTeamManager(guildChannel, teamRolePrefix);
IGoogleSheetsGenerator generator = this.GoogleSheetsGeneratorFactory.Create(type);
IResult<string> result = await generator.TryUpdateRosters(teamManager, sheetsUri);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,6 +244,10 @@ await this.Context.Channel.SendMessageAsync(
Logger.Information(
"Game started in guild '{0}' in channel '{1}'", guildChannel.Guild.Name, guildChannel.Name);
}
else
{
return;
}

// Prevent a cold start on the first buzz, and eagerly get the team prefix and channel pair
string teamRolePrefix;
Expand All @@ -264,7 +268,7 @@ await this.Context.Channel.SendMessageAsync(
// Set teams here, if they are using roles.
if (teamRolePrefix != null)
{
state.TeamManager = new ByRoleTeamManager(this.Context.Guild, teamRolePrefix);
state.TeamManager = new ByRoleTeamManager(guildChannel, teamRolePrefix);
}
else
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>netcoreapp3.1</TargetFramework>
<Version>3.8.0</Version>
<Version>3.8.1</Version>
<Authors>Alejandro Lopez-Lago</Authors>
<Company />
<Product>Quiz Bowl Discord Score Tracker</Product>
Expand Down
35 changes: 33 additions & 2 deletions QuizBowlDiscordScoreTracker/TeamManager/ByRoleTeamManager.cs
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,12 @@ public class ByRoleTeamManager : ITeamManager, IByRoleTeamManager
{
private readonly object teamIdToNameLock = new object();

public ByRoleTeamManager(IGuild guild, string teamRolePrefix)
public ByRoleTeamManager(IGuildChannel channel, string teamRolePrefix)
{
this.Guild = guild;
Verify.IsNotNull(channel, nameof(channel));

this.Guild = channel.Guild;
this.Channel = channel;
this.TeamRolePrefix = teamRolePrefix;
this.InitiailzeTeamIdToName();
}
Expand All @@ -24,6 +27,8 @@ public ByRoleTeamManager(IGuild guild, string teamRolePrefix)

private IGuild Guild { get; }

private IGuildChannel Channel { get; }

private string TeamRolePrefix { get; }

private IDictionary<string, string> TeamIdToName { get; set; }
Expand Down Expand Up @@ -73,8 +78,34 @@ private void InitiailzeTeamIdToName()
{
lock (this.teamIdToNameLock)
{
OverwritePermissions? everyonePermissions = this.Channel.GetPermissionOverwrite(this.Guild.EveryoneRole);
PermValue everyoneViewPermissions = everyonePermissions?.ViewChannel ?? PermValue.Inherit;
PermValue everyoneSendPermissions = everyonePermissions?.SendMessages ?? PermValue.Inherit;

this.TeamIdToName = this.Guild.Roles
.Where(role => role.Name.StartsWith(this.TeamRolePrefix, StringComparison.InvariantCultureIgnoreCase))
.Where(role =>
{
// Players need both View and Send permissions to play, so make sure either the role or the
// everyone role has it
OverwritePermissions? permissions = this.Channel.GetPermissionOverwrite(role);
if (!permissions.HasValue)
{
// No specific permissions, so inherit it from everyone
return everyonePermissions?.ViewChannel != PermValue.Deny &&
everyonePermissions?.SendMessages != PermValue.Deny;
}

OverwritePermissions permissionsValue = permissions.Value;
PermValue viewPermissions = permissionsValue.ViewChannel != PermValue.Inherit ?
permissionsValue.ViewChannel :
everyoneViewPermissions;
PermValue sendPermissions = permissionsValue.SendMessages != PermValue.Inherit ?
permissionsValue.SendMessages :
everyoneSendPermissions;

return viewPermissions != PermValue.Deny && sendPermissions != PermValue.Deny;
})
.ToDictionary(
role => role.Id.ToString(CultureInfo.InvariantCulture),
role => role.Name.Substring(this.TeamRolePrefix.Length).Trim());
Expand Down
Loading

0 comments on commit 64c173e

Please sign in to comment.