holy shit esef still hasnt forgotten about this bot

This commit is contained in:
esef committed 2026-10-03 17:59:11 +03:00
1 parent 4fa7d5ca50
commit 2691806453
6 files changed
+322 -101

No files matched your search

+14 -18
View File
@@ -3,6 +3,7 @@ import time
import json
import aiohttp
import discord
from discord import app_commands
from discord.ext import commands
# load aliases from json
@@ -47,37 +48,32 @@ class CurrencyConverter(commands.Cog):
_RATE_CACHE[base] = (now, rates)
return rates
@commands.command(name='currency')
async def currency(self, ctx, amount: float = 1, from_cur: str = None, to_cur: str = None):
"""Convert *amount* from *from_cur* to *to_cur*.
If *amount* is omitted the command is parsed as ``!currency <from> <to>``.
"""
# discord.py passes arguments left-to-right; we need to support two signatures.
# When the user supplies only two strings, *amount* will be parsed as the first string.
if from_cur is None and to_cur is None:
# only two arguments were given, treat them as from/to and default amount=1
to_cur = amount # type: ignore
from_cur = str(amount) # type: ignore
amount = 1.0
@app_commands.command(name='currency')
@app_commands.rename(from_cur='from', to_cur='to')
@app_commands.describe(from_cur='Currency to convert from, e.g. usd, euro, lira',
to_cur='Currency to convert to',
amount='How much to convert (default 1)')
async def currency(self, interaction: discord.Interaction, from_cur: str, to_cur: str, amount: float = 1.0):
"""Convert money between currencies."""
try:
from_code = self._norm(str(from_cur))
to_code = self._norm(str(to_cur))
from_code = self._norm(from_cur)
to_code = self._norm(to_cur)
except ValueError as e:
return await ctx.reply(f"❌ {e}")
return await interaction.response.send_message(f"❌ {e}", ephemeral=True)
try:
rates = await self._get_rates(from_code)
except Exception:
return await ctx.reply('❌ could not fetch conversion rates, try again later')
return await interaction.response.send_message('❌ could not fetch conversion rates, try again later', ephemeral=True)
rate = rates.get(to_code)
if rate is None:
return await ctx.reply(f"❌ unsupported conversion: {from_code} → {to_code}")
return await interaction.response.send_message(f"❌ unsupported conversion: {from_code} → {to_code}", ephemeral=True)
result = amount * rate
embed = discord.Embed(title='Currency conversion', color=0x00ff00)
embed.add_field(name='From', value=f"{amount:g} {from_code}", inline=True)
embed.add_field(name='To', value=f"{result:g} {to_code}", inline=True)
embed.add_field(name='Rate', value=f"1 {from_code} = {rate:g} {to_code}", inline=False)
embed.set_footer(text='Data from frankfurter.app')
await ctx.reply(embed=embed)
await interaction.response.send_message(embed=embed)
async def setup(bot):
await bot.add_cog(CurrencyConverter(bot))