1

I'm trying to replicate some games from a few discord bots on my server, one of these is slots.

Using node.js for reference.

I've created a clone of it however am missing one element -> Emoji cycling.

Basically in the same Embed message the bot gets each icon to cycle through; like an actual slot machine and then stop on one, then it moves to the next icon and does the same. My bot processes all of this at once so if you type the +slots 1000 it sends a messsage back with the slots already chosen.

How would I get my bot to cycle through emojis then move to the next and do the same before showing all of the information?

Is there some kind of "Wait for seconds" function I'm missing?

I'm assuming the animated icon is just a custom emoji that goes through each of the options, however I'm still unsure how to actually go:

Animated emoji -> 0.5s later pick one -> 0.5s later animate seconds -> 0.5s later pick one etc.

Cheers!

See picture here:

cloned
  • 6,346
  • 4
  • 26
  • 38
Oldog
  • 11
  • 2

2 Answers2

2

So I actually had this same question about this same bot. I was disappointed that there weren't any answers here, but I ended up figuring it out. I initially tried just updating the emoji fields a couple of times a second to make it look like it was cycling. This approach doesn't work, however, because of the 5 "thing" rate limit (you can only do 5 message edits (or things) in 1 second).

What they are actually doing (like you hinted at) is using a custom animated emoji. If you want to use their emoji for the cycle, you'll find it here. You then would need to download this gif and import it into your bot's discord server. You can change the tag for the emoji, which is the :name:, that you use to call it in your server. I called mine :cycle_test:. If then enter the following text into a chat with the bot or on server channel it will give you the unique identifier for the custom emoji: \:cycle_test:. Just right click on your message and click "quote" and it will give you this unique identifier, which looked like this for me: <a:cycle_test:732311116742393856>.

Now that you have this identifier for your animated emoji you can then use it in a discord embed. I was using Python, but the syntax should be quite similar:

embed.add_field(name="Slot results", value="<a:cycle_test:732311116742393856>")

. Then you will want to add code to wait for a second or two and as you go through all of your slot results you will stop them one column at a time. You only do an edit when you want to display a result from a column, which if you give it about a second between each result, it won't hit the limit.

Here's the code I used (again, it is in Python but the logic should help you figure it out)

slot_embed = discord.Embed(title="**Slot Machine**")
slot_embed.add_field(name="Results",value=f"<a:cycle_test:732311116742393856> <a:cycle_test:732311116742393856> <a:cycle_test:732311116742393856>")
sent_embed = await ctx.send(embed=slot_embed)
current_slot_pics = ["<a:cycle_test:732311116742393856>","<a:cycle_test:732311116742393856>","<a:cycle_test:732311116742393856>"]
for i in range(0,len(slot_results_pic)):
    await asyncio.sleep(1.5)
    current_slot_pics[i] = slot_results_pic[i]
    new_slot_embed = None
    new_slot_embed = discord.Embed(title="**Slot Machine**")
    slot_results_str = ""        
    for thisSlot in current_slot_pics:
        slot_results_str += f"{thisSlot} "
    new_slot_embed.add_field(name="Results",value=f"{slot_results_str}")
    await sent_embed.edit(embed=new_slot_embed)

Walking through the code, what I'm doing is first setting up a embed to send out that has the animated emoji "cycle_test." I send the embed out and start a for loop for the size of my slot_results_pic variable which is a variable that I setup earlier to determine what slots they picked. (Note, the results of the slots are already known long before it is displayed for the user, all of this code is just gimicky to make it look cooler). I have an array called "current_slot_pics" which contains the data that is currently being displayed to the user. Notice how they are all set to to the animated emoji at the beginning.

Next, I run a sleep command so that it cycles for 1.5 seconds before showing a result. Now I set the first element of the "current_slot_pics" array to be the first result that the program calculated earlier to be the first slot. After this I make a new embed field and send it as an edit to the original message "slot_embed." The variable i helps keep track of how far we are along in the results and helps me set the proper value of current_slot_pics.

Here is what my finished results looked like:

enter image description here

Final note: if you want to have an animation with the emojis that you have chosen for the slot machine, you would have to make a gif like the one that gambling bot uses and upload it as a custom emoji.

Spencer A
  • 33
  • 2
  • 7
0

lol 4 years and I was just looking for a solution, so I made a little script to generate them :), feel free to replace the emojis and use it however

import requests
from PIL import Image, ImageSequence
from io import BytesIO

# List of emojis
emojis = [
    "",  # :apple:
    "",  # :cherries:
    "",  # :grapes:
    "",  # :lemon:
    "",  # :peach:
    "",  # :tangerine:
    "",  # :watermelon:
    "",  # :strawberry:
    "",  # :banana:
    "",  # :pineapple:
    "",  # :kiwi:
    "",  # :pear:
    "",  # :crown:
    "",  # :gem:
]

# Convert the emojis to their corresponding Twemoji URLs
emoji_urls = [
    f"https://twemoji.maxcdn.com/v/latest/72x72/{'-'.join(hex(ord(c))[2:] for c in emoji)}.png"
    for emoji in emojis
]

print("Emoji URLs:", emoji_urls)

# Download the emojis and open them as PIL Images
emoji_images = []
for url in emoji_urls:
    print("Downloading:", url)
    response = requests.get(url)
    print("Response status code:", response.status_code)
    img = Image.open(BytesIO(response.content))
    emoji_images.append(img.convert("RGBA"))  # Convert image to RGBA

print("Downloaded all emojis")

# Create a new GIF image
print("Creating GIF")
output = BytesIO()

# Set the disposal method for each frame to '2' (replace)
gif_images = [img.copy() for img in emoji_images]
for img in gif_images:
    img.info['duration'] = 100  # Decrease this value to make the animation faster
    img.info['transparency'] = 255
    img.info['disposal'] = 2  # 'replace'

# Save the images as a GIF
gif_images[0].save(output, format='GIF', append_images=gif_images[1:], save_all=True, loop=0)  # Set loop to 0 for infinite loop

# Get the GIF data
gif_data = output.getvalue()

print("GIF created")

# Save the GIF as a file
with open('spin.gif', 'wb') as f:
    f.write(gif_data)

print("GIF saved as spin.gif")

# Now you can send the GIF data in a Discord message
# await ctx.send(file=discord.File(BytesIO(gif_data), 'spin.gif'))

heres what it generates heres what it generates

Overtime
  • 1
  • 1
  • Your answer could be improved with additional supporting information. Please [edit] to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers [in the help center](/help/how-to-answer). – Community Jul 04 '23 at 13:55