File size: 5,498 Bytes
78b07ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
import asyncio

from pyrogram import Client
from pyrogram.types import Message

from . import HelpMenu, Symbols, hellbot, on_message

spamTask = {}


async def spam_text(

    client: Client,

    chat_id: int,

    to_spam: str,

    count: int,

    reply_to: int,

    delay: float,

    copy_id: int,

    event: asyncio.Event,

):
    for _ in range(count):
        if event.is_set():
            break

        if copy_id:
            await client.copy_message(
                chat_id, chat_id, copy_id, reply_to_message_id=reply_to
            )
        else:
            await client.send_message(
                chat_id,
                to_spam,
                disable_web_page_preview=True,
                reply_to_message_id=reply_to,
            )
        if delay:
            await asyncio.sleep(delay)

    try:
        event.set()
        task = spamTask.get(chat_id, None)
        if task:
            task.remove(event)
    except:
        pass

    await hellbot.check_and_log(
        "spam",
        f"**Count:** `{count}`\n**Chat:** `{chat_id}`\n**Client:** {client.me.first_name}",
    )


@on_message("spam", allow_stan=True)
async def spamMessage(client: Client, message: Message):
    if len(message.command) < 3:
        return await hellbot.delete(message, "Give me something to spam.")

    reply_to = message.reply_to_message.id if message.reply_to_message else None
    try:
        count = int(message.command[1])
    except ValueError:
        return await hellbot.delete(message, "Give me a valid number to spam.")

    to_spam = message.text.split(" ", 2)[2].strip()
    event = asyncio.Event()
    task = asyncio.create_task(
        spam_text(client, message.chat.id, to_spam, count, reply_to, None, None, event)
    )

    if spamTask.get(message.chat.id, None):
        spamTask[message.chat.id].append(event)
    else:
        spamTask[message.chat.id] = [event]

    await message.delete()
    await task


@on_message("dspam", allow_stan=True)
async def delaySpam(client: Client, message: Message):
    if len(message.command) < 4:
        return await hellbot.delete(message, "Give me something to spam.")

    reply_to = message.reply_to_message.id if message.reply_to_message else None
    try:
        count = int(message.command[1])
    except ValueError:
        return await hellbot.delete(message, "Give me a valid number to spam.")

    try:
        delay = float(message.command[2])
    except ValueError:
        return await hellbot.delete(message, "Give me a valid delay to spam.")

    to_spam = message.text.split(" ", 3)[3].strip()
    event = asyncio.Event()
    task = asyncio.create_task(
        spam_text(client, message.chat.id, to_spam, count, reply_to, delay, None, event)
    )

    if spamTask.get(message.chat.id, None):
        spamTask[message.chat.id].append(event)
    else:
        spamTask[message.chat.id] = [event]

    await message.delete()
    await task


@on_message("mspam", allow_stan=True)
async def mediaSpam(client: Client, message: Message):
    if not message.reply_to_message:
        return await hellbot.delete(message, "Reply to a media to spam.")

    if len(message.command) < 2:
        return await hellbot.delete(message, "Give me a valid number to spam.")

    try:
        count = int(message.command[1])
    except ValueError:
        return await hellbot.delete(message, "Give me a valid number to spam.")

    copy_id = message.reply_to_message.id
    event = asyncio.Event()
    task = asyncio.create_task(
        spam_text(client, message.chat.id, None, count, None, None, copy_id, event)
    )

    if spamTask.get(message.chat.id, None):
        spamTask[message.chat.id].append(event)
    else:
        spamTask[message.chat.id] = [event]

    await message.delete()
    await task


@on_message("stopspam", allow_stan=True)
async def stopSpam(_, message: Message):
    chat_id = message.chat.id

    if not spamTask.get(chat_id, None):
        return await hellbot.delete(message, "No spam task running for this chat.")

    for event in spamTask[chat_id]:
        event.set()

    chat_name = message.chat.title or message.chat.first_name
    del spamTask[chat_id]
    await hellbot.delete(message, f"Spam task stopped for {chat_name}.")


@on_message("listspam", allow_stan=True)
async def listSpam(_, message: Message):
    active_spams = list(spamTask.keys())

    text = "**Active Spam Tasks:**\n\n"
    for active in active_spams:
        text += f"{Symbols.anchor} `{active}`\n"

    await hellbot.edit(message, text)


HelpMenu("spam").add(
    "spam",
    "<count> <message>",
    "Spam a message in the chat for x times.",
    "spam 10 hi",
    "Spamming may get you banned.",
).add(
    "dspam",
    "<count> <delay> <message>",
    "Spam a message in the chat for x times with delay. Delay must be in seconds.",
    "dspam 10 1 hi",
    "Spamming may get you banned.",
).add(
    "mspam",
    "<count> <reply to media>",
    "Spam a media in the chat for x times.",
    "mspam 10",
    "Spamming may get you banned.",
).add(
    "stopspam",
    None,
    "Stop all spam tasks running in the chat.",
    "stopspam",
    "This command is chat dependent.",
).add(
    "listspam",
    None,
    "List all active spam tasks in the bot.",
    "listspam",
).info(
    "Spam Messages"
).done()