python - 如何在一行中发送所有这些消息(discord.py)?
问题描述
if results=='3':
with open("somerandomwords.txt") as f:
words = f.read().split()
word_dict = defaultdict(list)
for word, next_word in zip(words, words[1:]):
word_dict[word].append(next_word)
word="the"
while not word.endswith('.'):
await ctx.send(word)
time.sleep(2)
word = random.choice(word_dict[word])
await ctx.send(word)
time.sleep(50)
print('going')
每个字都是自己发送的。我怎样才能使它形成句子(在不和谐的单个消息上发送单词)?
解决方案
首先你必须使用await asyncio.sleep(1)
而不是time.sleep
因为 time.sleep 会让你的整个机器人睡眠而不是函数本身。也不需要字典,列表就可以了。
我没有清楚地理解这个想法,但这就是我得到的。
if results == '3':
with open("somerandomwords.txt") as f:
words = f.read().splitlines() # a list
random.shuffle(words)
words_to_send = []
for word in words:
if word.endswith('.'):
break
else:
words_to_send.append(word)
await ctx.send(' '.join(words_to_send))
推荐阅读
- android - Kotlin 最佳实践:声明/初始化实例变量
- reactjs - 更新用户名后不重新呈现个人资料页面
- coq - Coq:在假设或目标中使用“forall”重写
- javascript - 续集动态数据库配置
- javascript - 当 scrollTop 为 0 时 React.js,检测到我们在第一部分,使用 react-full-page npm
- javascript - 使用 javascript 使用选择器更改类
- angular - Angular MatDialog 附加到 HTML 而不是显示为弹出窗口
- javascript - 在 AJAX 中获取 http://url/route?id=clinic_id 404(未找到)
- php - 作曲家创建项目 symfony/网站骨架我的项目
- python - Cron 作业一次执行两次