예제 #1
0
    public async Task <IResult> AddBanAsync
    (
        IUser user,
        string reason,
        TimeSpan?expiresAfter = null
    )
    {
        DateTimeOffset?expiresOn = null;

        if (expiresAfter is not null)
        {
            expiresOn = DateTimeOffset.UtcNow.Add(expiresAfter.Value);
        }

        var createBan = await _bans.CreateBanAsync
                        (
            _context.User.ID,
            user.ID,
            _context.GuildID.Value,
            reason,
            expiresOn : expiresOn
                        );

        if (!createBan.IsSuccess)
        {
            return(createBan);
        }

        var ban = createBan.Entity;

        var notifyResult = await _logging.NotifyUserBannedAsync(ban);

        if (!notifyResult.IsSuccess)
        {
            return(notifyResult);
        }

        return(await _guildAPI.CreateGuildBanAsync(_context.GuildID.Value, user.ID, reason : reason));
    }
예제 #2
0
            /// <inheritdoc />
            public async Task <Result <ModerationAction> > Handle(Command request, CancellationToken cancellationToken)
            {
                var banModerationAction = new ModerationAction
                                          (
                    moderationActionType: ModerationActionType.Ban,
                    guildId: request.GuildId.Value,
                    isActive: true,
                    reason: request.Reason,
                    date: DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(),
                    userDiscordId: request.UserDiscordId?.Value,
                    userIgn: request.UserIgn,
                    expiryDate: request.ExpiryDate
                                          );

                if (request.UserIgn is not null)
                {
                    var proto = new GenericCommand
                    {
                        DefaultCommand     = "ban",
                        DiscordCommandName = "ban",
                        DiscordChannelId   = request.ChannelId.Value.ToString(),
                        Args = { request.UserIgn }
                    };
                    await _ps.BroadcastMessage(proto);
                }

                if (request.UserDiscordId is not null)
                {
                    var guildResult = await _guildApi.GetGuildAsync(request.GuildId, ct : cancellationToken);

                    if (!guildResult.IsSuccess)
                    {
                        return(Result <ModerationAction> .FromError(guildResult.Error));
                    }

                    var banResult = await _guildApi.CreateGuildBanAsync(
                        request.GuildId,
                        request.UserDiscordId.Value,
                        reason : request.Reason,
                        ct : cancellationToken
                        );

                    if (!banResult.IsSuccess)
                    {
                        return(Result <ModerationAction> .FromError(banResult.Error));
                    }

                    var guildName = guildResult.Entity.Name;
                    var embed     = new Embed
                    {
                        Title     = $"You have been banned from {guildName}.",
                        Colour    = _colourPalette.Red,
                        Thumbnail = EmbedProperties.MmccLogoThumbnail,
                        Timestamp = DateTimeOffset.UtcNow,
                        Fields    = new List <EmbedField>
                        {
                            new("Reason", request.Reason, false),
                            new(
                                "Expires at",
                                request.ExpiryDate is null
                                    ? "Permanent"
                                    : $"{DateTimeOffset.FromUnixTimeMilliseconds(request.ExpiryDate.Value).UtcDateTime} UTC"
                                ,
                                false
                                ),
                            new(
                                "Appeals",
                                $"You can appeal this decision **[here]({_discordSettings.AppealsUrl})**."
                                )
                        }
                    };

                    var createDmResult = await _userApi.CreateDMAsync(request.UserDiscordId.Value, cancellationToken);

                    const string errMsg =
                        "Failed to send a DM notification to the user. It may be because they have blocked the bot. This error can in most cases be ignored.";
                    if (!createDmResult.IsSuccess || createDmResult.Entity is null)
                    {
                        _logger.LogWarning(errMsg);
                    }
                    else
                    {
                        var sendDmResult = await _channelApi.CreateMessageAsync(createDmResult.Entity.ID,
                                                                                embeds : new[] { embed },
                                                                                ct : cancellationToken);

                        if (!sendDmResult.IsSuccess)
                        {
                            _logger.LogWarning(errMsg);
                        }
                    }
                }

                try
                {
                    await _context.AddAsync(banModerationAction, cancellationToken);

                    await _context.SaveChangesAsync(cancellationToken);
                }
                catch (Exception e)
                {
                    return(e);
                }

                return(banModerationAction);
            }