我尝试通过这 2 种方式使用 Discord bot 将消息发送到特定的文本频道,但是这 2 种方式都不适合我。
这里我留下了我尝试的 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'
如果我在函数中添加这个额外的参数,就会出现错误:
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'
答案 0 :(得分:1)
我遇到了同样的问题,然后我发现 ID 必须是整数而不是字符串,请尝试 bot.get_channel(814238156396298310)
答案 1 :(得分:0)
在第一种情况下,您使用的 send_message
方法似乎不再存在于您安装的库版本中(检查 Migrating to 1.0)。
在第二种情况下,可能未找到您指定的频道,因此 bot.get_channel
方法返回 None
as expected。
答案 2 :(得分:0)
#1
假设您使用的是 discordpy,错误是正确的:根据他们发布的 API,discord.Client
和扩展 discord.ext.commands.Bot
都没有方法 .send
,尽管它可以在 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
老实说,我开始写这个答案时认为我知道问题是什么。我现在认为它正在深入研究我并不完全熟悉的上下文管理器和装饰器的世界。话虽如此,我建议您在初始化函数时需要调用相关上下文,我认为它应该类似于:
# 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 指出函数 bot.get_channel()
返回:
返回的频道或 None 如果未找到。
您是否可能只是拥有错误的频道 ID?