Home > Blockchain >  "TypeError: 'datetime.datetime' object is not callable" on making discord bot wi
"TypeError: 'datetime.datetime' object is not callable" on making discord bot wi

Time:06-24

I'm trying to make the bot to kick the player who has the role for a week, but I'm getting some errors.

2022-06-23T04:48:44.209432 00:00 app[worker.1]: Ignoring exception in on_member_update
2022-06-23T04:48:44.209541 00:00 app[worker.1]: Traceback (most recent call last):
2022-06-23T04:48:44.209560 00:00 app[worker.1]:   File "/app/.heroku/python/lib/python3.10/site-packages/discord/client.py", line 343, in _run_event
2022-06-23T04:48:44.209561 00:00 app[worker.1]:     await coro(*args, **kwargs)
2022-06-23T04:48:44.209570 00:00 app[worker.1]:   File "/app/ttsbot.py", line 112, in on_member_update
2022-06-23T04:48:44.209571 00:00 app[worker.1]:     max_join_time_1 = timenow() - timedelta(weeks=1)
2022-06-23T04:48:44.209592 00:00 app[worker.1]: TypeError: 'datetime.datetime' object is not callable
import discord
import asyncio
import datetime
import math
import pytz
from discord.ext import commands
from discord.ext.commands import has_permissions, MissingPermissions
from discord.utils import get
from datetime import datetime, timedelta, timezone

TOKEN = "*something*"
intents = discord.Intents.all()
intents.members = True
bot = commands.Bot(command_prefix='.', intents = intents)
bot.remove_command('help')
utc=pytz.UTC

@bot.event
async def on_member_update(before, after):
    if len(before.roles) < len(after.roles):
        new_role = next(role for role in after.roles if role not in before.roles)
        guild = bot.get_guild(*something*)
        if '.• 미인증 유저' in new_role.name:
            role_name1 = ".• 미인증 유저"
            role1 = discord.utils.get(guild.roles, name=role_name1)
            await asyncio.sleep(604800)
            timenow = datetime.now(timezone.utc)
            max_join_time_1 = timenow() - timedelta(weeks=1)
            current_timezone_time = after.joined_at
            new_timezone_time = current_timezone_time.astimezone(pytz.UTC)
            log_channel = bot.get_channel(*something*)
            if after in role1.members and new_timezone_time < max_join_time_1:
                await after.kick(reason="일주일동안 인증하지 않으셨습니다.")
                
                embed = discord.Embed(title="강퇴",
                                      colour=0xB11B1B,
                                      timestamp=datetime.now())
                              
                embed.set_thumbnail(url=after.avatar_url)
                
                fields = [("이름", f"{after.name}", False),
                          ("사유", f"7일간 미인증으로 머물러 강퇴당함.", False)]
                          
                for name, value, inline in fields:
                    embed.add_field(name=name, value=value, inline=inline)
                    
                await log_channel.send(embed=embed)
            else:
                pass
        else:
            pass

I've tried some of the relevant answers in stackoverflow, like TypeError: 'module' object is not callable and TypeError: 'datetime.datetime' object is not callable, but nothing worked. Maybe because it's my first time working with python?

Sorry for my bad english, but I need some help.

-edit-

My first code was actually

@bot.event
async def on_member_update(before, after):
    if len(before.roles) < len(after.roles):
        new_role = next(role for role in after.roles if role not in before.roles)
        guild = bot.get_guild(*something*)
        if '.• 미인증 유저' in new_role.name:
            role_name1 = ".• 미인증 유저"
            role1 = discord.utils.get(guild.roles, name=role_name1)
            await asyncio.sleep(604800)
            timenow = datetime.now
            max_join_time_1 = timenow() - timedelta(weeks=1)
            log_channel = bot.get_channel(*something*)
            if after in role1.members and after.joined_at < max_join_time_1:
                await after.kick(reason="일주일동안 인증하지 않으셨습니다.")
                
                embed = discord.Embed(title="강퇴",
                                      colour=0xB11B1B,
                                      timestamp=datetime.now())
                              
                embed.set_thumbnail(url=after.avatar_url)
                
                fields = [("이름", f"{after.name}", False),
                          ("사유", f"7일간 미인증으로 머물러 강퇴당함.", False)]
                          
                for name, value, inline in fields:
                    embed.add_field(name=name, value=value, inline=inline)
                    
                await log_channel.send(embed=embed)
            else:
                pass
        else:
            pass

but then I had the

TypeError: can’t compare offset-naive and offset-aware datetimes

error, so I fixed both of the timezone to utc.

First it worked fine when I ran it my computer, and it had errors only when I uploaded it to heroku.

But after the edit I can't run it by my computer, too.

CodePudding user response:

Here is the problem:

timenow = datetime.now(timezone.utc)
max_join_time_1 = timenow() - timedelta(weeks=1)
#                        ^^

You create a datetime object called timenow and then you try to call it on the next line.

Remove the parentheses:

timenow = datetime.now(timezone.utc)
max_join_time_1 = timenow - timedelta(weeks=1)

CodePudding user response:

I am still learning, but I believe on line 24 (column 42) you have (something). This occurs again on line 33 (column 54). In python * is used as an unpack operator when not used in a mathematical sense, which is causing at least one issue.

I see that you assigned TOKEN the string something above, did you mean to do that below? If so you are missing the " " around the strings within the parenthesis on both of the above-mentioned lines.

I hope I was able to help with what meager knowledge I have so far.

  • Related