Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

discord.py 'NoneType' Object is not subscriptable

I’m getting errors at if datetime.now() > datetime.strptime(str(expire['expiration']), "%Y-%m-%d %H:%M:%S"): for "NoneType" Object is not subscribable, well, I searched on StackOverflow and couldn’t resolve my issue, first I thought it was a row issue, but I’m having data on my MySQL.

import discord, asyncio, os, json, pymysql.cursors
from discord.ext import commands
import databaseconnection
from datetime import datetime, timedelta


# Opens/Read the Config file
with open("configs/config.json") as f:
    data = json.load(f)
    
con = databaseconnection.connection

bot = commands.Bot(command_prefix=data["prefix"])
bot.remove_command('help')


@bot.event
async def on_message(message):
    with con.cursor() as cursor:
        cursor.execute("SELECT * FROM `info` WHERE `discord_id`=%s", (str(message.author.id)))
        expire = cursor.fetchone()
        if datetime.now() >  datetime.strptime(str(expire['expiration']), "%Y-%m-%d %H:%M:%S"):
            embed = discord.Embed(
                color = discord.Color.dark_red(),
                timestamp = datetime.utcnow()
            )
            embed.set_author(name="❗️ ERROR ❗️", icon_url="https://c.tenor.com/2gdcdzl-xaoAAAAM/error-computer.gif")
            embed.add_field(name="__INFO:__", value=f"Your plan has been removed! Due to expiration.")
            embed.set_footer(text="Evil Genios", icon_url="https://cdn.discordapp.com/icons/904638053396672532/643055721178f74ad6a0f1e46843410e.png?size=96")  
            await message.channel.send(embed=embed)
            await asyncio.sleep(5)
            with con.cursor() as icursor:
                icursor.execute("DELETE FROM `info` WHERE `discord_id`=%s", (message.author.id))
                r = icursor.fetchone()
                con.commit()
                embed1 = discord.Embed(
                    colour = discord.Color.dark_green(),
                    timestamp = datetime.utcnow()
                )
                embed1.set_author(name="❗️ SYSTEM ❗️", icon_url="http://www.happy32.in/images/checkmarksuccess.gif")
                embed1.add_field(name="__INFO:__",value=f"`{message.author}`s has been removed!", inline=False)
                embed1.set_footer(text="Evil Genios", icon_url="https://cdn.discordapp.com/icons/904638053396672532/643055721178f74ad6a0f1e46843410e.png?size=96")
                await message.channel.send(embed=embed1)
            


# Loads the Cogs and shit
@bot.event
async def on_ready():
    print('loaded!')
    for filename in os.listdir("cogs"):
        if filename.endswith('.py'):
            bot.load_extension(f"cogs.{filename[:-3]}")
    

# Runs the bot
bot.run("mfa.E-Sc02z7AZ70ZiVV0PPuJC05WxGlSe7QLtG4YuWa6VO__z2OuAiLFLazFjrUOrr11cow2M6DWT_filFY9T9Q", bot=False)
#bot.run("ODgyNjEyODk0ODg0MzE5Mjgy.YS-Y2Q.ta-xQHrdu4rrD-bwKvAeEiGGnoQ", bot=False)import discord, asyncio, os, json, pymysql.cursors
from discord.ext import commands
import databaseconnection
from datetime import datetime, timedelta


# Opens/Read the Config file
with open("configs/config.json") as f:
    data = json.load(f)
    
con = databaseconnection.connection

bot = commands.Bot(command_prefix=data["prefix"], self_bot=True)
bot.remove_command('help')


@bot.event
async def on_message(message):
    with con.cursor() as cursor:
        cursor.execute("SELECT * FROM `info` WHERE `discord_id`=%s", (str(message.author.id)))
        expire = cursor.fetchone()
        if datetime.now() >  datetime.strptime(str(expire['expiration']), "%Y-%m-%d %H:%M:%S"):
            embed = discord.Embed(
                color = discord.Color.dark_red(),
                timestamp = datetime.utcnow()
            )
            embed.set_author(name="❗️ ERROR ❗️", icon_url="https://c.tenor.com/2gdcdzl-xaoAAAAM/error-computer.gif")
            embed.add_field(name="__INFO:__", value=f"Your plan has been removed! Due to expiration.")
            embed.set_footer(text="Evil Genios", icon_url="https://cdn.discordapp.com/icons/904638053396672532/643055721178f74ad6a0f1e46843410e.png?size=96")  
            await message.channel.send(embed=embed)
            await asyncio.sleep(5)
            with con.cursor() as icursor:
                icursor.execute("DELETE FROM `info` WHERE `discord_id`=%s", (message.author.id))
                r = icursor.fetchone()
                con.commit()
                embed1 = discord.Embed(
                    colour = discord.Color.dark_green(),
                    timestamp = datetime.utcnow()
                )
                embed1.set_author(name="❗️ SYSTEM ❗️", icon_url="http://www.happy32.in/images/checkmarksuccess.gif")
                embed1.add_field(name="__INFO:__",value=f"`{message.author}`s has been removed!", inline=False)
                embed1.set_footer(text="Evil Genios", icon_url="https://cdn.discordapp.com/icons/904638053396672532/643055721178f74ad6a0f1e46843410e.png?size=96")
                await message.channel.send(embed=embed1)
            


# Loads the Cogs and shit
@bot.event
async def on_ready():
    print('loaded!')
    for filename in os.listdir("cogs"):
        if filename.endswith('.py'):
            bot.load_extension(f"cogs.{filename[:-3]}")
    

# Runs the bot
bot.run("")

>Solution :

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

If expire is None, then expire['expiration'] will throw an exception.

Check the value of expire before doing it.

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading