首页 > 解决方案 > 如何在一行中发送所有这些消息(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')

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

标签: pythonpython-3.xdiscord.pydiscord.py-rewrite

解决方案


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

推荐阅读