discord channel link in message - python

Using discord.py, I am making a bot to send users a direct message if a keyword of their choosing is mentioned.
Everything is working, except I just want to add the channel they were mentioned in to the message. Here is my code:
print("SENDING MESSAGE")
sender = '{0.author.name}'.format(message)
channel = message.channel.name
server = '{0.server}'.format(message)
await client.send_message(member, server+": #"+channel+": "+sender+": "+msg)
This results in a correct message being composed, but the #channel part of the message is not a clickable link as it would be if i typed it into the chat window myself. Is there a different object I should be feeding into the message?

In Discord: there is channel mention.
Try that way, do message.channel.mention instead of message.channel.name
it should able to link a channel in PM or everywhere.
Source: Discord Documentation

Related

I'd like my Discord.py bot to display a random embedded message from a single channel

I have a discord channel that is full of embedded messages. I would like have the bot display a random embedded message from this channel in to any other channel where the command is called, similar to the functionality shown below.
When I call this code while pointed at the embed channel it returns an error: "discord.errors.HTTPException: 400 Bad Request (error code: 50006): Cannot send an empty message"
I believe this is caused by the channel being totally embeds, but I am very stupid and open to being corrected. I have confirmed that the bot has full admin privileges and thus can see/write into any channel. I've also confirmed that the below code works on non-embeds in other channels.
Thank you in advance for any assistance you can provide.
#client.command()
async def bestpost(ctx):
channel = client.get_channel(channelID)
best_posts = []
async for message in channel.history(limit=500):
best_posts.append(message)
random_bestpost = random.choice(best_posts).content
await ctx.send(random_bestpost)
If the channel is totally embeds, then you have to look into the message's embed rather than the message content. From there you can just send the embed or access a property of the embed and send that.
For example:
#client.command()
async def bestpost(ctx):
channel = client.get_channel(channelID)
best_posts = []
async for message in channel.history(limit=500):
best_posts.append(message)
random_bestpost = random.choice(best_posts).embeds[0]
await ctx.send(embed=random_bestpost)

how to make a discord.py bot not accepts commands from dms

How do I make a discord.py bot not react to commands from the bot's DMs? I only want the bot to respond to messages if they are on a specific channel on a specific server.
If you wanted to only respond to messages on a specific channel and you know the name of the channel, you could do this:
channel = discord.utils.get(ctx.guild.channels, name="channel name")
channel_id = channel.id
Then you would check if the id matched the one channel you wanted it to be in. To get a channel or server's id, you need to enable discord developer mode. After than you could just right click on the server or channel and copy the id.
To get a server's id you need to add this piece of code as a command:
#client.command(pass_context=True)
async def getguild(ctx):
id = ctx.message.guild.id # the guild is the server
# do something with the id (print it out)
After you get the server id, you can delete the method.
And to check if a message is sent by a person or a bot, you could do this in the on_message method:
def on_message(self, message):
if (message.author.bot):
# is a bot
pass
You Can Use Simplest And Best Way
#bot.command()
async def check(ctx):
if not isinstance(ctx.channel, discord.channel.DMChannel):
Your Work...
So just to make the bot not respond to DMs, add this code after each command:
if message.guild:
# Message comes from a server.
else:
# Message comes from a DM.
This makes it better to separate DM from server messages. You just now have to move the "await message.channel.send" function.
I assume that you are asking for a bot that only listens to your commands. Well, in that case, you can create a check to see if the message is sent by you or not. It can be done using,
#client.event
async def on_message(message):
if message.author.id == <#your user id>:
await message.channel.send('message detected')
...#your code

Discord.py API for discord bot to listen to message, and delete the message in question

I'm looking to make a bot that uses the VirusTotal API to scan URLs sent in discord messages and if any are detected, they will delete the message in question, send a warning, and log the situation in a modlogs channel. This is the code for my on_message so far.
#bot.event
async def on_message(message):
urls = regex.findall('http[s]?://(?:[a-zA-Z]|[0-9]|[$-_#.&+]|[!*(),]|(?:%[0-9a-fA-F][0-9a-fA-F]))+',message.content.lower())
flag = False
for i in urls:
if (scan_url(i)):
flag = True
break
if flag:
#delete message, send warning, find the channel named "modlogs" and send the log
scan_url will be used to check each url individually and returns true if the link in question is malicious. I am stuck however on using the bot.event decorator and just a string of the message on how to find the message in question, delete it, send the warning in the same channel, then add to mod logs.
if flag:
await message.channel.send(send_warning_message_here) # Send warning message
modlogs_channel = discord.utils.get(message.guild.text_channels, name='modlogs') # Get modlogs channel
await modlogs_channel.send(send_log_message_here) # Send log
await message.delete() # Delete the message
You could also check if the message content starts with https:// or http://.
if message.content.startswith('https://') or message.content.startswith('http://):
# Scan URL
This helps in making API calls only when required.

Checking What channel a message was sent in, discord.py

i am making a message logger for fun and i wanted to know if with it saying what was said and who said it if i could see what channel it was in and print it, here is the logger, everything works I just wanted to add specification of channel.
code:
async def on_message(message):
msg = message.content
hook.send(f'|{message.author.name}|{msg}')```
async def on_message(message):
msg = message.content
channel = message.channel
hook.send(f'|{message.author.name}|{msg}')
It is all in the docs.
If you want to also mention the channel in the a message, then just message.channel.mention
You can answer this question yourself by looking at the documentation for on_message(). The message parameter is a Message object. The documentation for Message lists the attributes which includes channel. If you need the channel's name, then look at the documentation for Channel. Learning how to navigate and read this documentation will be incredibly helpful as you continue to work on your bot.

Discord.py Private Messages Bot

In Discord.py how can I make my bot privately send messages to other people in a server. Like, if I want to message a person in the server who I have not friend requested yet, I can use this bot. I want it to use a slash command like
"/{user}{message}"
And the bot will display the message for them and make the user who it was sent to only see it.
How do I do this using discord.py?
You have to get the member and create a DM with them, then send whatever you want to the DM. See the following code:
member = discord.utils.get(ctx.guild.members, id = (member id)) # could also use client.get_user(ID)
dm = await member.create_dm()
await dm.send('this will be sent to the user!')

Categories