简体   繁体   中英

Trying to make a slot machine bot with discord.py

This is my code below:

import discord
import random
import time

tst = [1, 2, 3]

if "!roll" in message.content.lower():
    first = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    second = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    third = [await message.channel.send(str(random.choice(tst)).format(message))]
    time.sleep(1)
    if first == second == third:
        await message.channel.send("you win!".format(message))

The code works but doesn't send the message when you win.

I figured I am doing something wrong but can't figure out the correct way to write the code.

It never sends something because the if-statement is never true. You're comparing three lists of discord.Message instances for three messages. Those are all different, so [message1] == [message2] == [message3] will never be True . Compare the values instead.

Also, .format(message) doesn't do anything at all, and I'm not sure what you're expecting it to do. You should just remove it (or make it do something useful).

first = random.choice(tst)
second = random.choice(tst)
third = random.choice(tst)

await message.channel.send(str(first))
await message.channel.send(str(second))
await message.channel.send(str(third))

if first == second == third:
    await message.channel.send("You win!")

Also,

if ".roll" in message.content:lower():

Consider using commands instead of manually parsing everything. There's a basic example on how they work on the GitHub repo .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM