Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
386 views
in Technique[技术] by (71.8m points)

python - 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.

question from:https://stackoverflow.com/questions/65862632/trying-to-make-a-slot-machine-bot-with-discord-py

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...