I want to make that when a user join to the server where this bot is located, the bot should send him a greeting message in the dm. But when connecting, the bot does not send a message. No errors are displayed. How can I fix this? My code:
#client.event
async def on_member_join():
await member.send("Welcome!")
#client.eevent
async def on_member_join(member):
await member.send("Welcome!")
you need to have an arguement in the function!
Also you need to enable intents in your bot
client = discord.Client(command_prefix="!", intents=discord.Intents.all(),case_insensitive=True)
you also need to enable all intents in the discord Developer portal
Related
I am working on a bot which is supposed to send slash command in the Discord channel and those slash commands will be received by another bot in the same channel. But when I send a message formatted as a slash command, the other bot doesn't detect it as a command but as a simple text message. Here is my code;
import discord
import asyncio
client = discord.Client()
#client.event
async def on_ready():
print("Bot is ready.")
#client.event
async def on_message(message):
async with message.channel.typing(): await asyncio.sleep(2)
# Send a message after 5 seconds
await message.channel.send("/spoiler 'this is spoiler'")
return
client.run('My_Bot_Token')
I tried the following to get it working
I tried using typing() method but that didn't work.
I read the discord.py docs but found nothing from there that can help.
Searched the internet but again nothing about sending slash commands from a bot
I'd be grateful if someone could help me. Thanks
This is not supported by Discord.
If you want the bot which receives commands from another bot, you need to have control of this bot.
You can try to rewrite the process_commands function in discord.py since dpy automatically ignores the message (triggered by on_message event) sent from other bots.
Discord bot (py) Dont't reply to messages on any servers but reply to direct dms
I build a discord Bot with python using discord.py an as I write the (example $hello), it reply only on private dms not on any servers messages. I olready gived the bot admin on the server but still not doing anything.
My py code is:
intents = discord.Intents.default()
intents.members = True
client = discord.Client(intents=intents)
async def on_message(message):
if message.content.startswith('$hello'):
print(message.content)
await message.channel.send('Hello')
client.run(token)
Also it not write anything to the console if I send the message on a dc server.
There is no firewall rule what can cose the issue I olrady chacked it.
Make sure that the bot intents are toggled in the Developer Portal
If you had it enabled, you can try enabling all the intents and then putting this in the intents parameter
client = discord.Client(intents=discord.Intents().all())
The code will probably look like this
import discord
client = discord.Client(intents=discord.Intents().all())
async def on_message(message):
if message.content.startswith('$hello'):
print(message.content)
await message.channel.send('Hello')
client.run(token)
Just as a quick hint:
according to discords API reference to the "on_message()" function you need to set Intents.messages to True.
In your case this would be
intents.messages = True
I did not test this code, but it's what I read from discords docs.
In the future you might want to have a look at it https://discordpy.readthedocs.io/en/stable/
I hope I could help you with this quick answer :)
So, I've got a bot that can send a user a DM via a very simple command. All I do is "!DM #user message" and it sends them the message. However, people sometimes try responding to the bot in DMs with their questions, but the bot does nothing with that, and I cannot see their replies. Would there be a way for me to make it so that if a user sends a DM to the bot, it will forward their message to a certain channel in my server (Channel ID: 756830076544483339).
If possible, please can someone tell me what code I would have to use to make it forward on all DMs sent to it to my channel 756830076544483339.
Here is my current code:
import discord
from discord.ext import commands
bot = commands.Bot(command_prefix='!')
#bot.event
async def on_ready():
activity = discord.Game(name="DM's being sent", type=3)
await bot.change_presence(
activity=discord.Activity(
type=discord.ActivityType.watching,
name="Pokemon Go Discord Server!"))
print("Bot is ready!")
#bot.command()
async def DM(ctx, user: discord.User, *, message=None):
message = message or "This Message is sent via DM"
await user.send(message)
await ctx.send('DM Sent Succesfully.')
Thank you!
Note: I'm still quite new to discord.py coding, so please could you actually write out the code I would need to add to my script, rather than just telling me bits of it, as I often get confused about that. Thank you!
What you're looking for is the on_message event
#bot.event
async def on_message(message):
# Checking if its a dm channel
if isinstance(message.channel, discord.DMChannel):
# Getting the channel
channel = bot.get_channel(some_id)
await channel.send(f"{message.author} sent:\n```{message.content}```")
# Processing the message so commands will work
await bot.process_commands(message)
Reference
I am currently learning how to make a discord bot using python. But I am stuck at the beginning. My bot is not responding.
It is NOT showing an error. Also in the discord server the bot is showing online.
But when I run guild.member_count it shows the correct number of members. But when I try to get the information of the members by guild.members, it just shows my bot in the list.
Moreover If I try to send message by await member.create_dm() in on_member_join(), it don't send any message.
Also I gave the bot administrator permission to see if its some problem with permissions but still the same.
Below is my code :
import discord
TOKEN = <MyToken> # I have replaced this with my actual token in the actual code
client = discord.Client()
#client.event
async def on_ready():
guild = discord.utils.get(client.guilds, name=GUILD)
print(
f"{client.user} is connected to Discord!\n"
f"Connected to {guild.name} (id: {guild.id}, members-count: {guild.member_count})"
)
members = '\n - '.join([member.name for member in guild.members])
print(f'Guild Members:\n - {members}')
#client.event
async def on_member_join(member):
await member.create_dm()
await member.dm_channel.send(
f"Hello {member.name}, Welcome to the test discord server!"
)
print(f"Welcomed {member.name}.\n")
client.run(TOKEN)
it just shows my bot in the list.
See this page on Gateway Intents
it don't send any message
Try just using
await member.send(...)
I know that it is possible to DM someone with the bot if you have their ID, but how can I get the users ID's when they enter the server?
I was thinking:
#client.event
async def on_member_leave(member):
or
async def on_guild_remove(guild):
user = await client.fetch_user(**the id**)
await user.send("Your message")
There's no need to use fetch_user, you can simply use message.send -
#client.event
async def on_member_leave(member):
await member.send("Your message")
Also, if the member has no server common with the client or if the member has disabled direct messages, The message will not be sent.