Skip to content

Commit

Permalink
[Elements] 1.1.1 run get all elements in the default executor so it d…
Browse files Browse the repository at this point in the history
…oesn't block.

Add typing to the command so you know it's working and defer since getting all elements can take time breaking the slash command version.
  • Loading branch information
TrustyJAID committed Nov 2, 2023
1 parent a6f661c commit 537fa3c
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 23 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ TrustyJAID's Cogs for [Red-DiscordBot](https://github.com/Cog-Creators/Red-Disc
| Conversions | 1.3.2 | <details><summary>Conversions for currencies, crypto-currencies, and stocks.</summary>Conversions for currencies, crypto-currencies, and stocks.</details> | TrustyJAID |
| CrabRave | 1.1.3 | <details><summary>Make Crab rave videos, in discord!</summary>Create your very own Crab Rave videos with custom text! This cog requires FFMPEG, moviepy (https://github.com/Zulko/moviepy), and imagemagick to work. This cog downloads a template video and font file which is then saved locally and generates crab rave videos from the template. Old videos are deleted after uploading. This cog may consume heavy resources rendering videos.</details> | DankMemer Team, TrustyJAID, and thisisjvgrace |
| | 1.9.0 | <details><summary>Thanks for installing</summary></details> | |
| Elements | 1.1.0 | <details><summary>Periodic table of elements</summary>Get a plethora of information about elements on the periodic table.</details> | TrustyJAID |
| Elements | 1.1.1 | <details><summary>Periodic table of elements</summary>Get a plethora of information about elements on the periodic table.</details> | TrustyJAID |
| Encoding | 1.3.1 | <details><summary>Encode messages into various types of encoding.</summary>Encode messages into various types of encoding. Encoding types include: DNA, binary, Caeser cipher, hex, base 64, character, and braille.</details> | TrustyJAID |
| EventPoster | 2.1.3 | <details><summary>Admin approved announcments/events</summary>Allow users to setup and host events to be approved by admins.</details> | TrustyJAID |
| ExtendedModLog | 2.12.3 | <details><summary>ExtendedModLog, track changes made in the server.</summary>Log changes within the server using extended modlogs, an extension of RedBot cores modlog.</details> | RePulsR and TrustyJAID |
Expand Down
50 changes: 28 additions & 22 deletions elements/core.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import asyncio
from typing import Any, List, NamedTuple, Optional, Union

import discord
Expand Down Expand Up @@ -103,25 +104,25 @@ async def autocomplete(
def get_xray_wavelength(element: mendeleev.models.Element) -> str:
try:
ka = 1239.84 / (
13.6057 * ((element.atomic_number - 1) ** 2) * ((1 / 1**2) - (1 / 2**2))
13.6057 * ((element.atomic_number - 1) ** 2) * ((1 / 1 ** 2) - (1 / 2 ** 2))
)
except Exception:
ka = ""
try:
kb = 1239.84 / (
13.6057 * ((element.atomic_number - 1) ** 2) * ((1 / 1**2) - (1 / 3**2))
13.6057 * ((element.atomic_number - 1) ** 2) * ((1 / 1 ** 2) - (1 / 3 ** 2))
)
except Exception:
kb = ""
try:
la = 1239.84 / (
13.6057 * ((element.atomic_number - 7.4) ** 2) * ((1 / 1**2) - (1 / 2**3))
13.6057 * ((element.atomic_number - 7.4) ** 2) * ((1 / 1 ** 2) - (1 / 2 ** 3))
)
except Exception:
la = ""
try:
lb = 1239.84 / (
13.6057 * ((element.atomic_number - 7.4) ** 2) * ((1 / 1**2) - (1 / 2**4))
13.6057 * ((element.atomic_number - 7.4) ** 2) * ((1 / 1 ** 2) - (1 / 2 ** 4))
)
except Exception:
lb = ""
Expand Down Expand Up @@ -377,7 +378,7 @@ async def interaction_check(self, interaction: discord.Interaction):
class Elements(commands.Cog):
"""Display information from the periodic table of elements"""

__version__ = "1.1.0"
__version__ = "1.1.1"
__author__ = "TrustyJAID"

def __init__(self, bot):
Expand Down Expand Up @@ -415,29 +416,34 @@ async def element(
`measurement` can be any of the Elements data listed here
https://mendeleev.readthedocs.io/en/stable/data.html#electronegativities
"""
if measurement is None or element is None:
elements = mendeleev.get_all_elements()
page_start = 0
if element is not None:
page_start = element.atomic_number - 1

source = ElementPages(elements)
if measurement is None or element is None:
async with ctx.typing():
loop = asyncio.get_running_loop()
task = loop.run_in_executor(None, mendeleev.get_all_elements)
elements = await task
page_start = 0
if element is not None:
page_start = element.atomic_number - 1

source = ElementPages(elements)
await BaseView(
source=source,
cog=self,
page_start=page_start,
).start(ctx)
return
else:
extra_1 = ""
extra_2 = ""
name = measurement.name
units = measurement.units
data = getattr(element, measurement.key, "")
if measurement.key == "lattice_structure":
extra_1, extra_2 = LATTICES[element.lattice_structure]
if measurement.key == "xrf":
extra_2 = get_xray_wavelength(element)

msg = f" {element.name}:{name} {data} {extra_1} {extra_2} {units}\n"
async with ctx.typing():
extra_1 = ""
extra_2 = ""
name = measurement.name
units = measurement.units
data = getattr(element, measurement.key, "")
if measurement.key == "lattice_structure":
extra_1, extra_2 = LATTICES[element.lattice_structure]
if measurement.key == "xrf":
extra_2 = get_xray_wavelength(element)

msg = f" {element.name}:{name} {data} {extra_1} {extra_2} {units}\n"
await ctx.send(msg)

0 comments on commit 537fa3c

Please sign in to comment.