Welcome message not being sent in discord.py - python

So I made a new server and made a custom bot for it recently, but the welcome message doesn't really seem to work... It is also supposed to give a particular role to the member who joined but I removed that code and the current code still doesn't work.
Sadly, the command prompt gives no errors. I have no idea what to do now.
Here is the code -
#commands.Cog.listener()
async def on_member_join(self, member):
#GETTING THE GUILD, CHANNEL AND ROLE
channel = self.client.get_channel(828481599057166356)
name = member.name
#CREATING THE WELCOME EMBED
welcomeem = discord.Embed(title = f"Hey there {member.name}!", description = f"Welcome to {chanenl.guild.name}! Have a fun time here in TigerNinja's server!")
welcomeem.add_field(name="1. Rules", value = f"{name}, before you start having fun here, make sure to check <#752474442650878002> and read all rules as they will come in handy in the server!", inline=False)
welcomeem.add_field(name="2. Self-roles", value = f"{name}, be sure to check out <#776293478594379797> and get all roles you want!", inline=False)
welcomeem.add_field(name="2. Help", value = f"{name}, need help with this bot then type `t.help`. If you need help relating to something else, contact the mods via dms, but don't ping them!!", inline=False)
#SENDING THE PING, EMBDE AND ADDING ROLE
await channel.send(f"Welcome, {member.mention}!")
await channel.send(embed=welcomeem)
(I am using cogs btw)

Did you set up this command as bot event? Before adding such command you should add
#bot.event (or whatever name you gave to discord.Client in your code + .event, it might be useful to share more of your code, just be careful not to include your token or any private information).
For more info, look into this post, I believe it is the answer you are looking for:
Discord.py on_member_join not working, no error message

Following the dicord.py docs:
channel = guild.get_channel(828481599057166356)
should be:
channel = client.get_channel(828481599057166356)
EDIT:
I found something else:
welcomeem = discord.Embed(title = f"Hey there {member.name}!", description = f"Welcome to {chanenl.guild.name}! Have a fun time here in TigerNinja's server!")
you misspelled "channel" in {chanenl.guild.name}

Related

Discord.Py / Bot was working great at first. Now the bot is live but when I try to give a command, Nothing happens and it doesnt show any error

Is my code supposed to be working fine? because it was doing well before. Right now after I start running the code. And input and relay the messages . It wouldnt be sending it anymore. And would getting the channel name better rather than getting the channel id? if getting the channel id is better, What should I replace with if channel.name in? , If my code is fine does that mean the issue is gonna be the discod permissions?
#commands.has_permissions(kick_members=True)
async def alert(ctx, *, msg): #ALERT
for guild in client.guilds:
role = get(guild.roles, name = 'RayGunOptions Alerts') # roles
for channel in guild.channels:
if channel.name in ('π—’π—½π˜π—Άπ—Όπ—»-π—”π—Ήπ—²π—Ώπ˜π˜€','gunvir-gay'): # change for the list of channel names you got in your discord server
one = Button(style=ButtonStyle.URL, label='Twitter', url="https://twitter.com/RayGunsOptions")
two = Button(style=ButtonStyle.URL, label='Discord', url="https://discord.gg/6Fh4MTZN")
three = Button(style=ButtonStyle.URL, label='Trading Journal', url="https://tradingjournal.com")
embed=discord.Embed(title= ':moneybag: **Option Alert** :moneybag:', description= (msg), url='https://twitter.com/RayGunsOptions', color=0x33FF9F, timestamp=datetime.datetime.utcnow())
"""embed.set_author(name="Crypto Alert", icon_url = ctx.author.avatar_url)""" #Top left name # IGNORE
embed.set_thumbnail(url='https://pbs.twimg.com/profile_images/1383574983645962246/kD6PNI_L_400x400.jpg')
embed.set_footer(icon_url = ctx.author.avatar_url, text='Powered by Duck Programming',)
await channel.send(f"{role.mention}")
await channel.send(embed=embed)
await channel.send(
'** **',
components=[
[one,]
]
)
If you have a on_message_function in your code please add this in the end:
await self.bot.process_commands(message) # or client if you don't use bot
Source: https://discordpy.readthedocs.io/en/stable/faq.html?highlight=on_message#why-does-on-message-make-my-commands-stop-working
Also if your command is not in your cog please don't do:
commands.command()
Instead do:
bot.command() #or client.command()

Is there a way to see if a member is being mentioned in the embed?

I am using discord.py and I am trying to get the member object from the command, and when a button is pressed, it edits the message and display's user mod log data. I got it working but in a sort of odd way, it pings the person in the message, and then the event checks to see if there was a ping in (interaction) I am wondering if there is a way to look inside the embed for a mention like instead of interaction.message.mentions is there something like that for embeds instead of message? Thanks in advance!
#client.command()
async def modlogs(ctx, member: discord.Member):
main=discord.Embed(title=" ", description=f"{ctx.author.mention} please use the buttons below to navigate {member.mention}'s modlogs.", color=0x76dba8)
main.set_author(name=f"{member}", icon_url = member.avatar_url)
ram_member = member
await ctx.send(f"{member.mention}",
embed = main,
components=[[
Button(style=ButtonStyle.blue, label="Warnings", custom_id= "blue"),Button(style=ButtonStyle.blue, label="Kicks", custom_id= "red"),Button(style=ButtonStyle.blue, label="Bans", custom_id= "green")
]],
)
#client.event
async def on_button_click(interaction):
print(interaction.message)
if interaction.message.mentions:
if (interaction.message.mentions.__len__() > 0):
for member in interaction.message.mentions:
if collection2.count_documents({"_id": member.id}) == 0:
embed1=discord.Embed(description=f"{member.mention} has no warnings!", color=0xff0000)
embed1.set_author(name="Error")
else:
Get a Message
Msg = await Bot.get_message(channel, id)
Get an Embed
Look at the first answer to [this question][1].
Emb = discord.Embed.from_data(Msg.embeds[0])
Get the fields
Look at the answer to [this question][2].
for field in Emb.fields:
print(field.name)
print(field.value)
Get the description
Look at the first answer to [this question][3].
Des = Emb.description
Search for the mention
>>> Member.mention in Des
True
Searching a mention
A mention begins with ```#!``` and ends with ```>```.
So you might use this fact to search a mention into a string (Embed.description returns a string-type object).
Like this:
Men = Des[Des.find("<#!"):Des.find(">")]
UserID = int(Men[3:])
Member = ctx.fetch_user(UserID)
Links
Discord.py get message embed
How to align fields in discord.py embedded messages
discord.py Check content of embed title
How do I mention a user using user's id in discord.py?
In conclusion
I guess it will work.

How to program a discord.py bot to dm me when a command is used by another person?

I want to create a command that people can use to send suggestions to me. They'd use the command "!suggest", followed by their input, and the bot privately dms that whole message to me. However I am struggling with making the bot dm me instead of the author of the command.
if message.content.startswith('!suggest'):
await message.author.send(message.content)
The problem here lies with the author part in the code- instead of dming me every time, it will dm the person who typed the command, which is useless to me. How can I replace 'author' with my discord ID, to make it dm me and only me with the message? I've asked this before and all answers have not worked for me. These are the solution's I've gotten that did not work:
message.author.dm_channel.send("message"), but that is the same problem, it does not get rid of the author issue.
me = await client.get_user_info('MY_SNOWFLAKE_ID')
await client.send_message(me, "Hello!")
This code also did not work for me. The user that it is dming is the same each time, so I have no need to run the get_user_info in the first place. And whenever the second line runs, I get an error code because the MyClient cannot use the send_message attribute.
Your code is from the old discord version. It has been changed in the discord.py rewrite. I also recommend you to use discord.ext.commands instead of using on_message to read commands manually.
As for the dm, simply use
your_id = 123456789012345678
me = client.get_user(your_id)
await me.send("Hello")
Note that it requires members intents enabled. Use await fetch_user(your_id) if you don't have it enabled. Also, note that there might be also rate-limiting consequences if multiple users use commands simultaneously.
Docs:
get_user
fetch_user
User.send
#client.command()
async def suggest(ctx, message = None):
if message == None:
await ctx.send("Provide a suggestion")
else:
await ctx.{your_id}.send("message")
Please don't mind the indent
You can dm a user by doing this:
user = client.get_user(your_id_here)
await user.send('work!')
FAQ : https://discordpy.readthedocs.io/en/stable/faq.html#how-do-i-send-a-dm
if you want the full code here it is:
#client.command()
async def suggest(ctx, *, suggest_text):
user_suggestor = ctx.author
user = client.get_user(user_id) # replace the user_id to your user id
if suggest_text == None:
await ctx.send("Enter your suggestion! example : !suggest {your suggest here}")
else:
await user.send(f"{user_suggestor.name} sent an suggestion! \nSuggestion : {suggest_text}") # the \n thing is to space
thank me later :D

discord.py new invite link

i'm trying to make it so when ever you do the invite command it will make a brand new invite to the server with unlimited uses and never expires, this is the current code i've made but doesn't work and i'm struggling to work-out why, I've looked online and found nothing I hope you guys can all help :)
#bot.command(pass_context=True)
async def invite(ctx):
invitelinknew = await bot.create_invite(destination = ctx.message.channel, xkcd = True, max_uses = 100)
embed=discord.Embed(title="Discord Invite Link", description=invitelinknew, color=0xf41af4)
embed.set_footer(text="Discord server invited link.")
await bot.say(embed=embed)
I am assuming you want the bot to send the invite link directly to the chat because of the line: await bot.say(embed=embed).
First of all, I'm not sure setting the variable to embed is a good idea because this name might already be in use (not 100% sure though).
I believe that the issue with your code is the last line where the bot sends the message. Since there is no traceback, this is hard to tell for sure but I think replacing it with: await bot.send_message(ctx.message.channel, embed=embed) should fix your problem.
I would change the code to something like this instead:
#bot.command(pass_context=True)
async def invite(ctx):
invitelinknew = await bot.create_invite(destination = ctx.message.channel, xkcd = True, max_uses = 100)
embedMsg=discord.Embed(color=0xf41af4)
embedMsg.add_field(name="Discord Invite Link", value=invitelinknew)
embedMsg.set_footer(text="Discord server invited link.")
await bot.send_message(ctx.message.channel, embed=embedMsg)
Feel free to comment if this doesn't solve your problem.

DM commands to discord bot

I recently had the idea of DMing my bot commands. For example, a command which would unban me from every server that the bot is on.
Unfortunately I don't have any starting point for the command because I'm not even sure DMing commands is possible.
Keywords like discord.py, command or DM are so common in google that finding any good info on the topic is very hard.
I'm looking for a way for the bot to receive DMs as commands and only accept them from me (my ID is stored in the variable ownerID if anyone wants to share any code).
While I'm mostly looking for the above, some code for the DM unban command would also be very helpful.
EDIT: I was asked to show some example code from my bot. Here is the code for the command number which generates a random number and sends it in a message. I hope this gives you an idea of how my bot is made:
#BSL.command(pass_context = True)
async def number(ctx, minInt, maxInt):
if ctx.message.author.server_permissions.send_messages or ctx.message.author.id == ownerID:
maxInt = int(maxInt)
minInt = int(minInt)
randomInt = random.randint(minInt, maxInt)
randomInt = str(randomInt)
await BSL.send_message(ctx.message.channel, 'Your random number is: ' + randomInt)
else:
await BSL.send_message(ctx.message.channel, 'Sorry, you do not have the permissions to do that #{}!'.format(ctx.message.author))
You can send commands in private messages. Something like
#BSL.command(pass_context=True)
async def unban(ctx):
if ctx.message.channel.is_private and ctx.message.author.id == ownerID:
owner = await BSL.get_user_info(ownerID)
await BSL.say('Ok {}, unbanning you.'.format(owner.name))
for server in BSL.servers:
try:
await BSL.unban(server, owner) # I think having the same name should be fine. If you see weird errors this may be why.
await BSL.say('Unbanned from {}'.format(server.name))
except discord.HTTPException as e:
await BSL.say('Unbanning failed in {} because\n{}'.format(server.name, e.text))
except discord.Forbidden:
await BSL.say('Forbidden to unban in {}'.format(server.name))
else:
if ctx.message.author.id != ownerID:
await BSL.say('You are not my owner')
if not ctx.message.channel.is_private:
await BSL.say('This is a public channel')
should work. I'm not sure what happens if you try to unban a user who is not banned, that may be what raises HTTPException

Categories