I have a discord bot, and I want it to do something like
once a member joins
DM member (message)
if member replies with key
give them this role
Thanks
You need to use the function on_member_join().
#client.event
async def on_member_join(member):
pass
Then put the code message sending/receiving code in there. With your example you would do:
#client.event
async def on_member_join(member):
await client.send_message(member, 'Prompt.')
m = await client.wait_for_message(author=member, channel=member)
if m.content == 'key':
# give the user the role
await client.send_message(member, 'Role added')
else:
await client.send_message(member, 'Incorrect key')
To find out how to give a user a role from a dm to a server, read this question: How To Assign A User A Role In A Server From A Direct Message - Discord.py
#client.event
async def on_member_join(member):
await member.send("hello")
This code sends hello to the user when he/she joins the server.
Hope this is helpful for everybody
Related
This will not work for some reason. I have no idea why it doesnt work
# adds an event
#client.event
async def on_message(message):
# so i dont have to say message.content a lot
msg = message.content
# if a message starts with !dm create a dm channel with the specified user
if msg.content.startswith("!dm"):
await create_dm('user')
You need to create a DM targeting a user.
#client.command()
async def hello(ctx):
user = ctx.author
await ctx.send(f"Hello, {user.mention}")
dm = await user.create_dm()
await dm.send('hello')
Also, as you can see in this code snippet here, I recommend setting up a command (as it looks like you're doing) with #client.command rather than targetting an on_message event.
# adds an event
#client.event
async def on_message(message):
# so i dont have to say message.content a lot
msg = message.content
# if a message starts with !dm create a dm channel with the specified user
if msg.content.startswith("!dm"):
await member.send (member "or whatever is defined")
Using member.send sends a message to a member creating a dm with the bot.
Soo i want my bot to send a message when someone join but it is not workin
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
and
i want my bot to take a massage from me and send it to the guy i say but this is not workin too
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
and
here is my WHOLE code:
import os, discord,asyncio
import keep_alive
from discord.ext import commands
client = commands.Bot(command_prefix="!")
token = os.environ.get('Token')
GUILD = os.environ.get('Guild')
#client.event
async def on_ready():
print(f'{client.user} is connected')
#client.command()
async def dm(ctx, member: discord.Member):
await ctx.send('what do u want to say bitch!')
def check(m):
return m.author.id == ctx.author.id
massage = await client.wait_for('massage', check=check)
await ctx.send(f'send massage to {member} ')
await member.send(f'{ctx.member.mention} has a massage for you: \n {massage}')
#client.event
async def on_member_join(member):
channel = discord.util.get(member.Guild, name='general')
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
keep_alive.keep_alive()
client.run(token)
For making a welcome channel, it is safer to use get_channel instead of get. Because in your code, every time you rename your channel, you need to change your code too, but channel ids cannot be changed until if you delete and create another one with the same name.
Code:
#client.event
async def on_member_join(member):
channel = client.get_channel(YOUR_CHANNEL_ID_GOES_HERE)
await channel.send(f'Hey welcome to my server {member.mention}, hope you enjoy this server!')
As for the dm command, I recommend you to get your message as a function parameter. Also you can check when you're DM'ing your bot with the isinstance() function. There is a * before the message parameter though. Its purpose is collecting all of your messages with or without spacing.
Code:
#client.command()
async def dm(ctx, member:discord.Member,*, message):
if isinstance(ctx.channel,discord.DMChannel):
await member.send(f'{ctx.member.mention} has a message for you: \n {message}')
So, when a user sends a message I want my bot to send a message to that channel with an emoji
so far I've been able to send it to a specific channel, but after that I haven't been able to send a message to any channel that a message was deleted in.
maybe I just don't know the attributes.
thanks for all the help in advance!
#bot.event
async def on_message_delete(ctx):
if ctx.author.id != 835676293625282601:
author = ctx.message.author
del_msg = await channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Events never take ctx as an argument, if you take a look at the docs you can see that on_message_delete takes message as the only argument
#bot.event
async def on_message_delete(message):
if message.author.id != 835676293625282601:
author = message.author
del_msg = await message.channel.send(":eyes:")
await author.send(del_msg)
await asynciaito.sleep(10)
await del_msg.delete()
Also you don't have to learn all the attributes from all the discord objects, just read the documentation
I want to to make some code that will mention the name of the user that sends the message, this is what I have tried:
Im new to making discord bots, and python so any help would be perfect
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {member}")
You can do this with user.name
#client.command()
async def hello(ctx):
await ctx.send(f"hello, {ctx.author.name}")
The above can be called by {prefix}hello, if you want to say "hello, {name}" even when user just send "hello" (without prefix), then use on_message event.
#client.event
async def on_message(message):
if message.author.bot: return
if message.content.lower() == "hello":
await message.channel.send(f"Hello, {message.author.name}")
await client.process_commands(message)
Docs: on_message, user.name
Assuming you want to mention the user who used the command. the ctx argument has a lot of attributes one of them is the author
#client.command()
async def hello(ctx, member):
await ctx.send(f"hello, {ctx.author.mention}")
If you want the user to say hello to another user, he can mention or just type the name.
#client.command()
async def hello(ctx, *, user: discord.Member = None):
if user:
await ctx.send(f"hello, {user.mention}")
else:
await ctx.send('You have to say who do you want to say hello to')
All of the above providing you are using discord.ext.commands
This should work:
str(ctx.message.author)
Or:
str(ctx.author)
is there a way i can create a invite link using Discord.PY? My code is the following/
import discord
from discord.ext import commands
import pickle
client = commands.Bot("-")
#client.event
async def on_message(message):
message.content.lower()
if message.author == client.user:
return
#checks if the bot it running.
if message.content.startswith("message"):
await message.channel.send("hello there")
#tells the user where they are.
if message.content.startswith("whereami"):
await message.channel.send(f"You are in {message.guild.name} " + \
f"in the {message.channel.mention} channel!")
##Creates Instant Invite
if message.content.startswith("createinvite"):
await create_invite(*, reason=None, **fields)
await message.channel.send("Here is an instant invite to your server: " + link)
client.run('token')
If needed, let me know what other information you need, and if i need to edit this to make it more clear. If I need to import anything else, please inform me as to what libraries are needed.
#client.event
async def on_message(message):
if message.content.lower().startswith("createinvite"):
invite = await message.channel.create_invite()
await message.channel.send(f"Here's your invite: {invite}")
And using command decorators:
#client.command()
async def createinvite(ctx):
invite = await ctx.channel.create_invite()
await ctx.send(f"Here's your invite: {invite}")
References:
TextChannel.create_invite()
discord.Invite - Returned from the coroutine.
Is there a way to create one, but only with one use?
i have an on_message event: if someone type xy, the bot will kick him. and after the kick i want to send him an xy message.(its ready)
and after this i want to send him an invite