Is there a way to make a discord bot that sends a user a message the mentioned number of times? - discord

I've been trying to make a discord bot with discord.py. I know how to make the bot send a message to the specified user, but I was wondering, is there a way to send the message a given number of times? If so, how can I do it?
As an example, if the user types !dm #discorduser hello 5, the bot will send "hello" to the specified user 5 times.
So far, my code is:
import discord
from discord.ext import commands
client = commands.Bot(command_prefix='.')
#client.event
async def on_ready():
print('Bot is ready.')
#client.command()
async def spam(ctx, member: discord.Member, *, content):
channel = await member.create_dm()
await channel.send(content)
client.run('bot token')

Here's my answer for the question, from my understanding you're trying to Direct Message a user x amount of times, and x can be changed depending on what the user wants.
#client.command()
# the arguments are: member, the amount of times you want to DM, and what you want to DM them.
async def spam(ctx, member : discord.Member, amount=1, content=None):
# this loop will keep doing the script below the amount of 'amount' times.
for i in range(amount):
channel = await member.create_dm()
await channel.send(content)

Related

get the number of users in a voice channel with my discord bot in python

I'm looking for a way to get the number of users in a voice channel and to get their username to use it in one of my discord bot command, I found that I can use ctx.guild.member to get the members of users in the server, but not in a specific channel, is there a method to do that ?
I tried this
from discord.ext import commands
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.author.VoiceChannel
Or this
bot = commands.Bot(intents=discord.Intents.all(),command_prefix = "/")
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
None of them works, the first tells me that member objects has no attribute VoiceChannel
and the second one tells me that ctx.message.author.voice is a NoneType object and has no attribute channel
You're very close.
#bot.command()
async def teamup(ctx):
channel = ctx.message.author.voice.channel
ctx has an author property; this returns the user/member object for the individual that invoked the command. The user/member has a voice property that returns the VoiceState object of the user. This has a channel property which will return the voice channel that the user is in. Importantly, this will be None if the user isn't in a voice channel. So, putting it together:
#bot.command()
async def teamup(ctx):
voice = ctx.author.voice
if not voice.channel:
await ctx.send("You are not currently in a voice channel")
return
channel = voice.channel
# do what you want with the channel here
await ctx.send(f"You're in {channel.name}!")

Discord Bot Take User's Text and Post it

I am looking for a discord bot made in .py that does those 3 things:
post to a specific channel
whatever user writes then bot deletes it and post it
information of who wrote what in another specific channel. example:
{user} wrote Hi my name is Nick
Thanks in advance! I have not tried something yet, because I am not that experienced. Thanks a lot.
Try this
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='prefix')
#bot.command()
async def say(ctx, channel: discord.TextChannel, *, arg):
message = ctx.message # Get your message
await channel.send(f"{ctx.author}: {arg}") # Send your message
await message.delete() # Delete user message
bot.run('token')

How can i send a DM message to someone just by mentioning his name in on_message in discord.py

I want the bot to send a DM message to the user whose name is in the message content, here is the code:
#commands.Cog.listener()
async def on_message(self, message):
for user in People: # People list includes all members' names in the server
if f'{user}' in message.content:
await message.user.send("Hi")
else:
pass
I know that the problem is in await message.user.send("Hi") but all I want is when i say in my msg (for example) "Hello Ahmed" the bot should send a DM message to ahmed saying "Hi"
You don't have to iterate everybody's name in the guild. discord.Message has attribute mentions. It returns a list of member that mentioned in the message. So you can just iterate that list.
#commands.Cog.listener()
async def on_message(self, message):
for member in message.mentions:
await member.send('Hi')

Sending messages to the first channel. Discord.py

I need to make it so that when the bot joins a new server, it writes a specific message to the very first text channel.
I tried to do something:
#bot.event
async def on_guild_join(guild):
print("Join to " + guild.name)
guild_to_audiocontroller[guild] = AudioController(bot, guild)
await guild_to_audiocontroller[guild].register_voice_channel(guild.voice_channels[0])
for guild in bot.guilds:
await guild.text_channels[0].send(join_message)
But it doesn't want to work, how do I do it?
As long as your bot has Send Messages permissions, all you need to send a message on join is:
#bot.event
async def on_guild_join(guild):
await guild.text_channels[0].send("I have joined the server")

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.

Resources