在 twitch irc 机器人上创建 !addcom 和 !delcom 命令。跳过代码行
Posted
技术标签:
【中文标题】在 twitch irc 机器人上创建 !addcom 和 !delcom 命令。跳过代码行【英文标题】:Creating an !addcom and !delcom commands on twitch irc bot. Jump over line of code 【发布时间】:2016-01-29 22:55:10 【问题描述】:我正在创建一个 twitch 聊天机器人,我正在尝试创建和 !addcom 和 !delcom 命令来通过聊天创建和删除命令。事情是几乎同一行代码对一个命令起作用,而对另一个不起作用。我用打印和中断进行了测试,看起来它只是跳过了它。
他刚刚跳过的那一行是:
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
这是完整的代码:
import string
import json
from Socket import openSocket, sendMessage
from Initialize import joinRoom
from Read import getUser, getMessage, getChannel, string_1, string_2_plus
irc = openSocket()
joinRoom(irc)
readbuffer = ""
irc.send("CAP REQ :twitch.tv/membership\r\n")
irc.send("CAP REQ :twitch.tv/commands\r\n")
try:
with file("commands.json","r") as commandsDatabase:
commands = json.load(commandsDatabase)
except IOError:
commands =
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
globalcommands = "addcom": True, "delcom": True, "spank": True
while True:
readbuffer = readbuffer + irc.recv(1024)
temp = string.split(readbuffer, "\n")
readbuffer = temp.pop()
for line in temp:
###Essenciais###--------------------------------------------------------------------------------------------------------------------------------------------
#Mostra a linha que e dada pelo servidor de IRC (So pelo sim pelo nao).-----------------------------------------------------------------------
print (line)
#---------------------------------------------------------------------------------------------------------------------------------------------
#Impede que seja desconectado pelo servidor de IRC.-------------------------------------------------------------------------------------------
if line.startswith('PING'):
irc.send('PONG ' + line.split( ) [ 1 ] + '\r\n')
print "PONGED BACK"
break
#---------------------------------------------------------------------------------------------------------------------------------------------
#Le a linha que e dada pelo servidor de IRC e devevole o utilizador, a menssagem e o canal. Volta se algum for nulo.--------------------------
user = getUser(line)
message = getMessage(line)
channel = getChannel(line)
if channel == None or user == None or message == None:
break
#---------------------------------------------------------------------------------------------------------------------------------------------
#Random Strings.------------------------------------------------------------------------------------------------------------------------------
stringspace = " "
nothing = ""
#---------------------------------------------------------------------------------------------------------------------------------------------
#Formata o texto e mostra mostra na consola.--------------------------------------------------------------------------------------------------
print channel + ": " + user + " > " + message
#---------------------------------------------------------------------------------------------------------------------------------------------
###Essenciais END###----------------------------------------------------------------------------------------------------------------------------------------
if message.startswith("!addcom "):
if message.count(stringspace) >= 2:
try:
commandadd = string_1(message)
answer = string_2_plus(message)
except IndexError:
sendMessage(irc, channel, user + " the command is used this way !addcom !<command_name> <command_answer>")
break
if globalcommands.has_key(commandadd):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commandadd + '" !!!')
break
try:
commands[commandadd]
except KeyError:
commands[commandadd] = answer
sendMessage(irc, channel, user + " the command !" + commandadd + " has been added!!!")
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
break
sendMessage(irc, channel, user + " the command you tried to add alredy exists!!!")
break
sendMessage(irc, channel, user + " the command is used this way !addcom !<command_name> <command_answer>")
break
if message.startswith("!delcom "):
if message.count(stringspace) == 1:
try:
commanddel = string_1(message)
except IndexError:
sendMessage(irc, channel, user + "the command is used this way !delcom !<command_name>")
break
if globalcommands.has_key(commanddel):
sendMessage(irc, channel, user + " you can't add a command called " + '"!' + commanddel + '" !!!')
break
try:
commands[commanddel]
except KeyError:
sendMessage(irc, channel, user + " the command you tried to delete doens't exist!!!")
break
del commands[commanddel]
sendMessage(irc, channel, user + " the command !" + commanddel + " has been deleted!!!")
with file("commands.json","w") as commandsDatabase:
json.dump(commands, commandsDatabase)
break
sendMessage(irc, channel, user + " the command is used this way !delcom !<command_name>")
break
如果您需要查看这里的任何其他文件,请访问我的 github 存储库:https://github.com/BlasterJoni/ClientSideTwitchChatBotPython/
【问题讨论】:
使用break
将停止while
循环
这是有意的,但还是感谢您的评论。我已经发现它没有正确定义事物。忘了在行尾有一个 \r xD
【参考方案1】:
我已经发现我没有正确定义事物。我忘了行尾有一个\r。
我定义了两种从消息中获取字符串 1 的不同方法,一种用于 !addcom,另一种用于 !delcom。
我只需要这样定义 !delcom 一个:
def string_1(message):
string1 = message.split("!", 2)[2].split("\r", 1)[0]
return string1
它开始工作了。
【讨论】:
以上是关于在 twitch irc 机器人上创建 !addcom 和 !delcom 命令。跳过代码行的主要内容,如果未能解决你的问题,请参考以下文章
在 Python 中使用 Twitch IRC 时“由对等方重置连接”