首页 > 解决方案 > 从列表中获取随机元素(在其他文件中)

问题描述

(对不起我的英语:V)我正在用 discord.py 制作一个不和谐的机器人。

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

标签: pythondiscord.py

解决方案


ctx.send是协程而不是常规函数,您需要等待它。所以:await ctx.send(...)是正确的语法


推荐阅读