public async Task <Result> Handle(Command request, CancellationToken cancellationToken) { try { var msg = new GenericCommand { DiscordCommandName = "exec", DefaultCommand = "$args", Args = { request.McCmdArgs }, DiscordChannelId = request.ChannelId.ToString() }; if (request.ServerId.Equals("<all>")) { await _polychatService.BroadcastMessage(msg); return(Result.FromSuccess()); } var server = _polychatService.GetOnlineServerOrDefault(request.ServerId.ToUpperInvariant()); if (server is null) { return(new NotFoundError($"Could not find server with ID: {request.ServerId}")); } await _polychatService.SendMessage(server, msg); return(Result.FromSuccess()); } catch (Exception e) { return(e); } }
public async Task <Result> RespondAsync(IMessageCreate ev, CancellationToken ct) { if (ev.Author.IsBot.HasValue && ev.Author.IsBot.Value || ev.Author.IsSystem.HasValue && ev.Author.IsSystem.Value || !ev.GuildID.HasValue || ev.ChannelID.Value != _polychatSettings.ChatChannelId || ev.Content.StartsWith(_discordSettings.Prefix) ) { return(Result.FromSuccess()); } var sanitisedMsgContent = await _sanitiser.SanitiseMessageContent(ev); var protoMsgContent = $"§9[Discord] §7{ev.Author.Username}§r: {sanitisedMsgContent}"; var protoMsg = new ChatMessage { ServerId = "Discord", Message = protoMsgContent, MessageOffset = protoMsgContent.IndexOf(':') }; await _polychatService.BroadcastMessage(protoMsg); return(Result.FromSuccess()); }
/// <inheritdoc /> public async Task <Result <ModerationAction> > Handle(Command request, CancellationToken cancellationToken) { var ma = request.ModerationAction; if (ma.ModerationActionType != ModerationActionType.Ban) { return(new UnsupportedArgumentError( $"Wrong moderation action type. Expected: {ModerationActionType.Ban}, got: {ma.ModerationActionType}")); } //if (!ma.IsActive) return new ValidationError("Moderation action is already inactive."); if (ma.UserIgn is not null) { var proto = new GenericCommand { DefaultCommand = "ban", DiscordCommandName = "ban", DiscordChannelId = request.ChannelId.ToString(), Args = { request.ModerationAction.UserIgn } }; await _ps.BroadcastMessage(proto); } if (request.ModerationAction.UserDiscordId is not null) { var userDiscordIdSnowflake = new Snowflake(request.ModerationAction.UserDiscordId.Value); var banResult = await _guildApi.RemoveGuildBanAsync( new(request.ModerationAction.GuildId), new(request.ModerationAction.UserDiscordId.Value), new(), cancellationToken ); if (!banResult.IsSuccess) { return(Result <ModerationAction> .FromError(banResult.Error)); } var embed = new Embed { Title = "You have been unbanned from Modded Minecraft Club.", Colour = _colourPalette.Green, Thumbnail = EmbedProperties.MmccLogoThumbnail }; var createDmResult = await _userApi.CreateDMAsync(userDiscordIdSnowflake, cancellationToken); const string warningMsg = "Failed to send a DM notification to the user. It may be because they have blocked the bot or don't share any servers. This warning can in most cases be ignored."; if (!createDmResult.IsSuccess || createDmResult.Entity is null) { _logger.LogWarning(warningMsg); } else { var sendDmResult = await _channelApi.CreateMessageAsync(createDmResult.Entity.ID, embeds : new[] { embed }, ct : cancellationToken); if (!sendDmResult.IsSuccess) { _logger.LogWarning(warningMsg); } } } try { ma.IsActive = false; ma.ExpiryDate = DateTimeOffset.Now.ToUnixTimeMilliseconds(); await _context.SaveChangesAsync(cancellationToken); } catch (Exception e) { return(e); } return(ma); }
/// <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); }
/// <inheritdoc /> public async Task <Result <ModerationAction> > Handle(Command request, CancellationToken cancellationToken) { var warnModerationAction = new ModerationAction ( moderationActionType: ModerationActionType.Warn, guildId: request.GuildId.Value, isActive: true, reason: request.Reason, date: DateTimeOffset.UtcNow.ToUnixTimeMilliseconds(), userDiscordId: request.UserDiscordId?.Value, userIgn: request.UserIgn, expiryDate: null ); if (request.UserIgn is not null) { var protobufMessage = new ChatMessage { ServerId = "MMCC", Message = $"You have been warned, @{request.UserIgn}. Reason: {request.Reason}", MessageOffset = 5 }; await _ps.BroadcastMessage(protobufMessage); } 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 guildName = guildResult.Entity.Name; var embed = new Embed { Title = $"You have been warned in {guildName}.", Colour = _colourPalette.Yellow, Thumbnail = EmbedProperties.MmccLogoThumbnail, Timestamp = DateTimeOffset.UtcNow, Fields = new List <EmbedField> { new("Reason", request.Reason, false) } }; 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(warnModerationAction, cancellationToken); await _context.SaveChangesAsync(cancellationToken); } catch (Exception e) { return(e); } return(warnModerationAction); }