如何在一行中发送所有这些消息(discord.py)?

时间:2020-09-21 19:35:56

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

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')

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

1 个答案:

答案 0 :(得分:0)

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