我目前正在编写一个简单的IRC版主/工具机器人。我计划有一个Web界面,可以轻松更新命令数据,以及是否使用MySQL数据库启用该命令来存储大部分信息。它能够连接到数据库并正确检索数据。但是,在我更改数据库中的值后,它不会更新。我曾尝试使用.close()
和.commit()
或启用自动提交(即使我没有更改数据库中的任何内容)作为之前遇到此问题的其他人的解决方案。
import socket
import StringIO
import MySQLdb
ircsock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
server = "irc.twitch.tv"
channel = "#jusjoe99"
botName = "N0tABot"
oauth = ""
twitter = {"msg": "Check me out on me out on my Twitter to stay up to date and see when ill be live next! twitter.com/jusjoe99", "enabled": True}
youtube = {"msg": "Check me out on me out on the YouTubes, I dint post much but I plan to eventually doing some vids! youtube.com/jusjoe99", "enabled": True}
game = {"msg": "", "enabled":False}
resourcePack = {"msg":"", "enabled":False}
data = []
db = MySQLdb.connect(host = "localhost",user="myBot",passwd = "wHZ4VCWe2jaYxH9y",db = "mybot")
db.autocommit(True)
cursor = db.cursor()
cursor.execute("SELECT * FROM commands")
data = cursor.fetchall()
db.commit()
cursor.close()
db.close()
def MySQL():
db = MySQLdb.connect(host = "localhost",user="myBot",passwd = "wHZ4VCWe2jaYxH9y",db = "mybot")
cursor = db.cursor()
cursor.execute("SELECT * FROM commands")
data = cursor.fetchall()
db.commit()
cursor.close()
db.close()
def ping():
ircsock.send("PONG : Pong\n")
def joinChannel(chan):
ircsock.send("JOIN " + chan +"\n")
def sendMessage(chan, msg):
ircsock.send("PRIVMSG " + chan + " :" + msg + "\n")
def hello():
ircsock.send("PRIVMSG " + channel + " :Hello\n")
def start():
ircsock.connect((server, 6667))
ircsock.send("PASS " + oauth + "\r\n")
ircsock.send("USER "+ botName + botName + botName + "\r\n")
ircsock.send("NICK " + botName + "\r\n")
joinChannel(channel)
updateValues()
def getSong():
song = open("cur_song.txt","r")
cursong = song.read()
ircsock.send("PRIVMSG " + channel + " :" + "The Current song is: " + cursong + "\n")
song.close()
def updateValues():
MySQL()
checkTwitter()
checkYouTube()
checkGame()
checkResourcePack()
def checkTwitter():
twit = data[2]
print twit[1]
twitter["enabled"] = twit[1]
def checkGame():
gme = data[0]
game["msg"] = gme[2]
if gme[1] == 0:
game["enabled"] = False
else:
game["enabled"] = True
def checkResourcePack():
rp = data[1]
resourcePack["msg"] = rp[2]
if rp[1] == 0:
resourcePack["enabled"] = False
else:
resourcePack["enabled"] = True
def checkYouTube():
ytube = data[3]
if ytube[1] == 0:
youtube["enabled"] = True
else:
youtube["enabled"] = False
start()
while 1:
ircmsg = ircsock.recv(2048)
ircmsg = ircmsg.strip('\n\r')
print ircmsg
updateValues()
if ircmsg.find(":!song") !=-1:
getSong()
if ircmsg.find(":!twitter") !=-1 and twitter["enabled"]:
sendMessage(channel, twitter["msg"])
if ircmsg.find("PING :") != -1:
ping()
if ircmsg.find(":!youtube") !=-1 and youtube["enabled"]:
sendMessage(channel, youtube["msg"])
if ircmsg.find("!tp") !=-1 and resourcePack["enabled"]:
sendMessage(channel, resourcePack["msg"])
感谢您的帮助!(我很抱歉我的代码混乱)