This commit is contained in:
Pierre Berthe 2024-02-15 23:08:54 +01:00
parent 217905e6a8
commit f72ec9151f

131
bot.py
View file

@ -30,40 +30,46 @@ md = md - timedelta(milliseconds=600)
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'])
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='🟥'),
label="Red", description="Your favourite colour is red", emoji="🟥"
),
discord.SelectOption(
label='Green', description='Your favourite colour is green', emoji='🟩'),
label="Green", description="Your favourite colour is green", emoji="🟩"
),
discord.SelectOption(
label='Blue', description='Your favourite colour is blue', emoji='🟦'),
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)
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]}')
await interaction.response.send_message(
f"Your favourite colour is {self.values[0]}"
)
class DropdownView(discord.ui.View):
@ -102,75 +108,88 @@ client = MyClient(client_intents=intents)
@client.event
async def on_ready():
print(f'Logged in as {client.user} (ID: {client.user.id})')
print('------')
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}')
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',
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}')
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')
@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):
@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)
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')
@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)
await interaction.response.send_message(
f"{member} joined at {discord.utils.format_dt(member.joined_at)}",
ephemeral=True,
)
@client.tree.context_menu(name='Translate')
@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 = 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)
await interaction.followup.send(f"An error occurred: {e}", ephemeral=True)
return
try:
json = response.json()
translated_text = json['translatedText']
translated_text = json["translatedText"]
except Exception as e:
await interaction.followup.send(f'An error occurred: {e}', ephemeral=True)
await interaction.followup.send(f"An error occurred: {e}", ephemeral=True)
return
await interaction.followup.send(f'Translated text: {translated_text}', ephemeral=True)
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', verify=False)
response = get("https://api.quotable.io/random?maxLength=230", verify=False)
my_quote = response.json()
await interaction.response.send_message(embed=embed_from_quote(my_quote))
@ -179,10 +198,12 @@ async def quote(interaction: discord.Interaction):
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')
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')
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
@ -190,37 +211,48 @@ async def get_weather_json(city: str):
@client.tree.command()
@app_commands.describe(city='The city you want to get the weather for')
@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)
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']
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 '
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)
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}')
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')
await interaction.response.send_message("The best programming language is Rust")
@client.tree.command()
async def color(interaction: discord.Interaction):
@ -249,7 +281,8 @@ def fib(n: int) -> int:
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}!')
await channel.send(f"Welcome {member.mention} to {guild.name}!")
if __name__ == "__main__":
client.run(TOKEN)