我的代码看不到 message.content,除非 ping bot / TypeError:“NoneType”对象不可下标

问题描述 投票:0回答:2

我使用huggingface的InferenceAPI基于dialoGPT LM编写了discord bot。 (我在终端中测试了这段代码,它工作得很好)。机器人启动并传递初始化,但是当我尝试发送任何消息时,它在控制台中给出此错误。我在这里检查了这个确切的问题,但没有正常的解释是什么帮助解决了作者的问题。我正在发送错误图片(还有一个可能来自第一个错误)和完整代码,但出于明显原因,API 令牌除外 Errors

import requests
import discord
import speech_recognition as sr
from gtts import gTTS
import io

API_URL = "https://api-inference.huggingface.co/models/microsoft/DialoGPT-medium"
headers = {"Authorization": "Bearer *huggingface token*"}

client = discord.Client(intents=discord.Intents.default())
recognizer = sr.Recognizer()
text = ''

def query(payload):
    response = requests.post(API_URL, headers=headers, json=payload)
    return response.json()
    
def write_doc(text):
       file = open('c:/Users/SuSl1KK/Documents/Code/Project_Alma/v1/corpus.txt', 'r')
       Temp = file.read()
       file.close()
       file = open('c:/Users/SuSl1KK/Documents/Code/Project_Alma/v1/corpus.txt', 'w')
       file.write(Temp + text)
       file.close() 
       return
       
def txtToSpeech(text_to_say, voice_client):
    tts = gTTS(text=text_to_say, lang='en')
    fp = io.BytesIO()
    tts.write_to_fp(fp)
    fp.seek(0)
    if voice_client and voice_client.is_connected():
        voice_client.play(discord.FFmpegPCMAudio(fp))
    return
        
@client.event
async def on_ready():
    print(f'{client.user} has connected to Discord!')
    
@client.event
async def on_message(message):
    if message.author == client.user:
        return
    
    if message.content.startswith('!come here'):
        channel = message.author.voice.channel
        await channel.connect()
        return
    
    if message.content.startswith('!bye'):
        voice_client = message.guild.voice_client
        if voice_client:
            await voice_client.disconnect()
            return
            
    if message.content.startswith('!help'):
        await message.channel.send('Here is commands to control me: \n !come here - join voice chat \n !bye - disconnect from voice chat')
        return
        
    Ans = query({"inputs": message.content})
    await message.channel.send(Ans["generated_text"])
    text = text + 'input: ' + message.content + '\n' + 'output: ' + Ans["generated_text"] + '\n\n'
    write_doc(text)

@client.event
async def on_voice_state_update(member, before, after):
    if before.channel is None and after.channel is not None:
        if client.user in after.channel.members:  
            voice_client = await after.channel.connect()

            while voice_client.is_connected():
                audio_data = voice_client.listen()
                try:
                    speech_into_text = recognizer.recognize_google(audio_data)
                    Ans = query({"inputs": speech_into_text})
                    txtToSpeech(Ans, voice_client)
                    text = text + 'input: ' + speech_into_text + '\n' + 'output: ' + Ans["generated_text"] + '\n\n'
                    write_doc(text)
                except sr.UnknownValueError:
                    pass

client.run('*discord token*')

正如你所看到的,我尝试做以前的文章作者所做的事情 - 向所有功能添加“返回”,即使他们不需要它,但正如预期的那样,它没有帮助

编辑:我尝试打印response.json(),为什么我的代码不读取message.content(除非我ping我的机器人)并发送空提示,所以这是一个问题,但除此之外,即使当response.json() 包含“ generated_text”,它返回: 答案 = Ans['生成的文本'] 类型错误:“NoneType”对象不可订阅

python discord.py huggingface-transformers
2个回答
0
投票

已经不重要了不是吗?我们完了。祝一切顺利!


0
投票

问题解决了。当我尝试跟踪空输入的问题时,我已将 LM 函数中的 return 更改为 print() ,因此它只是没有返回任何内容,从而导致此错误。此外,由于代码和开发者门户中的意图不正确,机器人无法读取短信

© www.soinside.com 2019 - 2024. All rights reserved.