I tried to send a message with the discord bot to a specific text channel, in these 2 ways but none of the 2 really worked for me.
Here I leave the 2 ways that I try with the respective errors that it returns for each mode
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)
This is the error:
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#
if i use the other code (the commented code, and not the client) it give a similar error
what i need define 'client' ?
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)
And with this code, give that error
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'
What algorithm do you recommend and what should I do to avoid these errors? What attribute should I put?
I use that version of discord library
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'
the id is correct
I also tried the code that they suggest in the answers, but it keeps giving error...
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)
The error:
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'
And if I add this extra parameter in the function, this give thar error:
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'