&quot'typeError:' datetime.datetime'对象不可call与Discord.py制作Discord bot

发布于 2025-02-09 20:19:38 字数 4906 浏览 3 评论 0原文

我正在试图使机器人踢一个扮演一个星期的玩家,但我遇到了一些错误。

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

我在Stackoverflow中尝试了一些相关答案,例如 typeerror”> typeerror”> typeerror:'module'无法可叫 typeerror:' ,但是什么都没有。也许是因为这是我第一次与Python合作?

对不起,我的英语不好,但我需要一些帮助。

-edit-

我的第一个代码实际上是,

@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

但是我遇到了

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

错误,所以我将两个时区都定为UTC。

首先,当我运行计算机时,它运行正常,并且只有将其上传到Heroku时才出现错误。

但是在编辑之后,我也无法通过计算机运行它。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纸伞微斜 2025-02-16 20:19:38

这是问题:

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

您创建一个datetime对象,称为Timenow,然后尝试在下一行上调用它。

删除括号:

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

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)
舂唻埖巳落 2025-02-16 20:19:38

在我的情况下,我的活动python文件的同一目录中存在一个dateTime.py。
重命名或删除它可以解决我的问题

In my situation, there is a datetime.py exist in the same directory of my active python file.
Renaming or removing it resolve my issue

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文