-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.py
335 lines (248 loc) · 9.17 KB
/
index.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
import asyncio
import discord
from discord.ext import commands, tasks
from discord.voice_client import VoiceClient
import yt_dlp as youtube_dl
from discord.utils import get
from random import choice
import datetime
import re
from urllib import parse, request
import openai
intents = discord.Intents.default()
intents.message_content = True
openai.api_key = 'Token Chatgpt'
bot = commands.Bot(command_prefix="!", intents=intents)
status = ['Music!', 'Eating!', 'ZzzzZz!']
queue = []
loop = False
youtube_dl.utils.bug_reports_message = lambda: ''
ytdl_format_options = {
'format': 'bestaudio/best',
'restrictfilenames': True,
'noplaylist': True,
'nocheckcertificate': True,
'ignoreerrors': False,
'logtostderr': False,
'quiet': True,
'no_warnings': True,
'default_search': 'auto',
'source_address': '0.0.0.0' # bind to ipv4 since ipv6 addresses cause issues sometimes
}
ffmpeg_options = {
'before_options': '-reconnect 1 -reconnect_streamed 1 -reconnect_delay_max 5',
'options': '-vn'
}
ytdl = youtube_dl.YoutubeDL(ytdl_format_options)
class YTDLSource(discord.PCMVolumeTransformer):
def __init__(self, source, *, data, volume=0.9):
super().__init__(source, volume)
self.data = data
self.title = data.get('title')
self.url = ""
@classmethod
async def from_url(cls, url, *, loop=None, stream=True):
loop = loop or asyncio.get_event_loop()
data = await loop.run_in_executor(None, lambda: ytdl.extract_info(url, download=not stream))
if 'entries' in data:
# take first item from a playlist
data = data['entries'][0]
filename = data['url'] if stream else ytdl.prepare_filename(data)
return cls(discord.FFmpegPCMAudio(filename, **ffmpeg_options), data=data)
@bot.command()
async def ping(ctx):
await ctx.send(f'**Pong!** Latency: {round(bot.latency * 1000)}ms')
@bot.command()
async def sum(ctx, numOne: int, numTwo: int):
await ctx.send(numOne + numTwo)
@bot.command()
async def div(ctx, numOne: int, numTwo: int):
await ctx.send(numOne/numTwo)
@bot.command()
async def prod(ctx, numOne: int, numTwo: int):
await ctx.send(numOne*numTwo)
@bot.command()
async def hello(ctx):
author = ctx.message.author
await ctx.send(f'Hello, {author.mention}!')
@bot.command()
async def info(ctx):
embed = discord.Embed(title=f"{ctx.guild.name}", description="Lorem Ipsum asdasd",
timestamp=datetime.datetime.utcnow(), color=discord.Color.blue())
embed.add_field(name="Server created at", value=f"{ctx.guild.created_at}")
embed.add_field(name="Server Owner", value=f"{ctx.guild.owner}")
embed.add_field(name="Server ID", value=f"{ctx.guild.id}")
# embed.set_thumbnail(url=f"{ctx.guild.icon}")
embed.set_thumbnail(
url="https://pluralsight.imgix.net/paths/python-7be70baaac.png")
await ctx.send(embed=embed)
@bot.command()
async def p(ctx, *, search):
query_string = parse.urlencode({'search_query': search})
html_content = request.urlopen(
'http://www.youtube.com/results?' + query_string)
# print(html_content.read().decode())
search_results = re.findall(
'watch\?v=(.{11})', html_content.read().decode('utf-8'))
# I will put just the first result, you can loop the response to show more results
url = ('https://www.youtube.com/watch?v=' + search_results[0])
await ctx.send('https://www.youtube.com/watch?v=' + search_results[0])
await join(ctx)
await queue_(ctx, url)
await play(ctx)
@bot.command(pass_context=True)
async def join(ctx):
canal = ctx.message.author.voice.channel
voz = get(bot.voice_clients, guild=ctx.guild)
if voz and voz.is_connected():
await voz.move_to(canal)
else:
voz = await canal.connect()
@bot.command(pass_context=True)
async def exit(ctx):
canal = ctx.message.author.voice.channel
voz = get(bot.voice_clients, guild=ctx.guild)
await voz.disconnect()
@bot.command(name='loop', help='This command toggles loop mode')
async def loop_(ctx):
global loop
if loop:
await ctx.send('Loop mode is now `False!`')
loop = False
else:
await ctx.send('Loop mode is now `True!`')
loop = True
@bot.command(pass_context=True)
async def play(ctx):
global queue
print(queue)
if not ctx.message.author.voice:
await ctx.send("You are not connected to a voice channel")
return
elif len(queue) == 0:
await ctx.send('Nothing in your queue! Use `!p` to add a song!')
else:
try:
channel = ctx.message.author.voice.channel
await channel.connect()
except:
pass
server = ctx.message.guild
voice_channel = server.voice_client
while queue:
try:
while voice_channel.is_playing() or voice_channel.is_paused():
await asyncio.sleep(2)
pass
except AttributeError:
pass
try:
async with ctx.typing():
player = await YTDLSource.from_url(queue[0], loop=bot.loop)
voice_channel.play(player, after=lambda e: print(
'Player error: %s' % e) if e else None)
if loop:
queue.append(queue[0])
del (queue[0])
await ctx.send('**Now playing:** {}'.format(player.title))
except:
break
@bot.command()
async def queue_(ctx, url):
global queue
queue.append(url)
await ctx.send(f'`{url}` added to queue!')
@bot.command()
async def remove(ctx, number):
global queue
try:
del (queue[int(number)])
await ctx.send(f'Your queue is now `{queue}!`')
except:
await ctx.send('Your queue is either **empty** or the index is **out of range**')
@bot.command(pass_context=True)
async def pause(ctx):
voz = get(bot.voice_clients, guild=ctx.guild)
if voz and voz.is_playing():
print("Musica pausada")
voz.pause()
await ctx.send("Musica Pausada")
else:
print("No se esta Reproduciendo,Pausa erronea")
await ctx.send("Pausa Erronea, no se esta Reproduciendo")
@bot.command(pass_context=True)
async def resume(ctx):
voz = get(bot.voice_clients, guild=ctx.guild)
if voz and voz.is_paused():
print("Reproduciendo Nuevamente")
voz.resume()
await ctx.send("Reproduciendo Nuevamente")
else:
print("No se encuentra pausada")
await ctx.send("No se encuentra pausada")
@bot.command(pass_context=True)
async def stop(ctx):
voz = get(bot.voice_clients, guild=ctx.guild)
if voz and voz.is_playing():
print("Musica detenida")
voz.stop()
await ctx.send("Musica detenida")
else:
print("No se esta reproduciendo")
await ctx.send("No se esta reproduciendo")
@bot.command()
async def volume(ctx, volume: int):
if ctx.voice_client is None:
return await ctx.send("Not connected to a voice channel.")
ctx.voice_client.source.volume = volume / 100
await ctx.send(f"Volume cambiado a {volume}%")
class NoMoreTracks(Exception):
pass
@bot.command()
async def next(ctx):
voz = get(bot.voice_clients, guild=ctx.guild)
if not voz.is_playing():
raise NoMoreTracks
voz.stop()
await ctx.send("Siguiente cancion")
@bot.command()
async def coinflip(ctx):
if choice(["cara", "cruz"]) == "cara":
await ctx.send("Salió cara! :orange_circle:")
else:
await ctx.send("Salió cruz! :yellow_circle:")
@bot.command()
async def clear(ctx, amount=10000):
await ctx.channel.purge(limit=amount)
await ctx.send("Messages have been cleared")
@bot.command()
async def view(ctx):
await ctx.send(f' `{queue}` agregada a la lista')
# Events
@bot.event
async def on_ready():
await bot.change_presence(activity=discord.Game(choice(status)))
print('Estoy en linea my friend')
@tasks.loop(seconds=20)
async def change_status():
await bot.change_presence(activity=discord.Game(choice(status)))
@bot.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f'Hi {member.name}, Welcome to Chofus!'
)
#CHAT GPT
@bot.command()
async def chat(ctx, *, message: str):
# Limpia el mensaje de cualquier ruido y detención de palabras
message = message.lower()
# Envía el mensaje al modelo de GPT-3 y obtén la respuesta
response = openai.Completion.create(model="text-davinci-002", prompt=message, max_tokens=2032, n=1, stop=None, temperature=0.2)
# Filtra la respuesta para asegurarse de que sea coherente
answer = re.sub('[^A-Za-z0-9áéíóúñ¿?¡!.,;: ]+', '', response.choices[0].text)
if len(answer) > 2000:
answer = answer[:2000] # Limita la respuesta a los primeros 2000 caracteres
# Envía la respuesta al canal de Discord
await ctx.send(answer)
bot.run('Token Discord')