【发布时间】:2022-07-08 04:38:42
【问题描述】:
import discord
import os
import requests
import json
import random
from replit import db
client = discord.Client()
这对吗?
def get_pic():
pic_url = requests.get('https://api.thecatapi.com/v1/images/search').json()['url']
return pic_url
@client.event
async def on_ready():
print("We have logged in as {0.user}".format(client))
@client.event
async def on_message(message):
if message.author == client.user:
return
这是我应该做的。运行时出现错误。 另外,我需要在这段代码中使用 api 密钥吗?
if message.content.startswith('pls catto'):
pic = get_pic()
await message.channel.send(content=pic)
client.run(os.environ['TOKEN'])
【问题讨论】:
-
错误是什么?
-
Traceback(最近一次调用最后):文件“/home/runner/Test-Bot/venv/lib/python3.8/site-packages/discord/client.py”,第 343 行,在_run_event await coro(*args, **kwargs) 文件“main.py”,第 24 行,on_message pic = get_pic() 文件“main.py”,第 10 行,get_pic pic_url = requests.get('api.thecatapi.com/v1/images/search').json()['url'] TypeError: 列表索引必须是整数或切片,而不是 str
标签: python discord discord.py