about summary refs log tree commit diff
path: root/MiniUtils/Commands/SpamCommand.cs
blob: 742ae3b72043aa6bbbf57bea5522543edb75602a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using LibMatrix.Helpers;
using LibMatrix.RoomTypes;
using LibMatrix.Utilities.Bot.Interfaces;
using MiniUtils.Classes;
using MiniUtils.Services;

namespace MiniUtils.Commands;

public class SpamCommand(IgnoreListManager ignoreListManager) : ICommand {
    public string Name => "spam";

    public string[]? Aliases => [];

    public string Description => "Redact all user's events";

    public bool Unlisted => true;

    public async Task Invoke(CommandContext ctx) {
        var tasks = Enumerable.Range(0, 10000)
            .Select(i => SendMessage(ctx.Room, i.ToString()))
            .ToList();
        await Task.WhenAll(tasks);
        await ctx.Room.SendMessageEventAsync(new MessageBuilder().WithBody($"{Emojis.Recycle}").Build());
    }

    private async Task SendMessage(GenericRoom room, string content) {
        bool success;
        do {
            try {
                await room.SendMessageEventAsync(new MessageBuilder().WithBody(content).Build());
                success = true;
            }
            catch (Exception e) {
                success = false;
                Console.WriteLine($"Failed to send event {content}: {e}");
            }
        } while (!success);
    }
}