Using "with conn.cursor as cursor"

Inserting/deleting information about guild and prefix as the bot joins/leaves the server
pull/8/head
sgoudham 4 years ago
parent 09fe36542c
commit 1f1bc8c0b3

@ -37,6 +37,14 @@ if __name__ == '__main__':
client.load_extension(ext) client.load_extension(ext)
# Bot ~Ping command in milliseconds
@client.command(name="ping", aliases=["Ping"])
async def _ping(ctx):
"""Sends the latency of the bot (ms)"""
await ctx.send(f'Pong! `{round(client.latency * 1000)}ms`')
# Bot event making sure that messages sent by the bot do nothing
@client.event @client.event
async def on_message(message): async def on_message(message):
# Making sure that the bot does not take in its own messages # Making sure that the bot does not take in its own messages
@ -59,53 +67,58 @@ async def on_ready():
# await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Spider Man 3")) # await client.change_presence(activity=discord.Activity(type=discord.ActivityType.watching, name="Spider Man 3"))
# Bot ~Ping command in milliseconds # Bot event for the bot joining a new guild, storing all users in the database
@client.command(name="ping", aliases=["Ping"])
async def _ping(ctx):
"""Sends the latency of the bot (ms)"""
await ctx.send(f'Pong! `{round(client.latency * 1000)}ms`')
# Bot event for the bot leaving a guild, deleted all users stored in the database
@client.event @client.event
async def on_guild_remove(guild): async def on_guild_join(guild):
try: try:
# Set up connection to database # Set up connection to database
with db.connection() as conn: with db.connection() as conn:
# Iterate through every member within the guild
for member in guild.members: for member in guild.members:
# Delete the record of the member as they leave the server name = f"{member.name}#{member.discriminator}"
delete_query = """DELETE FROM members WHERE discordID = (?) AND guildID = (?)"""
vals = member.id, guild.id,
cursor = conn.cursor()
# Execute the SQL Query # Define the insert statement that will insert the user's information
cursor.execute(delete_query, vals) insert_query = """INSERT INTO members (guildID, discordUser, discordID) VALUES (?, ?, ?)"""
conn.commit() vals = guild.id, name, member.id,
print(cursor.rowcount, f"Record deleted successfully from Members from {guild.name}") with conn.cursor as cursor:
# Execute the query
cursor.execute(insert_query, vals)
print(cursor.rowcount, f"Record inserted successfully into Members from {guild.name}")
# Define the insert statement for inserting the guild into the guilds table
insert_query = """INSERT INTO guilds (guildID) VALUES (?)"""
val = guild.id,
with conn.cursor as cursor:
# Execute the query
cursor.execute(insert_query, val)
print(cursor.rowcount, f"Record inserted successfully into Guilds from {guild.name}")
except mariadb.Error as ex: except mariadb.Error as ex:
print("Parameterized Query Failed: {}".format(ex)) print("Parameterized Query Failed: {}".format(ex))
# Bot event for the bot joining a new guild, storing all users in the database # Bot event for the bot leaving a guild, deleted all users stored in the database
@client.event @client.event
async def on_guild_join(guild): async def on_guild_remove(guild):
try: try:
# Set up connection to database # Set up connection to database
with db.connection() as conn: with db.connection() as conn:
# Iterate through every member within the guild
for member in guild.members: for member in guild.members:
name = f"{member.name}#{member.discriminator}" # Delete the record of the member as the bot leaves the server
delete_query = """DELETE FROM members WHERE discordID = (?) AND guildID = (?)"""
# Define the insert statement that will insert the user's information vals = member.id, guild.id,
insert_query = """INSERT INTO members (guildID, discordUser, discordID) VALUES (?, ?, ?)""" with conn.cursor as cursor:
vals = guild.id, name, member.id, # Execute the SQL Query
cursor = conn.cursor() cursor.execute(delete_query, vals)
print(cursor.rowcount, f"Record deleted successfully from Members from {guild.name}")
# Delete the guild and prefix information as the bot leaves the server
delete_query = """DELETE FROM guilds WHERE guildID = (?)"""
val = guild.id,
with conn.cursor as cursor:
# Execute the query # Execute the query
cursor.execute(insert_query, vals) cursor.execute(delete_query, val)
conn.commit() print(cursor.rowcount, f"Record deleted successfully from Guilds from {guild.name}")
print(cursor.rowcount, f"Record inserted successfully into Members from {guild.name}")
except mariadb.Error as ex: except mariadb.Error as ex:
print("Parameterized Query Failed: {}".format(ex)) print("Parameterized Query Failed: {}".format(ex))

Loading…
Cancel
Save