-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.py
More file actions
194 lines (169 loc) · 7.12 KB
/
Copy pathmain.py
File metadata and controls
194 lines (169 loc) · 7.12 KB
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
import discord
from discord.ext import commands
import random
from discord.ext.commands import Bot
from dotenv import load_dotenv
import os
from requests import get
import openai
import json
import requests
import re
import yt_dlp
from discord.voice_client import VoiceClient
from pydub import AudioSegment
from googletrans import Translator
load_dotenv()
discordtoken = os.getenv("TOKEN")
openai_api_key = os.getenv("API_KEY")
openai.api_key = openai_api_key
news_key = os.getenv("NEWS_API_KEY")
weather_key = os.getenv("OPEN_WEATHER_KEY")
intents = discord.Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='$', intents=intents)
yt_dl_opts = {'format': 'bestaudio/best'}
ytdl = yt_dlp.YoutubeDL(yt_dl_opts)
ffmpeg_options = {'options': "-vn"}
#with open('badworlds.txt', 'r') as f:
# badworlds = f.read().splitlines()
@bot.event
async def on_ready():
print("bot is ready")
#@bot.event
#async def on_message(message):
# for word in badworlds:
# if word in message.content:
# await message.delete()
# else:
# pass
@bot.command()
async def doc(ctx):
embed = discord.Embed(title="List of available commands:", color=discord.Color.blue())
embed.add_field(name="$chat", value="This command lets you have a conversation with the bot. Usage: $chat [message]")
embed.add_field(name="$meme", value="This command sends a random meme. Usage: $meme")
embed.add_field(name="$play", value="This command plays a YouTube video in the voice channel you are in. Usage: $play [YouTube video URL]")
embed.add_field(name="$pause", value="This command pauses the currently playing YouTube video. Usage: $pause")
embed.add_field(name="$resume", value="This command resumes the currently paused YouTube video. Usage: $resume")
embed.add_field(name="$stop", value="This command stops the currently playing YouTube video. Usage: $stop")
embed.add_field(name="$rdog", value="This command sends a random dog image. Usage: $rdog")
embed.add_field(name="$translate", value="This command translates text to a specified language. Usage: $translate [language code] [text]")
embed.add_field(name="$news", value="This command displays the latest news for the given keyword. Usage: $news [keyword]")
embed.add_field(name="$weather", value="this command dispalys the weather in you city Usage: $weather [city_name]")
await ctx.send(embed=embed)
@bot.command()
async def chat(ctx, *, message: str = None):
if message is None:
await ctx.send(f"please enter somthing after **$chat** {ctx.message.author.mention}.")
return
try:
response = openai.Completion.create(engine="text-davinci-002", prompt=message)
await ctx.send(f"{response.choices[0].text} ~{ctx.message.author.mention}")
return
except:
await ctx.send(f"**AuthenticationError** Incorrect API key provided You can find your API key at https://platform.openai.com/account/api-keys. And please make your that you have internet connection")
@bot.command()
async def meme(ctx):
content = get("https://meme-api.com/gimme").text
data = json.loads(content,)
meme = discord.Embed(title=f"{data['title']}", color = discord.Color.random()).set_image(url=f"{data['url']}")
await ctx.send(embed=meme)
return
@bot.command()
async def play(ctx, url: str):
try:
voice_client = await ctx.author.voice.channel.connect()
info = ytdl.extract_info(url, download=False)
audio_url = info["url"]
voice_client.play(discord.FFmpegPCMAudio(audio_url))
#sound = AudioSegment.from_file(audio_url, format="mp3")youtube_dl
#voice_client.play(discord.FFmpegPCMAudio(sound))
if voice_client is None:
voice_client = ctx.voice_client
await voice_client.disconnect()
except discord.errors.ClientException:
await voice_client.disconnect()
except AttributeError:
await ctx.send(f"johin a voice channel")
@bot.command()
async def pause(ctx):
voice_client = ctx.guild.voice_client
if voice_client and voice_client.is_playing():
voice_client.pause()
await ctx.send(f"Paused the current song {ctx.message.author.mention}.")
else:
await ctx.send(f"Not playing any song {ctx.message.author.mention}.")
@bot.command()
async def resume(ctx):
voice_client = ctx.guild.voice_client
if voice_client:
voice_client.resume()
await ctx.send(f"resume the current song {ctx.message.author.mention}.")
else:
await ctx.send("fNot playing any song {ctx.message.author.mention}.")
@bot.command()
async def stop(ctx):
try:
voice_client = ctx.voice_client
await voice_client.disconnect()
except:
await ctx.send(f"not in a voice channel dont fool me {ctx.message.author.mention}.")
@bot.command()
async def rdog(ctx):
const = requests.get("https://random.dog/woof.json")
stuff = json.loads(const.text)
embed = discord.Embed(title=f"URL: {stuff['url']}", color = discord.Color.random())
embed.set_image(url=f"{stuff['url']}")
await ctx.send(embed=embed)
@bot.command()
async def translate(ctx, dest_lang: str, *, text: str):
translator = Translator()
translated_text = translator.translate(text, dest=dest_lang).text
await ctx.send(f"Translation: {translated_text}")
@bot.command()
async def news(ctx, innews):
news = requests.get(f"https://newsapi.org/v2/everything?q={innews}&apiKey={news_key}").json()
thnews = news["articles"]
if news["status"] == "ok":
for i , article in enumerate(thnews):
if i < 5:
emend = discord.Embed(title=article["title"], description=article["description"], url=article["url"],color=discord.Color.blue())
await ctx.send(embed=emend)
else:
break
else:
await ctx.send(f"{innews} not found please try again")
@bot.command()
async def imggen(ctx, *, text: str = None):
try:
if text is None:
await ctx.send("please enter some text")
return
await ctx.send("please wait a second genarting image")
response = openai.Image.create(
prompt=text,
n=1,
size="256x256",
)
await ctx.send(response["data"][0]["url"])
except:
await ctx.send("nsfw images are not allowed")
@bot.command()
async def weather(ctx, *, city: str = None):
try:
weather_data = requests.get(
f"https://api.openweathermap.org/data/2.5/weather?q={city}&units=imperial&APPID={weather_key}")
weather = weather_data.json()['weather'][0]['main']
temp = round(weather_data.json()['main']['temp'])
celsius = (temp - 32) * 5/9
int_celsius = int(celsius)
# if weather_data.json()['code'] == '404':
# await ctx.send(f"city {city} not found")
await ctx.send(f"""
The Weather in {city} is {weather}
The temperature in {city} is around: {temp}ºF or {int_celsius}°C
""")
except KeyError:
print("are you missing api key?")
await ctx.send(f"oops somthing went wong i think {city} does not exist")
bot.run(discordtoken)