python - 尝试使用 discord.py 制作老虎机机器人
问题描述
这是我下面的代码:
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))
该代码有效,但在您获胜时不会发送消息。
我想我做错了什么,但无法找出编写代码的正确方法。
解决方案
它从不发送任何东西,因为 if 语句永远不会为真。您正在比较discord.Message
三个消息的三个实例列表。这些都是不同的,所以[message1] == [message2] == [message3]
永远不会True
。而是比较这些值。
此外,.format(message)
根本不做任何事情,我不确定你期望它做什么。您应该删除它(或让它做一些有用的事情)。
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!")
还,
如果在 message.content.lower() 中出现“!roll”:
考虑使用commands
而不是手动解析所有内容。有一个关于它们如何在GitHub repo上工作的基本示例。
推荐阅读
- mongodb - Mongoose TypeError:无法读取 null 的属性“insertOne”
- reactjs - React Jest setInterval 测试覆盖率
- azure - 无法连接到 Azure 迁移服务中的源服务器
- julia - Python函数调用的参数是Julia中的保留字
- amazon-web-services - AWS ElastiCache (Redis) 的 Spring Data 原子增量
- dc.js - DC 热图如何为正值和负值指定 2 色图
- reactjs - Webpack:错误:无法解析“MyCustomPath/”
- python - 以更好的模式重写我的简单 Python 代码
- sql - 通过多个表循环查询
- reactjs - 如果使用 scaleLinear,如何将位置设置为刻度线