Reputation:
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'
Upvotes: 0
Views: 1221
Reputation: 26
I was having the same problem and then I found out that the ID must be an integer not a string, try bot.get_channel(814238156396298310)
Upvotes: 1
Reputation: 184
#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
I'll be honest, I started writing this answer thinking that I knew what the problem was. I now think it's delving into a world of context managers and decorators that I'm not entirely familiar with. Having said that, I'd suggest that you need to be invoking the relevant context when you initialise the function, which I believe should look something like:
# 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
The error strongly suggests that the following line greeting_channel = bot.get_channel("814238156396298310")
isn't assigning anything to greeting_channel
.
The API notes that the function bot.get_channel()
returns:
The returned channel or None if not found.
Is it possible that you simply have the wrong channel ID?
Upvotes: 0
Reputation: 2094
In the first case, it seems that the send_message
method that you are using no longer exists in the library version you have installed (check Migrating to 1.0).
In the second case, possibly the channel you have specified is not found so the bot.get_channel
method returns None
as expected.
Upvotes: 0