【发布时间】:2020-07-07 20:13:39
【问题描述】:
我一直在为我的 discord 机器人开发一个 ..joke 函数,它读取一个包含笑话列表的文件并随机选择一个。当.joke 运行时,什么都没有发生,没有错误,什么都没有。经过一番调试,我发现问题出在打开文件的位置。但是当我在不同的文件(单独)中运行读取功能时,它可以工作。我做错了什么?
import random
import discord
from discord.ext import commands
class joke(commands.Cog):
def __init__(self, client):
self.client = client
@commands.Cog.listener()
async def on_ready(self):
print("loaded!")
@commands.command()
async def joke(self, ctx):
file = open("jokes.txt", "r", encoding="utf8") #it appears it stops working here
jokes = file.readlines()
ctx.send(random.choice(jokes))
def setup(client):
client.add_cog(joke(client))
【问题讨论】:
标签: python python-3.x discord.py-rewrite