我目前正在创建一些discord bot,我有一些数据存储在mysql中,两个表在同一个数据库下。
使用mysql connector python文档中的相同示例,cnx.is\u connected返回false,并且我的“for循环”没有运行,或者错误地说我没有按正确的顺序使用它。
我与discord bot和async的代码(供idk以后参考,我的问题出在哪里):
import mysql.connector
import asyncio
import discord
client = discord.Client()
cnx = mysql.connector.connect(user='user', password=’password’',
host='IP',
database='creative role-play')
cursor = cnx.cursor()
@client.event
async def on_message(message):
if message.author == client.user:
return
SteamID = 0
messagelower = message.content.lower()
if messagelower.startswith("!verify"):
steamidnum = int(message.content[message.content.index(" ") + 1:])
query = ("SELECT steamid64, SteamID, CharName, TotalPlayTime FROM playerinfo, member_list "
"WHERE steamid64 AND SteamID = %d") % (steamidnum)
cursor.execute(query)
print (cursor)
if cnx.is_connected():
print('sucessfull...Connected to MySQL database')
for (steamid64, SteamID, CharName, TotalPlayTime) in cursor:
print("{}, {}, {}".format(SteamID, CharName, TotalPlayTime))
CharName = str(CharName)
TotalPlayTime = int(TotalPlayTime)
print (SteamID, "", steamid64)
print ("confirm 1")
if steamid64 == SteamID:
ifrole = "has already been entered, please contact <@232233468094840832> if this is you"
print ("confirm2")
else:
TotalPlayTime = int(TotalPlayTime)
print (TotalPlayTime)
if TotalPlayTime >= 86400:
ifrole = "has become a Member of Modern Role-Play"
role = discord.utils.get(message.server.roles, name="Member")
await client.add_roles(message.author, role)
add = ("INSERT INTO member_list "
"(steamid64, discordname, charactername)"
"VALUES (%(steamid64)s, %(discordname)s, %(charactername)s)")
values = {"steamid64": SteamID,
"discordname": message.author.name,
"charactername": CharName,}
cursor.execute(add, values)
cnx.commit()
else:
ifrole = "does not meet the time requirement of 24 hours to become a Member"
msg = "Player %s has a Playtime of %d hour's and %s! (%s)".format(message) % (CharName, TotalPlayTime / 3600, ifrole, steamidnum)
await client.send_message(message.channel, msg)
@client.event
async def on_ready():
print('Logged in as')
print(client.user.name)
print(client.user.id)
print('--Now Online--')
client.run('Token')
抱歉,如果你不想把不和谐的东西放在里面。
谢谢大家的帮助!
1条答案
按热度按时间gstyhher1#
你有一个额外的引号(soz如果我迟到)