discord.py the welcome command - python

why does it's not working for me?
#client.event
async def on_member_join(member):
await client.get_channel(965214301520560178).send(f"{member.name} has joined")
thanks for the helpers!
I tried to make a welcome command in discord.py

If you want to ping the new member and that is the part that doesn't work (a bit unclear) it should be:
await client.get_channel(965214301520560178).send(f"<#{member.id}> has joined")
also, make sure to check if the bot has the necessary permissions and/or intents

Related

Bot Mentions Discord.py

if client.user.mentioned_in(message):
await ctx.send("Hi!")
So I want my bot to respond to anyone who #mentions it. It does work as it should, but the only problem it'll respond to #everyone and #here pings. It's probably simple and I am overthinking it but I just want to know if there is any way to make the bot respond to messages it's actually mentioned in, not #everyone or #here pings?
Check whether the bot's user id is in the message content. Try the following:
if str(client.user.id) in message.content:
await ctx.send("Hi!")
Hmm Try This
#client.event
async def on_message(message):
if f"<#!{client.user.id}>" in message.content:
await ctx.send("Hi!")
You can use the
#client.event
async def on_message(message):
Then detect if the message has mentioned the bot:
if client.user.mentioned_in(message):
await message.channel.send("Hi!")
Documentation: https://docs.pycord.dev/en/master/api.html?highlight=mentioned#discord.ClientUser.mentioned_in

Discord.py List all server names where the bot is in

I made a bot but I want the bot to make list all the server names where it is when you type a command.
Can any one help me, please?
await ctx.send('\n'.join(guild.name for guild in bot.guilds))
Just remember to pass an intent with guilds enabled in your bot's constructor
#commands.command()
async def servers(self, ctx):
activeservers = client.guilds
for guild in activeservers:
await ctx.send(guild.name)
print(guild.name)
This code should work
#client.command()
async def server(ctx):
servers = list(client.guilds)
for server in servers:
await ctx.send(server.name)
it works for me...

Keywords in discord.py

I am trying to make a bot that responds to certain keywords but for some reason I can't seem to get it to work. E.g. if I were to say "This game is so hard" it would respond with "Hehe: Hard". (Childish I know). How would I edit this code to make it work because at the moment it refuses to work. thank you
#client.event
async def on_message(message):
if "hard" in message.content.lower():
await message.channel.send('Hehe "Hard"')
return
Where is this supposed to happen? In a server? Do other functions work, is your bot functioning at all? This code seems to have worked for me:
#client.event
async def on_message(message):
if not message.author.bot and "hard" in message.content.lower():
await message.channel.send('Hehe "Hard"')
Would it be possible for you to send all of the code you currently have?

Discord Bot Delete Messages in Specific Channel

TIA for your help and apologies, I'm a newbie so this may be a foolish question. I've searched through and can't find anything specific on how to make a discord bot (in Python) delete messages only within a specific channel. I want all messages sent to a specific channel to be deleted, their contents sent via PM to the user and the user's role changed.
Is there a way to use on_message and specify in an specific channel?
#client.event
async def on_message(message):
user = message.author
if message.content.startswith("Cluebot:"):
await message.delete()
await user.send("Yes?")
await user.remove_roles(get(user.guild.roles, "Investigator"))
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
Is it possible to make this work through a command instead of an event?
Thanks for your help. :)
The problem I'm having is I'm also using commands which now no longer work because the bot only responds if the message begins with "Cluebot:" Can I have the bot only look for "Cluebot:" in a specific channel?
About this problem the clue is:
await bot.process_commands(message)
as docs says Without this coroutine, none of the commands will be triggered.
about the main question you could try using get_channel by ID and then purge it:
eg.
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1)
you can add check to purge() to check for deleting specific message:
eg.:
def check(message):
return message.author.id == [author's ID]
#client.event
async def on_message(message):
purgeChannel = client.get_channel([CHANNEL ID]])
await purgeChannel.purge(limit=1, check=check)

Discord.py - How can I have a command invoked in an event?

So I am trying to create an event in discord.py where, whenever a user mentions/pings the bot, it will say something. The code below runs without errors, however the only way this will work is if my command prefix is in the message.
For my bot, the prefix is "/", so whenever I mention the bot with a "/" in the message it will say something. And if I decide to just mention the bot, the bot does not respond. I am pretty sure it's got something to do with the last line of code but I don't know how to fix this issue.
#client.event
async def on_message(message):
if client.user.mention in message.content.split():
await message.channel.send('You mentioned me!')
else:
await client.process_commands(message)
The code is written in Python 3.7.4.
All help would be appreciated!
When someone mentions a user the bot reads <#userid>. So if you want to use an on_message function you would want to include an
if '<#bot/user id>' in message.content:
await message.channel.send('hi')
You can also do something like...
if bot.user in message.mentions:
await message.channel.send('hi')

Categories

Resources