Exemplo n.º 1
0
    async Task IRoleRemover.RemoveBasementRolesAsync()
    {
        var basementRoleId = (DiscordRoleId)_dynamicConfiguration.DiscordMapping["BasementRoleId"];

        // If there are any users from the last check, free them this round.
        foreach (var discordUserID in _usersToFreeFromBasement)
        {
            if (!_discordAccess.CanManageRolesForUser(discordUserID))
            {
                continue;
            }

            var(success, roleName) = await _discordAccess.TryRevokeNonMemberRole(discordUserID, basementRoleId);

            if (success)
            {
                await _discordAccess.LogToDiscord($"Automatically removed role `{roleName}` from <@{discordUserID}>.");

                continue;
            }

            var leaderMention = _discordAccess.GetRoleMention("Leader");
            await _discordAccess.LogToDiscord($"{leaderMention}: failed to remove role `{roleName}` from <@{discordUserID}>");
        }

        // Gather users that should be freed next round.
        _usersToFreeFromBasement.Clear();
        var usersInBasement = _discordAccess.GetUsersIdsInRole(basementRoleId);

        if (usersInBasement.Any())
        {
            _usersToFreeFromBasement.AddRange(usersInBasement);
        }
    }
Exemplo n.º 2
0
    public async Task ApplyRoleAsync()
    {
        var birthdayRoleId = (DiscordRoleId)_dynamicConfiguration.DiscordMapping[BirthdayRoleIdKey];
        var birthdayUsers  = await _birthdayProvider.GetBirthdaysAsync(DateOnly.FromDateTime(DateTime.UtcNow));

        var userIdsWithAppliedRole = new List <DiscordUserId>(birthdayUsers.Length);

        foreach (var userId in birthdayUsers)
        {
            if (!_discordAccess.CanManageRolesForUser(userId))
            {
                continue;
            }

            var(success, _) = await _discordAccess.TryAddNonMemberRole(userId, birthdayRoleId);

            if (success)
            {
                userIdsWithAppliedRole.Add(userId);
            }
            else
            {
                await _discordAccess.LogToDiscord($"{_discordAccess.GetLeadershipMention()}: "
                                                  + $"Failed to apply role {birthdayRoleId.ToMention()} to {userId.ToMention()}.");
            }
        }

        if (userIdsWithAppliedRole.Count == 0)
        {
            return;
        }

        var birthdayAnnouncementChannelId = (DiscordChannelId)_dynamicConfiguration.DiscordMapping[BirthdayAnnouncementChannelIdKey];
        var messages = userIdsWithAppliedRole.Select(m => "HoU wishes a Happy Birthday to our latest Birthday Yata, "
                                                     + $"{m.ToMention()}. Have an amazing day filled with fun!!!")
                       .ToArray();
        await _discordAccess.CreateBotMessagesInChannelAsync(birthdayAnnouncementChannelId, messages);
    }