Format
This commit is contained in:
parent
217905e6a8
commit
f72ec9151f
1 changed files with 82 additions and 49 deletions
131
bot.py
131
bot.py
|
@ -30,40 +30,46 @@ md = md - timedelta(milliseconds=600)
|
||||||
mid = md.timetz()
|
mid = md.timetz()
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
def embed_from_quote(my_quote: dict):
|
def embed_from_quote(my_quote: dict):
|
||||||
embed = discord.Embed(title="Quote")
|
embed = discord.Embed(title="Quote")
|
||||||
embed.description = my_quote['content']
|
embed.description = my_quote["content"]
|
||||||
embed.set_author(name=my_quote['author'])
|
embed.set_author(name=my_quote["author"])
|
||||||
return embed
|
return embed
|
||||||
|
|
||||||
|
|
||||||
class Dropdown(discord.ui.Select):
|
class Dropdown(discord.ui.Select):
|
||||||
def __init__(self):
|
def __init__(self):
|
||||||
|
|
||||||
# Set the options that will be presented inside the dropdown
|
# Set the options that will be presented inside the dropdown
|
||||||
options = [
|
options = [
|
||||||
discord.SelectOption(
|
discord.SelectOption(
|
||||||
label='Red', description='Your favourite colour is red', emoji='🟥'),
|
label="Red", description="Your favourite colour is red", emoji="🟥"
|
||||||
|
),
|
||||||
discord.SelectOption(
|
discord.SelectOption(
|
||||||
label='Green', description='Your favourite colour is green', emoji='🟩'),
|
label="Green", description="Your favourite colour is green", emoji="🟩"
|
||||||
|
),
|
||||||
discord.SelectOption(
|
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 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 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
|
# The options parameter defines the dropdown options. We defined this above
|
||||||
super().__init__(placeholder='Choose your favourite colour...',
|
super().__init__(
|
||||||
min_values=1, max_values=1, options=options)
|
placeholder="Choose your favourite colour...",
|
||||||
|
min_values=1,
|
||||||
|
max_values=1,
|
||||||
|
options=options,
|
||||||
|
)
|
||||||
|
|
||||||
async def callback(self, interaction: discord.Interaction):
|
async def callback(self, interaction: discord.Interaction):
|
||||||
# Use the interaction object to send a response message containing
|
# Use the interaction object to send a response message containing
|
||||||
# the user's favourite colour or choice. The self object refers to the
|
# 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
|
# Select object, and the values attribute gets a list of the user's
|
||||||
# selected options. We only want the first one.
|
# 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):
|
class DropdownView(discord.ui.View):
|
||||||
|
@ -102,75 +108,88 @@ client = MyClient(client_intents=intents)
|
||||||
|
|
||||||
@client.event
|
@client.event
|
||||||
async def on_ready():
|
async def on_ready():
|
||||||
print(f'Logged in as {client.user} (ID: {client.user.id})')
|
print(f"Logged in as {client.user} (ID: {client.user.id})")
|
||||||
print('------')
|
print("------")
|
||||||
|
|
||||||
|
|
||||||
@client.tree.command()
|
@client.tree.command()
|
||||||
async def hello(interaction: discord.Interaction):
|
async def hello(interaction: discord.Interaction):
|
||||||
"""Says hello!"""
|
"""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()
|
@client.tree.command()
|
||||||
@app_commands.describe(
|
@app_commands.describe(
|
||||||
first_value='The first value you want to add something to',
|
first_value="The first value you want to add something to",
|
||||||
second_value='The value you want to add to the first value',
|
second_value="The value you want to add to the first value",
|
||||||
)
|
)
|
||||||
async def add(interaction: discord.Interaction, first_value: int, second_value: int):
|
async def add(interaction: discord.Interaction, first_value: int, second_value: int):
|
||||||
"""Adds two numbers together."""
|
"""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()
|
@client.tree.command()
|
||||||
@app_commands.rename(text_to_send='text')
|
@app_commands.rename(text_to_send="text")
|
||||||
@app_commands.describe(text_to_send='Text to send in the current channel')
|
@app_commands.describe(text_to_send="Text to send in the current channel")
|
||||||
async def send(interaction: discord.Interaction, text_to_send: str):
|
async def send(interaction: discord.Interaction, text_to_send: str):
|
||||||
"""Sends the text into the current channel."""
|
"""Sends the text into the current channel."""
|
||||||
await interaction.response.send_message(text_to_send)
|
await interaction.response.send_message(text_to_send)
|
||||||
|
|
||||||
|
|
||||||
@client.tree.command()
|
@client.tree.command()
|
||||||
@app_commands.describe(member='The member you want to get the joined date from; defaults to the user who uses the '
|
@app_commands.describe(
|
||||||
'command')
|
member="The member you want to get the joined date from; defaults to the user who uses the "
|
||||||
async def joined(interaction: discord.Interaction, member: Optional[discord.Member] = None):
|
"command"
|
||||||
|
)
|
||||||
|
async def joined(
|
||||||
|
interaction: discord.Interaction, member: Optional[discord.Member] = None
|
||||||
|
):
|
||||||
"""Says when a member joined."""
|
"""Says when a member joined."""
|
||||||
member = member or interaction.user
|
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):
|
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):
|
async def translate(interaction: discord.Interaction, message: discord.Message):
|
||||||
await interaction.response.defer(ephemeral=True)
|
await interaction.response.defer(ephemeral=True)
|
||||||
"""Translate text to another language."""
|
"""Translate text to another language."""
|
||||||
try:
|
try:
|
||||||
response = post('https://translate.argosopentech.com/translate', json={
|
response = post(
|
||||||
'q': message.content,
|
"https://translate.argosopentech.com/translate",
|
||||||
'source': 'fr',
|
json={"q": message.content, "source": "fr", "target": "en"},
|
||||||
'target': 'en'
|
timeout=30,
|
||||||
}, timeout=30)
|
)
|
||||||
response.raise_for_status()
|
response.raise_for_status()
|
||||||
except Exception as e:
|
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
|
return
|
||||||
try:
|
try:
|
||||||
json = response.json()
|
json = response.json()
|
||||||
translated_text = json['translatedText']
|
translated_text = json["translatedText"]
|
||||||
except Exception as e:
|
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
|
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()
|
@client.tree.command()
|
||||||
async def quote(interaction: discord.Interaction):
|
async def quote(interaction: discord.Interaction):
|
||||||
"""Sends a random quote."""
|
"""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()
|
my_quote = response.json()
|
||||||
await interaction.response.send_message(embed=embed_from_quote(my_quote))
|
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):
|
async def get_weather_json(city: str):
|
||||||
try:
|
try:
|
||||||
weather_response = get(
|
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()
|
weather_response.raise_for_status()
|
||||||
forecast_response = get(
|
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()
|
forecast_response.raise_for_status()
|
||||||
except Exception as e:
|
except Exception as e:
|
||||||
raise e
|
raise e
|
||||||
|
@ -190,37 +211,48 @@ async def get_weather_json(city: str):
|
||||||
|
|
||||||
|
|
||||||
@client.tree.command()
|
@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):
|
async def weather(interaction: discord.Interaction, city: str):
|
||||||
"""Get the current weather"""
|
"""Get the current weather"""
|
||||||
try:
|
try:
|
||||||
weather_response, forecast_response = await get_weather_json(city)
|
weather_response, forecast_response = await get_weather_json(city)
|
||||||
except Exception as _e:
|
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
|
return
|
||||||
current_temp = weather_response['current']['temp_c']
|
current_temp = weather_response["current"]["temp_c"]
|
||||||
forecast = forecast_response['forecast']['forecastday'][0]
|
forecast = forecast_response["forecast"]["forecastday"][0]
|
||||||
forecast_min = forecast['day']['mintemp_c']
|
forecast_min = forecast["day"]["mintemp_c"]
|
||||||
forecast_max = forecast['day']['maxtemp_c']
|
forecast_max = forecast["day"]["maxtemp_c"]
|
||||||
embed = discord.Embed(title="Weather")
|
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)
|
await interaction.response.send_message(embed=embed)
|
||||||
|
|
||||||
|
|
||||||
# A command tha tell the time until midnight
|
# A command tha tell the time until midnight
|
||||||
@client.tree.command()
|
@client.tree.command()
|
||||||
async def midnight(interaction: discord.Interaction):
|
async def midnight(interaction: discord.Interaction):
|
||||||
"""Sends the time until midnight."""
|
"""Sends the time until midnight."""
|
||||||
paris_dt = datetime.now(ZoneInfo("Europe/Paris"))
|
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
|
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
|
# A command that say what the best programming language is
|
||||||
@client.tree.command()
|
@client.tree.command()
|
||||||
async def best_language(interaction: discord.Interaction):
|
async def best_language(interaction: discord.Interaction):
|
||||||
"""Sends the best programming language."""
|
"""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()
|
@client.tree.command()
|
||||||
async def color(interaction: discord.Interaction):
|
async def color(interaction: discord.Interaction):
|
||||||
|
@ -249,7 +281,8 @@ def fib(n: int) -> int:
|
||||||
async def on_member_join(member: discord.Member):
|
async def on_member_join(member: discord.Member):
|
||||||
guild = member.guild
|
guild = member.guild
|
||||||
channel = client.get_channel(GENERAL_CHANNEL_ID)
|
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__":
|
if __name__ == "__main__":
|
||||||
client.run(TOKEN)
|
client.run(TOKEN)
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue