Discord.py bot reads message and if it encounters a particular word, copy and dm it to a particular user - discord

I want my bot to read the messages and if it encounters the word "John" in any of the messages, it should copy and dm the message to a specific user. Below is the code i've written but it's not doing anything. Any help would be greatly appreciated.
import discord
from discord import Client
TOKEN = "insert token"
client = discord.Client()
#bot.event
async def on_message(message):
MessageContent = message.content.lower()
if "john" in MessageContent:
user = client.get_user("insert user_id")
await user.send(MessageContent)
bot.run(TOKEN, bot = False)

rename client = discord.Client() to bot = discord.Client()

Related

Discord bot with Interactions.py | on_message not receiving messages from any channel. How to listen for messages?

My bot has fully functioning /slash commands and I would also like them to be text commands and add other functionality when users send messages in chat. However, I cannot seem to get the bot to listen for messages.
import interactions
# Create the bot
Bot = interactions.Client(
token = BotToken,
scope = ServerID
)
#Bot.event
async def on_message(message):
await HandleMessage(message)
return
Bot.start()
The code never enters the on_message method.
I had a working version with the discord.py module and they are very similar. I am not sure why it does not work with Interactions.py.
Working code with discord.py:
intents = discord.Intents.default()
intents.messages = True
intents.message_content = True
client = discord.Client(intents = intents)
#client.event
async def on_message(message):
await HandleMessage(message)
client.run(Token)

Why wont my discord bot recognise commands?

I'm trying to make a discord bot that will provide the current price of crypto currencies when a command (!price) is used, below is my code i have taken out my bots token for obvious reasons
import requests
from discord import Client, Intents
intents = Intents.default()
intents.members = True
client = Client(intents=intents)
#client.event
async def on_ready():
print(f'Logged in as {client.user}')
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content.startswith('!price'):
symbol = message.content[6:].upper()
url = f'https://api.coingecko.com/api/v3/simple/price?ids={symbol}&vs_currencies=usd'
print(f'Fetching price for symbol: {symbol}')
response = requests.get(url)
print(f'API response: {response.json()}')
print(f'response.status_code: {response.status_code}')
try:
price = response.json()[symbol]['usd']
await message.channel.send(f'The price of {symbol} is ${price}')
except KeyError:
print(f'Error: Symbol {symbol} not found')
await message.channel.send(f'Error: Symbol {symbol} not found')
client.run('TOKEN GOES HERE')
i have tried literally everything, Ive added so many debugging tools and I'm fairly certain that the problem lies with my bot picking up my commands in the discord server,

Discord py. How to get if user is online without commands

i need a help with a python code. All I need is to get if user selected (by myself) ID is currently online. How can I do that?
The code for now looks like this, but its dosnt print anserw
import discord
from discord.ext import commands
from discord import Member
TOKEN = "token"
intents = discord.Intents.default()
intents.message_content = True
intents.members = True
client = commands.Bot(command_prefix=".", intents=intents)
#client.event
async def on_ready():
return
#client.event
async def on_message(message, member : discord.Member=None):
if member == MyID:
print("draziv online")
if message.author == client.user:
return
if "hello" in message.content or "hi" in message.content:
return
# await message.channel.send(f'Hello there {message.author.mention}. I hope you have a fantastic day so far. ')
client.run(TOKEN)
I was trying all the methods I found over the internet, but almost all of them are outdated

discord.py changing bot status

I am trying to change my bot status but i keep on getting this error.
TypeError: Client.__init__() missing 1 required keyword-only argument: 'intents
my code
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents)
#client.event
async def on_message(message):
if message.content.startswith('bruh'):
channel = message.channel
await channel.send('brah')
client = discord.Client(activity=discord.Game('Beep Boop'))
i added the intents to my code but i am still getting the same error message. I tried to google but all the answers are from pre discord.py v2.
You redefined the client in line 14 and didn't pass the intents keyword argument. And if you want to change the bot's status, just put it in the first client constructor.
import discord
intents = discord.Intents.default()
intents.message_content = True
client = discord.Client(intents=intents, activity=discord.Game('Beep Boop'))
#client.event
async def on_message(message):
if message.content.startswith('bruh'):
channel = message.channel
await channel.send('brah')
# client = discord.Client(activity=discord.Game('Beep Boop'))

Discord self bot slow

from discord.ext.commands import Bot
from discord.ext import commands
import asyncio
#TOKEN
TOKEN = "token"
client = discord.Client()
b = Bot(command_prefix = "!")
#b.event
async def on_ready():
print("on")
#b.event
async def on_message(message):
if "here" in message.content:
try:
await message.author.dm_channel.send("Message I choose")
except:
print('The DM could not be send due to privacy permissions')
b.run(TOKEN, bot = False)
So I have this self bot in discord. Basically it just checks when a person in a server says "#here" it will auto dm them with the message I choose as quick as possible. I would just like to know how would I make this self bot check if the person said that said #here faster.

Resources