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
Related
So I want to know if it is possible, that a bot gets the content sent to it in a dm and send that in a specifyed channel on a server.
So basically you dm the bot the word "test" and the bots sends the word in a channel of a server
Yes, it is possible for a bot to receive a direct message and then repost the message in a specified channel on a server. This can be done using the Discord API.
You can do the following:
Create a Discord bot and add it to your server. You can do this using the Discord developer portal.
Use the Discord API to listen for messages sent to the bot in a DM. You can do this using the message event and the DMChannel class in the Discord API.
When the bot receives a DM, use the Discord API to repost the message in the specified channel on the server. You can do this using the send method of the TextChannel class in the Discord API.
Yes, this is possible:
First, we use the on_message() event to detect when a message is sent.
We then check if the message is sent in a DM.
If so, we will send a message to a specific channel.
Here is one way you can implement it:
# import ...
bot = discord.Bot(...)
#bot.event
async def on_message(message):
# check if it's
if isinstance(message.channel, discord.DMChannel):
# get the channel from ID
channel = client.get_channel(CHANNEL_ID) # put channel ID here
await channel.send("test") # whatever you want to send
#bot.event:
async on_message(message):
if message.DMChannel:
# define channel and send message to channel.
You can also refer to DOCS.
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".
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 a small problem that i dont know how to fix it. I want to edit a messages embed, that was send by a discord webhook directly if it gets posted. So that's why i use the 'on_message' event for this. In the normal way, you can just fetch the message and than use msg.edit but that doesnt work for webhook messages.
How can i edit the message from the webhook directly when it gets posted? I tried to use the api "discord-webhooks" but i couldn't find a solution for that what i want.
I tried this:
#bot.event
async def on_message(message):
##### Netflix-News edit ######
if message.channel.id == 804484025166856262:
embed = discord.Embed(title=f"{title}",
url=f"{lonk}",
description=f"{descfix}",
color=0xe74c3c)
embed.set_thumbnail(url=f"https://i.imgur.com/4np2bdK.png")
embed.set_image(url=f"{Thumbnail}")
embed.set_footer(text=f"{footer}", icon_url="https://i.imgur.com/4np2bdK.png")
webhook = DiscordWebhook(url='LINKHERE')
webhook.embed = embed
await webhook.edit(sent_webhook)
Webhook.edit edits the webhook itself, not a message. To edit a message use Webhook.edit_message
await webhook.edit_message(message_id, embed=embed)
PS: You didn't define sent_webhook
Reference:
Webhook.edit_message
I want to send message to dms using my discord bot and because the api has changed from client.send_message(user, message) to channel.send(message) I have no idea how to do that, also I dont want to make it dependent on the on_message(message) event. Any ideas?
If you have the user ID you can do:
user = client.get_user(user_id)
await user.send('Hello')
However If you already have the User object, you can do the following:
await message.author.send('Hey')
Where message is a Message object received from a on_message() event.
As for whether you can send private messages without first receiving an event unfortunately that is not be possible due to obvious spam-related issues.