-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathelitebot.py
183 lines (154 loc) · 6.15 KB
/
elitebot.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
prefix='?'
myToken=''
weather_API_Key = ''
#==================================================================
from discord.ext.commands import Bot
import os
import json
import requests
import discord
from discord.ext import commands
intents = discord.Intents.default()
intents.members = True
intents.message_content = True
bot = Bot(command_prefix=commands.when_mentioned_or(prefix), intents=intents)
@bot.command(name = 'hello',
description= 'say hello to some fuck that needs a friend',
)
async def hello(ctx):
if isinstance(ctx.channel, discord.TextChannel):
if ctx.channel.permissions_for(ctx.me).send_messages:
await ctx.send('Hello!')
else:
print('I don\'t have permission to send messages in this channel.')
else:
await ctx.author.send('This command can only be used in a server/channel.')
# for adding user Loc
@bot.command(name = 'add',
description= 'Add location for weather api',
)
async def addLocation(ctx,*, location) -> None:
#chk in a mom joke then save
if location.find('house') != -1 and location.find('mom') != -1:
# joke good, saving
await ctx.send(content='adding')
wther().setWordCount(ctx.author, location)
return
# else chk in a real Loc then save
else:
lonLan = locationChk(location)
if len(lonLan) < 2 :
await ctx.send(lonLan[0])
return
# Loc good, saving
await ctx.send(content='adding')
wther().setWordCount(ctx.author, location)
# for adding user Loc
@bot.command(name = 'w',
description= 'Check the weather',
)
async def weather(ctx,*, location: str = '+') -> None:
# the bot does not handle emp str well so I used a +, I could have used None I guess but W.E
if location == '+':
locations = wther().getLocations()
# chk to see if we have a location as none was given
try:
location = locations[str(ctx.author)]
except:
await ctx.send(f'Sorry, you need to eather use {prefix}add, to add a location or send one while using {prefix}w')
return
# If Loc is mom joke
if location.find('house') != -1 and location.find('mom') != -1:
embed = discord.Embed(
title=f'Weather in {location}',
description=f'Temperature: 🌡️ It\'s always warm in her bed',
color=0x9C84EF
)
await ctx.send(embed=embed)
return
# chk Loc
lonLan = locationChk(location)
if len(lonLan) < 2 :
# Bad Loc or Server Error
await ctx.send(lonLan[0])
return
latitude = lonLan[0]
longitude = lonLan[1]
# make a request to the OpenWeatherMap API for the current weather data for the specified location
owm_response = requests.get(f'http://api.openweathermap.org/data/2.5/weather?lat={latitude}&lon={longitude}&units=metric&appid={weather_API_Key}')
if owm_response.status_code != 200:
await ctx.send('Sorry, there was an error retrieving the weather data.')
return
# extract the weather data from the API response
data = owm_response.json()
tempC = data['main']['temp']
tempF = c2f(tempC)
temperature = f'{tempC}°C/{tempF}°F'
feels_likeC = data['main']['feels_like']
feels_likeF = c2f(feels_likeC)
feels_like = f'{feels_likeC}°C/{feels_likeF}°F'
humidity = data['main']['humidity']
wind_speed = data['wind']['speed']
clouds = data['clouds']['all']
description = data['weather'][0]['description']
location_name = data['name']
# determine the appropriate emoji based on the weather description
emoji = ""
if "clear" in description:
emoji = "☀️"
elif "cloud" in description:
emoji = "☁️"
elif "rain" in description:
emoji = "🌧️"
elif "thunderstorm" in description:
emoji = "⛈️"
elif "snow" in description:
emoji = "❄️"
# send a message to the channel with the current weather data for the specified location, along with an appropriate emoji
embed = discord.Embed(
title=f'Weather in {location_name}',
description=f'Temperature: 🌡️ {temperature}\nFeels Like: {feels_like}\nHumidity: {humidity}%\nWind Speed: {wind_speed} m/s\nClouds: {clouds}%\nDescription: {emoji} {description}',
color=0x9C84EF
)
await ctx.send(embed=embed)
def c2f(tempc):
x = (tempc * 9/5) + 32
tempF = round(x, 2)
return tempF
# for saving usr Loc
# didnt need the class but eh
class wther():
def __init__(self):
dirName = f'{os.path.dirname(os.path.abspath(__file__))}'
self.userLocationsFile = f'{dirName}/userLocations.json'
self.getLocations()
def getLocations(self):
if os.path.isfile(self.userLocationsFile):
with open(self.userLocationsFile, 'r') as openfile:
self.userLocations = json.load(openfile)
else:
self.userLocations = {}
with open(self.userLocationsFile, 'a') as outfile:
json.dump(self.userLocations, outfile)
return self.userLocations
def setWordCount(self, user, location):
self.userLocations.update({str(user):str(location)})
with open(self.userLocationsFile, 'w') as outfile:
json.dump(self.userLocations, outfile)
return self.userLocations
def locationChk(location):
# make a request to the OpenStreetMap API to retrieve the latitude and longitude for the location
nominatim_response = requests.get(f"https://nominatim.openstreetmap.org/search?q={location}&format=json")
if nominatim_response.status_code != 200:
return ['Sorry, there was an error retrieving the location data.']
# extract the latitude and longitude from the API response
nominatim_data = nominatim_response.json()
if len(nominatim_data) == 0:
return [f'No results found for location: {location}']
latitude = nominatim_data[0]["lat"]
longitude = nominatim_data[0]["lon"]
return [latitude, longitude]
@bot.event
async def on_ready():
print('Connected to Discord!')
bot.run(myToken)