识别发送用户Python-IRC
因此,我为自己的频道制作了一个Twitch.tv bot,在玩了一点之后,我想将一些命令限制在一些用户,以及一些可以显示用户名的命令,例如:识别发送用户Python-IRC,python,bots,irc,twitch,Python,Bots,Irc,Twitch,因此,我为自己的频道制作了一个Twitch.tv bot,在玩了一点之后,我想将一些命令限制在一些用户,以及一些可以显示用户名的命令,例如: Username reply example: Person1: !tea PythonBot: Would you like some tea, Person1? Admin restriction example: Person1: !ban Person2 PythonBot: I'm sorry, Person1, This command
Username reply example:
Person1: !tea
PythonBot: Would you like some tea, Person1?
Admin restriction example:
Person1: !ban Person2
PythonBot: I'm sorry, Person1, This command is restricted to admins only.
好的,下面是我正在使用的代码(我将很快修改它,使之成为我自己的代码)
导入套接字
导入线程
bot_owner=“~今天不行~”
尼克=“~今天不行~”
频道=“~今天不行~”
服务器='irc.twitch.tv'
密码=“~今天不行~”
队列=13
irc=socket.socket()
irc.connect((服务器,6667))
irc.send('PASS'+password+'\r\n')
irc.send('USER'+nick+'0*:'+bot\u owner+'\r\n')
irc.send('NICK'+NICK+'\r\n')
irc.send('JOIN'+channel+'\r\n')
def消息(msg):
全局队列
队列=5
如果队列<20:
发送('PRIVMSG'+channel+':'+msg+'\r\n')
其他:
打印“已删除邮件”
def queuetimer():
全局队列
队列=0
threading.Timer(30,queuetimer.start())
队列计时器()
尽管如此:
botdata=irc.recv(1204)
botuser=botdata.split(“:”)[1]
botuser=botuser.split(“!”)[0]
打印数据
如果botdata.find('PING')!=-1:
发送(botdata.replace('PING','PONG'))
如果botdata.find(“!res”)!=-1:
irc.send(botdata.replace('!res','1600x900'))
twitch IRC原始消息如下
:jkm!jkm@jkm.tmi.twitch.tvPRIVMSG#trumpsc:需要卡帕
对于上面的消息,它实际上意味着用户jkm
在频道trumpsc
说需要Kappa
对于您的代码,获取botuser
的方法是正确的,但是您没有收到用户发送的消息,添加以下代码应该获得消息
botmsg = botdata.split(':')[2]
因此,当您获得消息和用户名时,下一步就是处理它们。
下面是一些你需要的例子。对于我来说,我将为其他命令准备一个adminuserList
和commmandList
,但我将在这里对其进行简化
def commmanHandler(botuser, botmsg): # botmsg = '!ban user1'
command = botmsg.split(' ')[0] # command = '!ban'
command = command[1:] # command = 'ban'
argu = message.split(' ')[1] # argu = 'user1'
if command not in commmandList:
raise Exception("command not support")
if command == 'ban': # ban command, or using switch
# check if admin
if botuser not in adminList:
raise Exception("I'm sorry, " + botuser + ", This command is restricted to admins only.")
# admin, able to ban
irc.send('PRIVMSG' + channel + ' :' + '.ban ' + argu)
然后在while循环中调用此函数来处理您收到的所有消息
try:
commmanHandler(botuser, botmsg)
except Exception, e:
print e
irc.send('PRIVMSG' + channel + ' :' + e)
这是我的解决方案,还有,别忘了给机器人版主特权。你看过Python IRC机器人SuPyBot/Gribble/Limnoria吗?你的代码正常吗?
try:
commmanHandler(botuser, botmsg)
except Exception, e:
print e
irc.send('PRIVMSG' + channel + ' :' + e)