I can't get it to work through properties like command or just content_types. No errors show up.
#dp.message_handler(commands=["start"])
async def start(msg:Message):
if msg.chat.type == ChatType.CHANNEL:
await msg.answer(msg)
I made so bot sent a message to the channel. Still nothing.
P.S It works with getUpdates
In the Dispatcher class there is a decorator called "channel_post_handler".
Related
I'm trying to simply create a function that sends a discord message to a specific channel when the bot runs. I want to later call this function in a separate python script.
The code below seems to run fine, and the bot appears online - but it is fails to send the message:
import discord
client = discord.Client()
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
on_ready()
I'm sure I'm missing something basic but I cannot figure out what it is. I've tried a similar function in Node.js and it works fine however, I would prefer the bot to work in python. Discord.py is up to date, python is v3.10.
Any help would be greatly appreciated.
Client.run is a blocking call, meaning that events registered after you call it won't actually be registered. Try calling Client.run after you register the event, as in:
import discord
client = discord.Client()
#client.event
async def on_ready():
logs_channel = client.get_channel(12345689123456789) # the channel ID
await logs_channel.send("Bot is up and running")
client.run("ABCDeFGHIjklMNOP.QrSTuV-HIjklMNOP") # the token
You also probably do not want to be manually calling an event handler. If you want to send a message by calling a function, you probably want to consider writing a separate function to the event handler.
I have problem creating simple python script which sends message to default channel when executed from terminal.
import discord
#bot.event
async def on_ready(ctx):
print('Online')
await ctx.send('Message sent!')
bot.run('MYTOKEN')
with this example I keep getting "ctx" is not defined
The issue here is that the on_ready event should not receive any parameters. See the Minimal Bot documentation here and the on_ready documentation in the Event Reference here. If you want to send a message when the bot connects. You must first get the channel object and then use the send method. You can find an example of getting a channel and sending a message in the FAQ section of the docs here
I am trying to get an output when the bot is invited to a server, but for some reason when I try to test my code it doesn't output anything. It doesn't give any errors as well. First I tried to do it from a Cog file:
#commands.Cog.listener()
async def on_server_join(self,server):
print("hello")
print(server.id)
then in the main file:
#client.event
async def on_server_join(server):
print("hello world")
print(server.id)
both doesn't result in any errors and they doesn't output anything when I'm kicking the bot from a server and then adding him back.
Other events, like on_ready and on_member_join are working OK.
Creating a new server and adding the bot there also doesn't trigger the event.
on_server_join was changed to on_guild_join when discord.py migrated to v1.0
I want to send a private message to a specific user with my discord bot. I am using discord==1.0.1
discord.py==1.3.1. I already tried using the docs (https://discordpy.readthedocs.io/en/latest/api.html?highlight=private%20message#discord.DMChannel) but i dont understand that. I tried following code, which didnt worked:
#client.command()
async def cmds(ctx):
embed = discord.Embed(title='Discord Server Befehle',
description='Description',
color=0x374883)
[...]
msg = await ctx.send(embed=embed)
await ctx.author.create_dm('Hi')
print(f'{command_prefix}help ausgeführt von {ctx.author}')
Try using the send function instead of the create_dm. create_dm only creates the channel between two users which discord does automatically as far as i know.
according to documentation
This should be rarely called, as this is done transparently for most people.
so it should be
ctx.author.send('Hi')
When I run the python code below, it doesn't pick up messages from other bots:
#bot.event
async def on_message(message):
print(message)
Is there any way to make so that my discord.py bot does pick up messages from other bots?
Discord.py bots are set to ignore messages sent by other bots, see the code here - more specifically lines 972 and 973:
if message.author.bot:
return
To work around this, you can subclass the bot, and override the process_commands method as shown below:
class UnfilteredBot(commands.Bot):
"""An overridden version of the Bot class that will listen to other bots."""
async def process_commands(self, message):
"""Override process_commands to listen to bots."""
ctx = await self.get_context(message)
await self.invoke(ctx)
and run your code using this bot instead. Probably not the best to use in production, but is a good way to facilitate testing of your bot via another bot
I decided to just use the channel.history(limit=10).flatten() and channel.fetch_message(ID) functions and put them in a loop, which also works well for my application.
As the messages are from bots, could it be because the bots are using embeds? Because discord cannot print message from embeds(Maybe unless if you use message.embeds) Check if the messages the bots are sending are plain text instead of embeds