-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbot.py
287 lines (228 loc) · 9.31 KB
/
bot.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
from os import getenv
from datetime import datetime, timedelta
from zoneinfo import ZoneInfo
from typing import Iterator, Optional
from dotenv import load_dotenv
from asyncache import cached
from cachetools import TTLCache
from requests import get, post
import discord
from discord import app_commands
from discord.ext import tasks
from helper import nth_element
load_dotenv()
GUILD_ID = int(getenv("GUILD_ID"))
TOKEN = getenv("DISCORD_TOKEN")
WEATHER_API_KEY = getenv("WEATHER_API_KEY")
GENERAL_CHANNEL_ID = int(getenv("GENERAL_CHANNEL_ID"))
MY_GUILD = discord.Object(id=GUILD_ID)
tt = datetime.now(ZoneInfo("Europe/Paris"))
md = tt.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(days=1)
mid = md.timetz()
def embed_from_quote(my_quote: dict):
embed = discord.Embed(title="Quote")
embed.description = my_quote["content"]
embed.set_author(name=my_quote["author"])
return embed
class Dropdown(discord.ui.Select):
def __init__(self):
# Set the options that will be presented inside the dropdown
options = [
discord.SelectOption(
label="Red", description="Your favourite colour is red", emoji="🟥"
),
discord.SelectOption(
label="Green", description="Your favourite colour is green", emoji="🟩"
),
discord.SelectOption(
label="Blue", description="Your favourite colour is blue", emoji="🟦"
),
]
# The placeholder is what will be shown when no option is chosen
# The min and max values indicate we can only pick one of the three options
# The options parameter defines the dropdown options. We defined this above
super().__init__(
placeholder="Choose your favourite colour...",
min_values=1,
max_values=1,
options=options,
)
async def callback(self, interaction: discord.Interaction):
# Use the interaction object to send a response message containing
# the user's favourite colour or choice. The self object refers to the
# Select object, and the values attribute gets a list of the user's
# selected options. We only want the first one.
await interaction.response.send_message(
f"Your favourite colour is {self.values[0]}"
)
class DropdownView(discord.ui.View):
def __init__(self):
super().__init__()
# Adds the dropdown to our view object.
self.add_item(Dropdown())
class MyClient(discord.Client):
def __init__(self, *, client_intents: discord.Intents):
super().__init__(intents=client_intents)
self.tree = app_commands.CommandTree(self)
async def setup_hook(self):
self.tree.copy_global_to(guild=MY_GUILD)
self.quote_task.start()
await self.tree.sync(guild=MY_GUILD)
@tasks.loop(time=mid)
async def quote_task(self):
channel = self.get_channel(GENERAL_CHANNEL_ID)
# response = get("https://api.quotable.io/random?maxLength=230")
# my_quote = response.json()
# await channel.send(embed=embed_from_quote(my_quote))
await channel.send("https://www.youtube.com/watch?v=F8jlpPVeTUg")
@quote_task.before_loop
async def before_quote_task(self):
await self.wait_until_ready()
intents = discord.Intents.default()
client = MyClient(client_intents=intents)
@client.event
async def on_ready():
print(f"Logged in as {client.user} (ID: {client.user.id})")
print("------")
@client.tree.command()
async def hello(interaction: discord.Interaction):
"""Says hello!"""
await interaction.response.send_message(f"Hi, {interaction.user.mention}")
@client.tree.command()
@app_commands.describe(
first_value="The first value you want to add something to",
second_value="The value you want to add to the first value",
)
async def add(interaction: discord.Interaction, first_value: int, second_value: int):
"""Adds two numbers together."""
await interaction.response.send_message(
f"{first_value} + {second_value} = {first_value + second_value}"
)
@client.tree.command()
@app_commands.rename(text_to_send="text")
@app_commands.describe(text_to_send="Text to send in the current channel")
async def send(interaction: discord.Interaction, text_to_send: str):
"""Sends the text into the current channel."""
await interaction.response.send_message(text_to_send)
@client.tree.command()
@app_commands.describe(
member="The member you want to get the joined date from; defaults to the user who uses the "
"command"
)
async def joined(
interaction: discord.Interaction, member: Optional[discord.Member] = None
):
"""Says when a member joined."""
member = member or interaction.user
await interaction.response.send_message(
f"{member} joined {discord.utils.format_dt(member.joined_at)}", ephemeral=True
)
@client.tree.context_menu(name="Show Join Date")
async def show_join_date(interaction: discord.Interaction, member: discord.Member):
await interaction.response.send_message(
f"{member} joined at {discord.utils.format_dt(member.joined_at)}",
ephemeral=True,
)
@client.tree.context_menu(name="Translate")
async def translate(interaction: discord.Interaction, message: discord.Message):
await interaction.response.defer(ephemeral=True)
"""Translate text to another language."""
try:
response = post(
"https://translate.argosopentech.com/translate",
json={"q": message.content, "source": "fr", "target": "en"},
timeout=30,
)
response.raise_for_status()
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}", ephemeral=True)
return
try:
json = response.json()
translated_text = json["translatedText"]
except Exception as e:
await interaction.followup.send(f"An error occurred: {e}", ephemeral=True)
return
await interaction.followup.send(
f"Translated text: {translated_text}", ephemeral=True
)
@client.tree.command()
async def quote(interaction: discord.Interaction):
"""Sends a random quote."""
response = get("https://api.quotable.io/random?maxLength=230")
my_quote = response.json()
await interaction.response.send_message(embed=embed_from_quote(my_quote))
@cached(cache=TTLCache(maxsize=1024, ttl=600))
async def get_weather_json(city: str):
try:
weather_response = get(
f"https://api.weatherapi.com/v1/current.json?key={WEATHER_API_KEY}&q={city}&aqi=no"
)
weather_response.raise_for_status()
forecast_response = get(
f"https://api.weatherapi.com/v1/forecast.json?key={WEATHER_API_KEY}&q={city}&days=1&aqi=no&alerts=no"
)
forecast_response.raise_for_status()
except Exception as e:
raise e
return weather_response.json(), forecast_response.json()
@client.tree.command()
@app_commands.describe(city="The city you want to get the weather for")
async def weather(interaction: discord.Interaction, city: str):
"""Get the current weather"""
try:
weather_response, forecast_response = await get_weather_json(city)
except Exception as _e:
await interaction.response.send_message(
f'Error: the city "{city}" was not found', ephemeral=True
)
return
current_temp = weather_response["current"]["temp_c"]
forecast = forecast_response["forecast"]["forecastday"][0]
forecast_min = forecast["day"]["mintemp_c"]
forecast_max = forecast["day"]["maxtemp_c"]
embed = discord.Embed(title="Weather")
embed.description = (
f"Current temperature: {current_temp}°C\n"
f"Forecast: Min: {forecast_min}°C, Max: {forecast_max}°C "
)
await interaction.response.send_message(embed=embed)
# A command tha tell the time until midnight
@client.tree.command()
async def midnight(interaction: discord.Interaction):
"""Sends the time until midnight."""
paris_dt = datetime.now(ZoneInfo("Europe/Paris"))
midnight = paris_dt.replace(hour=0, minute=0, second=0, microsecond=0) + timedelta(
days=1
)
time_until_midnight = midnight - paris_dt
await interaction.response.send_message(
f"Time until midnight: {time_until_midnight}"
)
# A command that say what the best programming language is
@client.tree.command()
async def best_language(interaction: discord.Interaction):
"""Sends the best programming language."""
await interaction.response.send_message("The best programming language is Rust")
@client.tree.command()
async def color(interaction: discord.Interaction):
"""Sends a random color."""
await interaction.response.send_message(view=DropdownView())
def fibo_gen() -> Iterator[int]:
a, b = 0, 1
while True:
yield a
a, b = b, a + b
def fib(n: int) -> int:
return nth_element(fibo_gen(), n)
# @client.tree.command()
# async def fibonacci(interaction: discord.Interaction, n: int):
# """Sends the nth fibonacci number."""
# await interaction.response.send_message(f'{n}th fibonacci number is {fib(n)}')
@client.event
async def on_member_join(member: discord.Member):
guild = member.guild
channel = client.get_channel(GENERAL_CHANNEL_ID)
await channel.send(f"Welcome {member.mention} to {guild.name}!")
if __name__ == "__main__":
client.run(TOKEN)