【问题标题】:How can I send all these messages in one single line (discord.py)?如何在一行中发送所有这些消息(discord.py)?
【发布时间】:2020-09-22 13:45:45
【问题描述】:
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')

每个单词都是自己发送的。我怎样才能使它形成句子(在不和谐的单个消息上发送单词)?

【问题讨论】:

  • 你为什么要把它们插入字典?
  • 你想把 somerandomwords.txt 中的单词粘在一起组成一个句子吗?
  • @YannickFunk 是的,谢谢你的澄清。我想用 somerandomwords.txt 中的一些词来造句。
  • 你能提供几行你的txt文件吗?

标签: python python-3.x discord.py discord.py-rewrite


【解决方案1】:

首先你必须使用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))

【讨论】:

    猜你喜欢
    • 2020-10-15
    • 2021-05-03
    • 1970-01-01
    • 2022-10-07
    • 2021-06-06
    • 1970-01-01
    • 2021-07-08
    • 2020-10-01
    • 2021-10-15
    相关资源
    最近更新 更多