![](/img/trans.png)
[英]Getting an AttributeError: 'Update' object has no attribute 'send_message' when I try to send a message with Telegram bot
[英]AttributeError: 'NoneType' object has no attribute 'send' ,when I try to send a message with a bot to a specific discord channel
我尝试通过这两种方式使用 discord 机器人将消息发送到特定的文本频道,但这两种方式都不适合我。
在这里,我将尝试使用每种模式返回的相应错误的 2 种方法
1#
import time, random, discord
from datetime import datetime
from discord.ext import commands
async def time_messege(args):
bot = commands.Bot(command_prefix='.')
alarmtime = "19:23"
#greeting_channel = bot.get_channel("814238156396298310")
#channel = bot.get_channel = xxx
client = discord.Client()
while True:
lcltime = datetime.now().strftime('%H:%M')
if lcltime == alarmtime:
#aca pondria el code de detectar canal y enviar
print("is time!")
random_num = random.randint(1, 4)
if random_num == 1:
#await greeting_channel.send("Holi!, como estan chic@s?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como estan chic@s?")
elif random_num == 2:
#await greeting_channel.send("Holi!, oCmo va su dia? que me cuentan?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, oCmo va su dia? que me cuentan?")
elif random_num == 3:
#await greeting_channel.send("Holi!, Como va su dia? que andan haciendo?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, Como va su dia? que andan haciendo?")
elif random_num == 4:
#await greeting_channel.send("Holi!, como se encuentran?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como se encuentran?")
time.sleep(90)
else:
print("not yet")
time.sleep(10)
这是错误:
not yet
not yet
not yet
is time!
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "main_greetings.py", line 101, in between_callback
loop.run_until_complete(time_messege(args))
File "C:\Users\Maty0\Anaconda3\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\Maty0\Desktop\h ver 18-03\SmartBot-main\core\modules\greetings.py", line 30, in time_messege
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, oCmo va su dia? que me cuentan?")
AttributeError: 'Client' object has no attribute 'send_message'
2#
如果我使用其他代码(注释代码,而不是客户端),它会给出类似的错误
我需要定义“客户”吗?
import time, random, discord
from datetime import datetime
from discord.ext import commands
async def time_messege(args):
bot = commands.Bot(command_prefix='.')
alarmtime = "19:36"
greeting_channel = bot.get_channel("814238156396298310")
#channel = bot.get_channel = xxx
#client = discord.Client()
while True:
lcltime = datetime.now().strftime('%H:%M')
if lcltime == alarmtime:
#aca pondria el code de detectar canal y enviar
print("is time!")
random_num = random.randint(1, 4)
if random_num == 1:
await greeting_channel.send("Holi!, como estan chic@s?")
#await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como estan chic@s?")
elif random_num == 2:
await greeting_channel.send("Holi!, oCmo va su dia? que me cuentan?")
#await client.send_message(discord.Object(id='814238156396298310'), "Holi!, oCmo va su dia? que me cuentan?")
elif random_num == 3:
await greeting_channel.send("Holi!, Como va su dia? que andan haciendo?")
#await client.send_message(discord.Object(id='814238156396298310'), "Holi!, Como va su dia? que andan haciendo?")
elif random_num == 4:
await greeting_channel.send("Holi!, como se encuentran?")
#await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como se encuentran?")
time.sleep(90)
else:
print("not yet")
time.sleep(10)
并使用此代码,给出该错误
not yet
not yet
not yet
is time!
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "main_greetings.py", line 101, in between_callback
loop.run_until_complete(time_messege(args))
File "C:\Users\Maty0\Anaconda3\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\Maty0\Desktop\h ver 18-03\SmartBot-main\core\modules\greetings.py", line 33, in time_messege
await greeting_channel.send("Holi!, oCmo va su dia? que me cuentan?")
AttributeError: 'NoneType' object has no attribute 'send'
你推荐什么算法,我应该怎么做才能避免这些错误? 我应该放什么属性?
我使用那个版本的 discord 库
Python 3.8.5 (default, Sep 3 2020, 21:29:08) [MSC v.1916 64 bit (AMD64)] :: Anaconda, Inc. on win32
Type "help", "copyright", "credits" or "license" for more information.
>>> import discord
>>> discord.__version__
'1.6.0'
id是正确的
我也尝试了他们在答案中建议的代码,但它不断给出错误......
import time, random, discord
from datetime import datetime
from discord.ext import commands
bot = commands.Bot(command_prefix='.')
@bot.command()
async def time_messege(args):
alarmtime = "20:54"
#greeting_channel = bot.get_channel("814238156396298310")
client = discord.Client()
while True:
lcltime = datetime.now().strftime('%H:%M')
if lcltime == alarmtime:
#aca pondria el code de detectar canal y enviar
print("is time!")
random_num = random.randint(1, 4)
if random_num == 1:
#await greeting_channel.send("Holi!, como estan chic@s?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como estan chic@s?")
elif random_num == 2:
#await greeting_channel.send("Holi!, oCmo va su dia? que me cuentan?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, oCmo va su dia? que me cuentan?")
elif random_num == 3:
#await greeting_channel.send("Holi!, Como va su dia? que andan haciendo?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, Como va su dia? que andan haciendo?")
elif random_num == 4:
#await greeting_channel.send("Holi!, como se encuentran?")
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como se encuentran?")
time.sleep(90)
else:
print("not yet")
time.sleep(10)
错误:
not yet
not yet
not yet
is time!
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "main_greetings.py", line 101, in between_callback
loop.run_until_complete(time_messege(args))
File "C:\Users\Maty0\Anaconda3\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\Maty0\Anaconda3\lib\site-packages\discord\ext\commands\core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
File "C:\Users\Maty0\Desktop\h ver 18-03\SmartBot-main\core\modules\greetings.py", line 41, in time_messege
await client.send_message(discord.Object(id='814238156396298310'), "Holi!, como se encuentran?")
AttributeError: 'Client' object has no attribute 'send_message'
如果我在 function 中添加这个额外的参数,则会出现错误:
Exception in thread Thread-2:
Traceback (most recent call last):
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 932, in _bootstrap_inner
self.run()
File "C:\Users\Maty0\Anaconda3\lib\threading.py", line 870, in run
self._target(*self._args, **self._kwargs)
File "main_greetings.py", line 101, in between_callback
loop.run_until_complete(time_messege(args))
File "C:\Users\Maty0\Anaconda3\lib\asyncio\base_events.py", line 616, in run_until_complete
return future.result()
File "C:\Users\Maty0\Anaconda3\lib\site-packages\discord\ext\commands\core.py", line 374, in __call__
return await self.callback(*args, **kwargs)
TypeError: time_messege() missing 1 required positional argument: 'args'
我遇到了同样的问题,然后我发现 ID 必须是 integer 而不是字符串,尝试bot.get_channel(814238156396298310)
在第一种情况下,您使用的send_message
方法似乎不再存在于您安装的库版本中(检查Migrating to 1.0 )。
在第二种情况下,可能找不到您指定的频道,因此bot.get_channel
方法按预期返回None
。
#1 Assuming you're using discordpy, the error is correct: neither discord.Client
nor the extension discord.ext.commands.Bot
have the method .send
according to their published API, though it is found in discord.ext.commands.Context
https://discordpy.readthedocs.io/en/stable/api.html#discord.Client https://discordpy.readthedocs.io/en/stable/ext/commands/api.html https://discordpy.readthedocs. io/en/stable/ext/commands/api.html#discord.ext.commands.Context
老实说,我开始写这个答案时以为我知道问题出在哪里。 我现在认为它正在深入研究我并不完全熟悉的上下文管理器和装饰器的世界。 话虽如此,我建议您在初始化 function 时需要调用相关上下文,我认为它应该类似于:
# In the docs they place this before the function, not inside it
bot = commands.Bot(command_prefix='.')
# This is the decorator which should invoke the appropriate commands
@bot.command()
# Note the additional parameter, ctx, which appears to be required in this case
async def time_messege(ctx,args):
# etc., etc.
#2 该错误强烈表明以下行greeting_channel = bot.get_channel("814238156396298310")
没有为greeting_channel
分配任何东西。
API 注意到 function bot.get_channel()
返回:
返回的频道或 None 如果没有找到。
您是否有可能只是输入了错误的频道 ID?
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.