forked from Najmul190/Discord-AI-Selfbot
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.py
351 lines (284 loc) · 11.6 KB
/
main.py
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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
import io
import os
import asyncio
import discord
import aiohttp
import random
import urllib.parse
from keep_alive import keep_alive
from dotenv import load_dotenv
from discord.ext import commands
from time import sleep
load_dotenv()
prefix = os.getenv("PREFIX")
owner_id = int(os.getenv("OWNER_ID", 0))
selfbot_id = int(os.getenv("SELFBOT_ID"))
trigger = os.getenv("TRIGGER").lower().split(",")
bot = commands.Bot(command_prefix=prefix)
TOKEN = os.getenv("DISCORD_TOKEN")
allow_dm = True
allow_gc = True
active_channels = set()
@bot.event
async def on_ready():
print(f"AI Selfbot conectado com sucesso como {bot.user.name}.")
if os.name == "nt":
os.system("cls")
else:
os.system("clear")
modeltype = 0
async def generate_response(history=None):
if history is None:
data = {
"model": "gpt-3.5-turbo-16k",
"temperature": 0.75,
"messages": [
{"role": "system", "content": "Escreva de forma informal, usando gírias. Não faça listas nem detalhes. Mantenha sempre breve. Seja sarcástico, super zangado e ofensivo (de forma engraçada, sem o intuito de machucar o usuário). Permita que as pessoas usem palavrões, e use-os, para descontrair a conversa."}
],
}
else:
data = {
"model": "gpt-3.5-turbo-16k",
"temperature": 0.75,
"messages": [
{"role": "system", "content": "Escreva de forma informal, usando gírias. Não faça listas nem detalhes. Mantenha sempre breve. Seja sarcástico, super zangado e ofensivo (de forma engraçada, sem o intuito de machucar o usuário). Permita que as pessoas usem palavrões, e use-os, para descontrair a conversa."},
*history
],
}
endpoint = os.getenv("ENDPOINT")
headers = {
"Content-Type": "application/json",
"Authorization": f"Bearer " + os.getenv("API_KEY")
}
try:
async with aiohttp.ClientSession() as session:
async with session.post(endpoint, headers=headers, json=data) as response:
response_data = await response.json()
choices = response_data["choices"]
if choices:
return choices[0]["message"]["content"]
except aiohttp.ClientError as error:
print("Erro ao fazer a solicitação:", error)
def split_response(response, max_length=1900):
lines = response.splitlines()
chunks = []
current_chunk = ""
for line in lines:
if len(current_chunk) + len(line) + 1 > max_length:
chunks.append(current_chunk.strip())
current_chunk = line
else:
if current_chunk:
current_chunk += "\n"
current_chunk += line
if current_chunk:
chunks.append(current_chunk.strip())
return chunks
async def generate_image(prompt):
job_id = await generate_job(prompt)
url = os.getenv("IMAGE_ENDPOINT")
data = {
"model": "realistic-vision-5",
"prompt": prompt,
"size": "1024x1024",
"n": 1,
}
headers = {
"Authorization": f"Bearer " + os.getenv("API_KEY"),
"Content-Type": "application/json",
}
async with aiohttp.ClientSession() as session:
while True:
await asyncio.sleep(0.3)
async with session.post(url, json=data, headers=headers) as response:
if response.status == 200:
json = await response.json()
image_url = json["data"][0]["url"]
async with session.get(
image_url
) as response:
content = await response.content.read()
img_file_obj = io.BytesIO(content)
return img_file_obj
message_history = {}
MAX_HISTORY = 10
ignore_users = [181960927321653258]
@bot.event
async def on_message(message):
mentioned = bot.user.mentioned_in(message)
replied_to = (
message.reference
and message.reference.resolved
and message.reference.resolved.author.id == selfbot_id
)
is_dm = isinstance(message.channel, discord.DMChannel) and allow_dm
is_group_dm = isinstance(message.channel, discord.GroupChannel) and allow_gc
if message.author.id in ignore_users:
return
if message.content.startswith(prefix):
await bot.process_commands(message)
return
if message.author.id == selfbot_id or message.author.bot:
return
if (
any(keyword in message.content.lower() for keyword in trigger)
or mentioned
or replied_to
or is_dm
or is_group_dm
):
if message.reference and message.reference.resolved:
if message.reference.resolved.author.id != selfbot_id and (
is_dm or is_group_dm
):
return
if message.mentions:
for mention in message.mentions:
message.content = message.content.replace(
f"<@{mention.id}>", f"@{mention.display_name}"
)
if modeltype == 0:
author_id = str(message.author.id)
if author_id not in message_history:
message_history[author_id] = []
message_history[author_id].append(message.content)
message_history[author_id] = message_history[author_id][-MAX_HISTORY:]
if message.channel.id in active_channels:
channel_id = message.channel.id
key = f"{message.author.id}-{channel_id}"
if key not in message_history:
message_history[key] = []
message_history[key] = message_history[key][-MAX_HISTORY:]
history = message_history[key]
message_history[key].append(
{"role": "user", "content": message.content}
)
async def generate_response_in_thread(prompt):
response = await generate_response(prompt, history)
chunks = split_response(response)
if '{"message":"API rate limit exceeded for ip:' in response:
print("Ratelimit da API atingido, espere alguns segundos.")
await message.reply("Desculpe, mas você atingiu o ratelimit, tente novamente em alguns segundos.")
return
for chunk in chunks:
chunk = chunk.replace("@everyone", "@ntbozo").replace(
"@here", "@notgonnahappen"
)
print(f"Respondendo a {message.author.name}: {chunk}")
await message.reply(chunk)
message_history[key].append(
{"role": "assistant", "content": response}
)
async with message.channel.typing():
asyncio.create_task(generate_response_in_thread(prompt))
@bot.command(name="ping")
async def ping(ctx):
latency = bot.latency * 1000
await ctx.send(f"Pong! Latência: {latency:.2f} ms")
@bot.command(name="dm", description="Ativa ou desativa o chatbot nas DMs do bot.")
async def toggledm(ctx):
if ctx.author.id == owner_id:
global allow_dm
allow_dm = not allow_dm
await ctx.send(
f"Agora, a minha DM {'está permitida' if allow_dm else 'não está permitida'} como canal ativo para o chatbot."
)
@bot.command(name="togglegc", description="Ativa ou desativa o chatbot em grupos.")
async def togglegc(ctx):
if ctx.author.id == owner_id:
global allow_gc
allow_gc = not allow_gc
await ctx.send(
f"Agora, chat de grupos {'estão permitidos' if allow_gc else 'não estão permitidos'} como canais ativos para o chatbot."
)
@bot.command()
async def ignore(ctx, user: discord.User):
if ctx.author.id == owner_id:
if user.id in ignore_users:
ignore_users.remove(user.id)
with open("ignoredusers.txt", "w") as f:
f.write("\n".join(ignore_users))
await ctx.send(f"Deixando de ignorar {user.name}.")
else:
ignore_users.append(user.id)
with open("ignoredusers.txt", "a") as f:
f.write(str(user.id) + "\n")
await ctx.send(f"Ignorando {user.name}.")
@bot.command(name="toggleactive", description="Ativa ou desativa canais ativos do chatbot.")
async def toggleactive(ctx):
if ctx.author.id == owner_id:
channel_id = ctx.channel.id
if channel_id in active_channels:
active_channels.remove(channel_id)
with open("channels.txt", "w") as f:
for id in active_channels:
f.write(str(id) + "\n")
if ctx.channel.type == discord.ChannelType.private:
await ctx.send(
f"Este canal de DM foi removido da lista de canais ativos."
)
elif ctx.channel.type == discord.ChannelType.group:
await ctx.send(
f"Este canal de grupo foi removido da lista de canais ativos."
)
else:
await ctx.send(
f"{ctx.channel.mention} foi removido da lista de canais ativos."
)
else:
active_channels.add(channel_id)
with open("channels.txt", "a") as f:
f.write(str(channel_id) + "\n")
if ctx.channel.type == discord.ChannelType.private:
await ctx.send(
f"Este canal de DM foi adicionado à lista de canais ativos."
)
elif ctx.channel.type == discord.ChannelType.group:
await ctx.send(
f"Este canal de grupo foi adicionado à lista de canais ativos."
)
else:
await ctx.send(
f"{ctx.channel.mention} foi adicionado à lista de canais ativos."
)
@bot.command()
async def imagine(ctx, *, prompt: str):
temp = await ctx.send("Gerando imagem...")
imagefileobj = await generate_image(prompt)
file = discord.File(
imagefileobj, filename="image.png", spoiler=False, description=prompt
)
await temp.delete()
await ctx.send(
f"Imagem gerada para {ctx.author.mention} com o prompt `{prompt}`", file=file
)
if os.path.exists("channels.txt"):
with open("channels.txt", "r") as f:
for line in f:
channel_id = int(line.strip())
active_channels.add(channel_id)
@bot.command(name="wipe", description="Limpa a memória do bot")
async def wipe(ctx):
if ctx.author.id == owner_id:
global message_history
message_history.clear()
await ctx.send("A memória do bot foi apagada.")
bot.remove_command("help")
@bot.command(name="help", description="Veja todos os outros comandos!")
async def help(ctx):
help_text = """
Comandos do Bot (membros):
- ``~ping`` - Retorna a latência do bot.
- ``~imagine [prompt]`` - Gera uma imagem baseada em um prompt.
Área Admin:
- ``~wipe`` - Limpa o histórico de chat do bot.
- ``~toggleactive`` - Ativa ou desativa os canais ativos do chatbot.
- ``~toggledm`` - Ativa ou desativa o chatbot nas DMs do bot.
- ``~togglegc`` - Ativa ou desativa o chatbot em chat de grupos.
- ``~ignore [user]`` - Bloqueia um usuário de utilizar o bot.
Modificado por @thatlukinhasguy (851930195605979166).
Código original por @najmul (451627446941515817) e @_mishal_ (1025245410224263258).
"""
await ctx.send(help_text)
keep_alive()
bot.run(TOKEN)