Make Discord DM a copy of messages - discord

I Have a discord bot that is basically deleting posts that don't match a regex format.
It then DM's the user a quick message that includes the required format.
I am wondering if it'd be possible for the bot to send the user their original message as a second DM or part of the DM it is already sending out.
Here is the code
if valid_check:
if re.match(self.regex, message.content) or re.match(self.regex2, message.content) and not gutted:
pass
else:
if not message.author.bot:
try:
await message.delete()
content = "Please use the following Format when posting:\n" \
"[Location] [H] [W]\n" \
"\nExample:\n" \
"[USA-CA] Narnia [H] Faun Hoof" \
"[W] Lions Tears.\n"
await message.author.send(content=content)
except:
pass

You can take the original message.content, save it, and append it to your new content as you please.
Beware though, consider what happens if someone sends a message containing #everyone. You should sanitize the user input.

Related

How to set up a discord bot that replies upon mention to messages which contain certain words without prefix command?

I'm trying to create a simple discord bot that replies with mention to messages that contain certain words.
Also, I want the bot to respond only when it gets mentioned and without a prefix.
This was my try, but the bot didn't reply at all to the messages.
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='')
#client.event
async def on_message(message):
if client.user.mentioned_in(message){
if message.content == "Hi"{
await message.channel.send(f"Hello {message.author.mention}")
}
if message.content == "How are you?"{
await message.channel.send(f"I'm good {message.author.mention}")
}
}
client.run("TOKEN")
message.content contains the entirety of the message text so when you do #mybot Hi, the message.content will be something like: <#MY_BOT_ID> hi. Your code is checking that the message.content is exactly equals to either Hi or How are you? and that is not going to be the case.
You could use Python's in operator to check for certain text:
#client.event
async def on_message(message):
if client.user.mentioned_in(message):
# making the text lowercase here to make it easier to compare
message_content = message.content.lower()
if "hi" in message_content:
await message.channel.send(f"Hello {message.author.mention}")
if "how are you?" in message_content:
await message.channel.send(f"I'm good {message.author.mention}")
client.run("TOKEN")
Though, this isn't perfect. The bot will also respond to anything with the characters hi in. You could split the content of the messages by word and check that or you could use regular expressions. See this post for more info.
Additionally, your python code had some {} brackets - which is invalid syntax for if statements - I've corrected that in my example. Consider looking up python syntax.

discord bot - send multiple variables from user

I am trying to let user send multiple variables to the bot. types are string, uint.
So in the end, user needs to send 'hello world` and 5.
Is the only way that user sends one string and i should be doing the split in the bot code ? such as user sends hello world&5 and then in bot code, I do msg.content.split('&').
Am I on the right path or is there a better way ?
This is another way to do it:
#bot.command()
async def send_message(ctx, *args):
print(args)
Then users will be able to write for example "hello world" "test 123" test 5 and discord.py will split the input.
Assuming you use JavaScript and discord.js; yes, you are on the right path. Next, you just need to trim the string if users pass spaces around the &.

Saving message content to a list discord.py

I'm trying to create a playlist which the bot can send but I can't figure it out, here's the code I've tried although it only takes the text to trigger the command and I can't access the list anywhere.
Code:
#client.event
async def on_message(message):
if message.content == "=playlist":
playlist = message.content
await message.channel.send(playlist)
Any ideas?
I don't understand what the problem is, by the looks of it, when a users message is =playlist, your code will send all the contents of that message essentially back to them. Is that not what you want?

How to check if my discord bot already dmed a person and it wont dm the person again (discord.py)

I have a discord bot that handles with alts, i'm looking for a way that my bot knows if he dmed the person already before (explaining why he was kicked) and it wont dm them again. My function is like this:
#client.event
async def on_member_join(member):
channel = member.guild.text_channels[0]
if something
await channel.send(f"**{member.display_name}** was kicked")
await member.send("**Hi, your account was kicked due to reason** \n"
"**please try again later!**\n"
f"**{member.guild.name}.**")
await member.kick(reason=None)
else:
pass
My problem is that every time someone is kicked my bot dms them and I want it to dm the user kicked only once in their lifetime (without saving which user was dmed before).
would like to get help :)
You could have a look at this but you should at least save their id's to a text file.

Deleting certain file type attachments in a specific channel?

I'm trying to add certain files from being posted to the general channel as we have designated channels for certain attachments, clips, videos, music, etc. I'm fine on getting the bot to recognize links, however, having a hard time getting it to recognize attachments, more specifically, .mp4 attachments.
I added a whitelist of acceptable attachments in an array, then try and check the message author attachment to see if it's okay to post, if its an .mp4 it should be deleted.
The try function is within the on_message event decorator.
whiteList = ['bmp','jpeg','jpg','png']
try:
for attachment in message.attachments:
#Get general channel ID
channel = client.get_channel(521376573245358081)
if message.channel is channel and attachment['filename'].split('.')[-1] not in whiteList:
await message.delete()
botsMessage = await channel.send("{0.mention} Please refrain from posting videos in General. You may post them in #videos".format(message.author))
await asyncio.sleep(5)
await botsMessage.delete()
except:
print('Unknown error')
No error comes of this as when I test this the attachment remains, the bot passes over the function and prints the console message (used for debugging to make sure the code reaches that far). Any suggestions?
attachment['filename'].split('.')[-1]
You treated attachment as an dictionary that has a key called filename.
You should have treated attachment as an object that has a property called filename as follows:
attachment.filename.split('.')[-1]
Also, you should break the loop whenever the message is deleted,
# ...
botsMessage = await channel.send("{0.mention} Please refrain from posting videos in General. You may post them in #videos".format(message.author))
await asyncio.sleep(5)
await botsMessage.delete()
break
# ...
in the event that the user have sent mutiple video files, the loop will still continue even after you delete the message. Which may cause it to try to delete a deleted message
The break statement prevents the above from happening.

Resources