The setup works when there’s only one option, but it stops working as soon as I add the second village into the mix. There’s supposed to be 12 in total.
import mysql.connector
import nextcord
from nextcord import Intents
from nextcord.ext import commands
from mysql.connector import Error
import random
intents = Intents.default()
intents.message_content = True
bot = commands.Bot(command_prefix='!', intents=intents)
@bot.command(name="village")
async def village(ctx):
villages = [1, 2]
user = ctx.message.author
guild = ctx.guild.id
connection = mysql.connector.connect(
host="localhost",
port="3306",
user="root",
password="root",
database="naruto_game"
)
if villages == [1]:
village = "Konohagakure"
try:
mySql_Create_Table_Query = """CREATE TABLE VILLAGES_""" + str(guild) + """ (
Id int(11) NOT NULL AUTO_INCREMENT,
User varchar(250) NOT NULL,
Village varchar(5000) NOT NULL,
PRIMARY KEY (Id)) """
cursor = connection.cursor()
result = cursor.execute(mySql_Create_Table_Query)
print(f"Village Database for Guild (" + str(guild) + ") Table created successfully")
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
finally:
if connection.is_connected():
table = "VILLAGES_" + str(guild)
mySql_Insert_Row_Query = "INSERT INTO " + table + " (User, Village) VALUES (%s, %s)"
mySql_Insert_Row_Values = (str(user), village)
sql_select_query = "SELECT * from " + table + " where user like '"+ str(ctx.message.author) +"'"
cursor.execute(sql_select_query)
records = cursor.fetchall()
if len(records) == 0:
cursor.execute(mySql_Insert_Row_Query, mySql_Insert_Row_Values)
connection.commit()
await ctx.send(f"{user} has been born into {village}.")
else:
await ctx.send("You are already in a village. Use !profile to see what village you're in.")
elif villages == [2]:
village = "Iwagakure"
try:
mySql_Create_Table_Query = """CREATE TABLE VILLAGES_""" + str(guild) + """ (
Id int(11) NOT NULL AUTO_INCREMENT,
User varchar(250) NOT NULL,
Village varchar(5000) NOT NULL,
PRIMARY KEY (Id)) """
cursor = connection.cursor()
result = cursor.execute(mySql_Create_Table_Query)
print(f"Village Database for Guild (" + str(guild) + ") Table created successfully")
except mysql.connector.Error as error:
print("Failed to create table in MySQL: {}".format(error))
finally:
if connection.is_connected():
table = "VILLAGES_" + str(guild)
mySql_Insert_Row_Query = "INSERT INTO " + table + " (User, Village) VALUES (%s, %s)"
mySql_Insert_Row_Values = (str(user), village)
sql_select_query = "SELECT * from " + table + " where user like '"+ str(ctx.message.author) +"'"
cursor.execute(sql_select_query)
records = cursor.fetchall()
if len(records) == 0:
cursor.execute(mySql_Insert_Row_Query, mySql_Insert_Row_Values)
connection.commit()
await ctx.send(f"{user} has been born into {village}.")
else:
await ctx.send("You are already in a village. Use !profile to see what village you're in.")
I’m trying to make it to where it will receive the command, pick a random village out of the 12, then store it in the database, then send a message stating what village they’ve gotten.
New contributor
TurkeySouls is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.