python - 类型错误 : does not support the buffer interface Twitch IRC Chat Bot

标签 python python-3.x irc twitch

首先,我无法在任何地方找到答案。我不确定它是否是我需要导入的东西,或者只是代码做得不好。简而言之,通过这个项目,我将通过短信和许多其他功能制作一个更好的 twitch follower 通知程序。

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

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')

最佳答案

您需要转换为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")

关于python - 类型错误 : does not support the buffer interface Twitch IRC Chat Bot,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/28182582/

相关文章:

python - 使用 Python 加载 DLL 时出现错误 193

python-3.x - 根据另一个数据帧中的值查找 Pandas 数据帧中的区间

haskell - 在 IRC 机器人 (Haskell) 中实现 CTCP 命令

python - 在不重新启动主脚本的情况下加载/重新加载 Python 中的部分代码

Char[] 丢失数据

Python从列表中打印 float

python - os.path.isdir() 无法识别隐藏目录

python - 尝试从不同的文件调用对象,但输出仅显示空白

python - 找到不固定长度的数字的所有可能排列以达到给定的总和或乘积

用于访问类变量的 Python 关键字