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")
Related
I don't have much experience in discord.py, but I'm making a bot. Right now I added the welcoming message:
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(embed=emb)
but I want to mention the user who joined as this picture
May you help me doing this? Thanks!
Use member.mention
This returns a string which allows you to mention the member.
#client.event
async def on_member_join(member):
channel=client.get_channel(channel_ID)
emb=discord.Embed(title="NEW MEMBER",description=f"Thanks {member.mention} for joining!")
await channel.send(embed=emb)
You should however keep in mind that because of caching, the mention will most likely look something like this <#!123456789>.
You need the code to look like this:
#client.event
async def on_member_join(member):
if member.guild.id !=Guild.id:
return
channel = client.get_channel(channel_id) # replace id with the welcome channel's id
await channel.send(f"{member.mention} has arrived!, check out our announcments channel for server and bot announcements!")
await member.send(f"Thank you for joining {member.guild.name}!")
This line right here is you only want people that join YOUR server to send a msg, if you don't have any server with your bot in it, will say the msg still
if member.guild.id !=Guild.id:
return
{member.mention} is what mentions the user if you are using {} in the sending part you need the f function
Next code will make the code call the {}
await channel.send(f"{member.mention}")
This calls the guild name:
{member.guild.name}
I am not sure about client.get_channel, but you sure can try this:
#client.event
async def on_member_join(member):
guild = client.get_guild(serverID)
channel = guild.get_channel(channelID)
emb = discord.Embed(title="NEW MEMBER",description=f"Thanks {member.name} for joining!")
await channel.send(member.mention, embed=emb)
I am making a bot in discord.py and want my bot to ping a role when a Webhook sends message in a specific channel. Is there anyway to do this? Right now all I have is the channel id and I am pretty sure it is a client Event
#client.event
async def pingrole():
channel = client.get_channel("channel id")
async def on_message(message):
if message.author.id == webhook.id:
await ctx.send(role.mention)
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)
I want my bot to delete any messages typed in a channel 1 second after it is sent.
Is there a way to do so?
Use await ctx.send("message", delete_after=1). Refer this
try something like this:
#client.event
async def on_message(message):
await asyncio.sleep(1)
await client.delete_message(message)
await client.process_commands(message)
I want to delete a specific message from a specific user using discord.py
The user's id is: 462616472754323457
The message that he can't send: lmao
So if he sends lmao, it deletes the message and send "You can't do that <#462616472754323457>
Try discord.TextChannel.purge.
#client.event
async def on_message(message):
if len(await message.channel.purge(limit=200, check=lambda x: ('lmao' in x.content.strip().lower()) and x.author.id == 462616572754323457)) > 0:
await message.channel.send('You are not allowed to do that, <#462616572754323457>')
Accounting for when the bot is offline, the bot will check 200 messages before the current message and delete any that are from 462616572754323457 and have 'lmao' in the contents. (You can further enhance this by using re.)
You can use the on_message event, which triggers whenever a message is sent.
#bot.event
async def on_message(message):
await bot.process_commands(message) # add this if also using command decorators
if message.author.id == 462616472754323457 and "lmao" in message.content.lower():
await message.delete()
await message.channel.send(f"You can't do that, {message.author.mention}")
References:
discord.on_message()
Message.delete()
Bot.process_commands()
Member.mention