Discord bot not wroking - python

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(...)

Related

Discord bot not responding unless I ping the user within the message

Im making a discord bot in python so that it can auto mod, and it works, but only if I ping the bot
For example
Saying [insert profanity here] wouldn't auto delete the message
But, saying [insert profanity here + #bot] would delete the message
How can I get it to work without any pings?
Source Code:
`import discord
token = "[insert discord token here]"
block_words = ["(List of profanity here)"]
intents = discord.Intents.default()
client = discord.Client(intents=intents)
#client.event
async def on_ready():
print(f"Bot logged in as {client.user}")
#client.event
async def on_message(msg):
if msg.author != client.user:
for text in block_words:
if str(msg.content.lower()):
await msg.delete()
return
print("not deleting")
client.run(token)
I wasn't able to think of any ideas for what to do`

Discord Bot isn't responding to command

My discord bot is not responding to "test". There are no error messages, so I am very confused why this is happening.
I just started Python today and I wanted to learn how to make a Discord bot
import discord
client = discord.Client(intents = discord.Intents.default())
#client.event
async def on_ready():
general_channel = client.get_channel(1045161050024185899)
await general_channel.send('Bot Activated By-**GotYoHat** \nMade By **GotYoHat** using **Python**')
#client.event
async def on_message(message):
if message.content == 'test':
await message.channel.send('work')
client.run('the token is right here')
I searched the internet for an answer and I can't find a single answer online
You need to turn on intents.
Try to discord.Intents.all()

Why is my Discord bot refusing to respond to commands now when it responded before?

My bot suddenly stopped responding to commands. I believe it's on Discord's end as any time I attempt to invite the bot, permissions only say "This will allow the developer to: Create commands in your server." I have a second bot that I haven't touched since the week it was made and it is having no problems. Am I doing something wrong in the developer portal? It's a super basic bot that sends text at the moment.
import discord
import os
from keep_alive import keep_alive
client = discord.Client()
#client.event
async def on_ready():
print('We have logged in as {0.user}'.format(client))
#client.event
async def on_message(message):
if message.author == client.user:
return
message.content
if "p.h" in message.content:
await message.channel.send('Hello!')
keep_alive()
client.run(os.getenv('TOKEN'))
Go discord developer portal > select your app > navigate to "bot" in sidebar
Enable message content intent, to allow the bot read message sent from the user

The bot does not send a message to the dm

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

How to make my bot forward DMs sent to it to a channel

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

Categories

Resources