【问题标题】:TypeError: does not support the buffer interface Twitch IRC Chat BotTypeError: 不支持缓冲接口 Twitch IRC Chat Bot
【发布时间】:2015-01-28 00:24:51
【问题描述】:

首先,我无法在任何地方找到答案。我不确定它是我需要导入的东西,还是只是代码做得不好。简而言之,通过这个项目,我将通过 SMS 消息和许多其他内容制作一个更好的 twitch 追随者通知器。

编辑:整个崩溃日志如下:

line 16 in <module>
irc.send('PASS ' + password + '\r\n')
TypeError: Does not support the buffer interface

另外,我不得不反复双击该文件才能得到这个,所以如果它有点不对劲,我很抱歉。我无法让编码的崩溃日志工作。

import socket #imports module allowing connection to IRC
import threading #imports module allowing timing functions

bot_owner = 'BetterFollowerBot'
nick = 'BetterFollowerBot'
channel = '#BetterFollowerBot'
server = 'irc.twitch.tv'
password = '~Took This Out~'

queue = 0 #sets variable for anti-spam queue functionality

irc = socket.socket()
irc.connect((server, 6667)) #connects to the server

#sends variables for connection to twitch chat
irc.send('PASS ' + password + '\r\n')
irc.send('USER ' + nick + ' 0 * :' + bot_owner + '\r\n')
irc.send('NICK ' + nick + '\r\n')
irc.send('JOIN ' + channel + '\r\n') 

def message(msg): #function for sending messages to the IRC chat
    global queue
    queue = queue + 1
    print (queue)
    if queue < 20: #ensures does not send >20 msgs per 30 seconds.
        irc.send('PRIVMSG ' + channel + ' :' + msg + '\r\n')
    else:
        print ('Message deleted')

def queuetimer(): #function for resetting the queue every 30 seconds
    global queue
    print ('queue reset')
    queue = 0
    threading.Timer(30,queuetimer).start()
queuetimer()

while True:
    data = irc.recv(1204) #gets output from IRC server
    user = data.split(':')[1]
    user = user.split('!')[0] #determines the sender of the messages
    print (data)

    if data.find('PING') != -1:
        irc.send(data.replace('PING', 'PONG')) #responds to PINGS from the server
    if data.find('!test') != -1: #!test command
        message('Hi')

【问题讨论】:

  • 你在哪一行得到错误,你能粘贴整个回溯吗?
  • 是的,我可以。现在添加
  • 看起来 irc.send 可能只接受字节编码的数据,而不是字符串。您必须在将字符串传递给 irc.send 之前对其进行转换。查看此问题的答案:stackoverflow.com/questions/11781639/…

标签: python python-3.x irc twitch


【解决方案1】:

你需要转换成bytes:

irc.send(bytes("PASS {}\r\n".format(password), 'utf-8'))
irc.send(bytes('USER {} 0 * :{}\r\n'.format(nick,bot_owner),"utf-8"))
irc.send(bytes('NICK {}\r\n'.format(nick),"utf-8"))
irc.send(bytes('JOIN {}\r\n'.format(channel),"utf-8"))

然后解码:

recv(1204).decode("utf-8")

【讨论】:

  • 哦是的...我忘了添加解码部分!对不起!
  • 所以目前,我正在运行 Padric 提交的代码,现在它只是一个空白屏幕,它不会崩溃或任何事情。我还删除了拆分代码。如果我需要添加它,我可以。
  • @EduCodes,忘了\r\n,它现在可以正常工作,一旦你有数据,你的拆分也可以工作
  • 现在回到原来的第 16 行,我得到了错误:urary 的错误操作数:str
  • 我自己测试了代码,它运行良好你确定你使用的是发布的代码吗?错误信息指向什么?
猜你喜欢
  • 2011-07-25
  • 2014-08-09
  • 2023-03-10
  • 1970-01-01
  • 1970-01-01
  • 2016-11-21
相关资源
最近更新 更多