Skip to content

Blacklist streamer module #161

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 18 commits into from
Jan 17, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
46 changes: 30 additions & 16 deletions src/Volvox.Helios.Core/Modules/Streamer/StreamerModule.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@
using Volvox.Helios.Core.Bot;
using Volvox.Helios.Core.Modules.Common;
using Volvox.Helios.Core.Modules.StreamAnnouncer;

using Volvox.Helios.Core.Utilities;
using Volvox.Helios.Domain.Module;
using Volvox.Helios.Domain.ModuleSettings;
Expand All @@ -20,7 +19,7 @@
namespace Volvox.Helios.Core.Modules.Streamer
{
/// <summary>
/// Announce the user to a specified channel when the user starts streaming and assign specificed streaming role to the
/// Announce the user to a specified channel when the user starts streaming and assign specified streaming role to the
/// user.
/// </summary>
public class StreamerModule : Module
Expand All @@ -29,7 +28,7 @@ public class StreamerModule : Module
private readonly IModuleSettingsService<StreamerSettings> _settingsService;

/// <summary>
/// Announce the user to a specified channel when the user starts streaming and assign specificed streaming role to the
/// Announce the user to a specified channel when the user starts streaming and assign specified streaming role to the
/// user.
/// </summary>
/// <param name="discordSettings">Settings used to connect to Discord.</param>
Expand All @@ -39,12 +38,10 @@ public class StreamerModule : Module
/// <param name="scopeFactory">Scope factory.</param>
public StreamerModule(IDiscordSettings discordSettings, ILogger<StreamerModule> logger,
IConfiguration config, IModuleSettingsService<StreamerSettings> settingsService,
IServiceScopeFactory scopeFactory
) : base(
IServiceScopeFactory scopeFactory) : base(
discordSettings, logger, config)
{
_settingsService = settingsService;

_scopeFactory = scopeFactory;
}

Expand Down Expand Up @@ -88,7 +85,8 @@ public override async Task Init(DiscordSocketClient client)
// Subscribe to the GuildMemberUpdated event.
client.GuildMemberUpdated += async (user, guildUser) =>
{
var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id, x => x.ChannelSettings);
var settings = await _settingsService.GetSettingsByGuild(guildUser.Guild.Id, x => x.ChannelSettings,
x => x.WhiteListedRoleIds);

if (settings != null && settings.Enabled)
try
Expand All @@ -99,7 +97,7 @@ public override async Task Init(DiscordSocketClient client)

// Stream Announcer
if (settings.ChannelSettings != null)
await CheckUser(guildUser, settings.ChannelSettings);
await CheckUser(guildUser, settings.ChannelSettings, settings);
}
catch (Exception e)
{
Expand All @@ -121,8 +119,6 @@ private async Task HandleStreamerRole(SocketGuildUser guildUser, StreamerSetting
Logger.LogError("Streamer Module: Role could not be found!");
}
else
{
//Ensure bot has necessary permissions to add/remove specified role.
using (var scope = _scopeFactory.CreateScope())
{
var botService = scope.ServiceProvider.GetRequiredService<IBot>();
Expand All @@ -132,7 +128,8 @@ private async Task HandleStreamerRole(SocketGuildUser guildUser, StreamerSetting
if (streamingRole.Position < botRolePosition)
{
// Add use to role.
if (guildUser.Activity != null && guildUser.Activity.Type == ActivityType.Streaming)
if (guildUser.Activity != null && guildUser.Activity.Type == ActivityType.Streaming &&
IsUserWhiteListed(settings, guildUser))
await AddUserToStreamingRole(guildUser, streamingRole);

// Remove user from role.
Expand All @@ -141,10 +138,12 @@ private async Task HandleStreamerRole(SocketGuildUser guildUser, StreamerSetting
}
else
{
Logger.LogError($"Streamer Module: Could not add/remove role as bot has insufficient hierarchical position. " +
Logger.LogError(
$"Streamer Module: Could not add/remove role as bot has insufficient hierarchical position. " +
$"Guild Id: {guildUser.Guild.Id}");

await botService.GetGuild(guildUser.Guild.Id).Owner.SendMessageAsync($"We couldn't add/remove Role '{streamingRole.Name}' to a user as the role's hierarchical position is greater than the bot's.{Environment.NewLine}" +
await botService.GetGuild(guildUser.Guild.Id).Owner.SendMessageAsync(
$"We couldn't add/remove Role '{streamingRole.Name}' to a user as the role's hierarchical position is greater than the bot's.{Environment.NewLine}" +
$"To fix this, please adjust your role's position and then re-enable the module via our website.{Environment.NewLine}" +
$"Guild: {guildUser.Guild.Name}");

Expand All @@ -155,15 +154,16 @@ await botService.GetGuild(guildUser.Guild.Id).Owner.SendMessageAsync($"We couldn
await _settingsService.SaveSettings(settingsDb);
}
}
}
}

/// <summary>
/// Announces the user if it's appropriate to do so.
/// </summary>
/// <param name="user">User to be evaluated/adjusted for streaming announcement.</param>
/// <param name="channels">List of channels with module enabled</param>
private async Task CheckUser(SocketGuildUser user, List<StreamerChannelSettings> channels)
/// <param name="settings">Streamer settings for specified guild.</param>
private async Task CheckUser(SocketGuildUser user, List<StreamerChannelSettings> channels,
StreamerSettings settings)
{
// Add initial hash set for the guild.
if (!StreamingList.TryGetValue(user.Guild.Id, out var set))
Expand All @@ -176,7 +176,8 @@ private async Task CheckUser(SocketGuildUser user, List<StreamerChannelSettings>
if (user.Activity != null && user.Activity.Type == ActivityType.Streaming)
{
// If the user is not in the streaming list, they just started streaming. So, handle announcement.
if (!StreamingList.Any(u => u.Key == user.Guild.Id && u.Value.Any(x => x.UserId == user.Id)))
if (!StreamingList.Any(u => u.Key == user.Guild.Id && u.Value.Any(x => x.UserId == user.Id)) &&
IsUserWhiteListed(settings, user))
await AnnounceUserHandler(user, channels);

// Else, the user is already streaming and already has an announcement message.
Expand Down Expand Up @@ -363,5 +364,18 @@ private async Task RemoveUserFromStreamingRole(IGuildUser guildUser, IRole strea
Logger.LogDebug($"Streamer Module: Removing {guildUser.Username} from role {streamingRole.Name}. " +
$"Guild ID: {guildUser.GuildId}, User ID: {guildUser.Id}.");
}

/// <summary>
/// Checks if a user is part of the white listed roles.
/// </summary>
/// <param name="settings">Streamer settings for specified guild.</param>
/// <param name="guildUser">User to check.</param>
/// <returns>True if the user is a part of the white listed roles; otherwise false.</returns>
private static bool IsUserWhiteListed(StreamerSettings settings, SocketGuildUser guildUser)
{
var whiteListedRoles = settings.WhiteListedRoleIds.Select(w => w.RoleId).ToList();

return !whiteListedRoles.Any() || guildUser.Roles.Any(r => whiteListedRoles.Contains(r.Id));
}
}
}
12 changes: 12 additions & 0 deletions src/Volvox.Helios.Domain/Module/WhiteListedRole.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.ComponentModel.DataAnnotations;

namespace Volvox.Helios.Domain.Module
{
public class WhiteListedRole
{
[Key]
public ulong RoleId { get; set; }

public ulong GuildId { get; set; }
}
}
2 changes: 2 additions & 0 deletions src/Volvox.Helios.Domain/ModuleSettings/StreamerSettings.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,7 @@ public class StreamerSettings : ModuleSettings
public bool StreamerRoleEnabled { get; set; }

public ulong RoleId { get; set; }

public List<WhiteListedRole> WhiteListedRoleIds { get; set; }
}
}
Loading