簡體   English   中英

從列表中獲取隨機元素(在其他文件中)

[英]Get random element from list (in other file)

(對不起我的英語:V)我正在用 discord.py 制作 discord 機器人。

import random
class List():
    list = [
        "very"
        "big"
        "list"
         ]
    def choice(self, list):
        result = random.choice(list)
        return result

這不是那個列表(它非常大)。 我必須從這個列表中選擇隨機元素。

import List
list = List
@bot.command
async def send(ctx):
    element = list.choice(list)
    ctx.send(element)

buuuut 它引發了一個異常:

RuntimeWarning: coroutine 'Messageable.send' was never awaited
ctx.send(element)
RuntimeWarning: Enable tracemalloc to get the object allocation traceback

ctx.send是一個協程,而不是常規的 function,您需要等待它。 所以: await ctx.send(...)是正確的語法

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM