How to print message from another file function from index.js - discord.js

I am making discord bot
I want to call testing.js from index.js to send message and return variables.
index.js
import { Client, Embed, GatewayIntentBits, GuildMember, REST, Routes } from 'discord.js';
import testPing from './commands/testing.js';
testPing('ABC FGH');
testing.js (directory: commands)
function testPing(name){
message.channel.send("Sentence "+name );
}
export default (testPing);
It said : ReferenceError: message is not defined
What I should change with the message?
Thank You
Sentence test ABC FGH

The issue is exactly what you would expect it to be
message is not defined. You are trying to perform a function on a class that doesn't exist.
You first need to handle an event (in this case messageCreate event) in order to access the parameter it passes to you.
I highly suggest you read the discord.js documentation, there you will find out how to do that, more specifically this page.
If however, you want to send a message to some specific static channel you can do this:
client.on("ready", async () => {
( client.channels.cache.get('CHANNEL ID') as TextChannel ).send('INSERT A STRING HERE!')
})
P.S. Don't forget to login with your bots token!

Related

How to get bot to reply to user with a (#)ping

I am trying to get a bot to respond to people with a ping in the message, for example: "#user", but everything I've tried have given me not a function error or undefined error. All the stuff I can find about it is either outdated for discord.js v14 or it is for discord.py
Here is my code:
client.on("messageCreate", (message) => {
if (message.content.startsWith("test")) {
const user = message.author.userId();
message.channel.reply(`Hello <#${user}>`)
}
});
I've also attempted variations of the .userId() part - such as .tag, .user.id, and .username but all of them have come back with some sort of undefined error. I know it says userId is a snowflake on discord.js but I am unsure on how to use that for I am fairly new to javascript and discord.js. Also, please know that I am using Replit to host the bot and have discord.js#14.5.0 installed.
So, there are a couple issues here.
Issue 1
message.author.userId() is not a function. When trying to get a user's ID, you want to know what each property is returning.
message -> returns the Message object, which contains the data for the message.
message.author -> returns the User object, which contains the data for the user profile.
message.member -> returns the Member object, which contains the data for the guild member profile.
And so on.
In this case, you're going to want to get the User object: being message.author. You've already figured this out.
Now, the User Object has it's own set of properties (which you can find in the documentation at https://discord.js.org/).
The property that you are looking for is: .id, or in your use-case, message.author.id.
Your other attempts, message.author.tag returns the tag, message.author.user.id will throw an error, and message.author.username returns the user's username.
Issue 2
Your second issue is the .reply() method that you're using. The Channel Object doesn't have a .reply() method, but the message does.
So, how you would write this code:
client.on("messageCreate", async(message) => {
if(message.content.toLowerCase().startsWith("test")) {
// Note that I added the .toLowerCase() method to ensure you can do tEST and it still works!
message.reply({ content: `Hello, ${message.author}!` });
}
});
Also, a cool feature that discord.js has is that you can supply the User or Member Object to the content of a message, and it will automatically mention the desired person.
Hope this helps!

(Discord selfbot) Is there anyway to response if 'hi' in someone message

Is there anyway to make the selfbot response 'hello' if 'hi' is in someone message?
Like
A: hi? Anyone here?
The bot: hello
The bot response because 'hi' is in A's message.
I think it's not possible because i tried many Discord selfbot library and none of library work (Python)
Which library are you using?
I made a discord bot with discord.js (Javascript), and with that you can receive an event every time someone talks in your discord server and then respond depending on the contents of the message.
First you start your discord client (the Intents may vary depending on what you want to do):
const discordClient = new Client({ intents: [Intents.FLAGS.DIRECT_MESSAGES, Intents.FLAGS.GUILD_MESSAGES] })
Then the idea is to get the content of every new message and answer properly, once you have created your discord client, you will need to set up an event listener like this one:
discordClient.on('messageCreate', message => {
const content = messageReceived.content.toLocaleLowerCase()
if (content === 'hi') {
messageReceived.channel.send("hello, I'm a bot!")
}
})
And don't forget to login with your discord bot key
const discordKey = "YOUR DISCORD BOT KEY"
discordClient.login(discordKey)
You can also see the repository for my bot here.
And for your use case you would want to mainly focus on some parts insid discord.ts.
Hopefully this helps.
Here are some basic examples using discord.js within node.js.
Simple Response
To send a response, simply check if the user's message contains the desired input you want your bot to respond to.
if(message.content.toLowerCase().includes("ping") {
message.reply("Pong!")
}
Multiple Responses
This is a much more efficient method of responding to multiple inputs. This includes using an array.
const array = [
"hello",
"hola",
"bonjour"
]
if(array.some(word => message.content.toLowerCase().includes(`${word}`)) {
message.reply("Hello!")
}
Here's the code
---Starts From Here:---
// type `await lib.` to display API autocomplete
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// Only respond to messages containing "hi", "hey", "hello", or "sup"
if (context.params.event.content.match(/hi|hey|hello|sup/i)) {
let messageContent = context.params.event.content.match(/hi|hey|hello|sup/i);
await lib.discord.channels['#0.3.0'].messages.create({
channel_id: context.params.event.channel_id,
content: `${messageContent[0]} to you too 👋`,
message_reference: {
message_id: context.params.event.id
}
});
}
--Ends--
Image of code
I suggest using autocode.com it will really help you in coding life. I use it myself.
Here's a small guide https://autocode.com/guides/how-to-build-a-discord-bot/
If your banned using discord selfbot, that's not my fault
from discord.ext import commands
client = commands.Bot(
command_prefix='-',
self_bot=True,
help_command=None
)
#client.event
async def on_message(message):
if message.author == client.user:
return
if message.content == 'Hello!':
await message.channel.send("Hi how your day!")
client.run('user token', bot=False)

discord.js say command help meeee

so im trying to put this in a .js file in my replit discord bot and it wont power up, any know how to fix it? this Is how i did my other things but it includes the prefix, and i don't want the prefix to show it, like, when someone says hi i want the bot to say for example hello back to him. enter image description here
client.on("message", async message =>{
if (message.content.startsWith("ce faci"))
{
message.channel.send(uite ma scarpinam la coaie)
}
If you haven't already, go to the Discord Developer Portal (https://discord.com/developers/applications), click "New Application", give it any name you want, go to the left side of the screen and click on "Bot", and press the button "Add Bot", and press "Yes, do it!" to the pop-up. There should be a token that's been generated. Copy it and put it in the code template bellow.
// Import discord.js and create the client
const Discord = require('discord.js')
const client = new Discord.Client();
// Register an event so that when the bot is ready, it will log a messsage to the terminal
client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
})
// Register an event to handle incoming messages
client.on('message', async msg => {
// Check if the message starts with '!hello' and respond with 'world!' if it does.
if(msg.content.startsWith("!hello")) {
msg.reply("world!")
}
})
// client.login logs the bot in and sets it up for use. You'll enter your token here.
client.login('your_token_here');
It might be helpful to use a guide, and you'll find plenty of them on YouTube that teaches you the basics of using Discord.js.
If this is your first time coding in JavaScript or coding in general, I'd suggest that you go on websites such as Codecademy, freeCodeCamp, Khan Academy, YouTube, W3Schools, etc. to learn the basics.

Discord: Reading messages from one channel in a (non-admin perm) server, and posting it to another (with admin perms)

I am new to Discord API framework.
ServerFROM is a public server that I was invited to (non-admin perms). Hence I cannot add bots there. But I can view the content in ChannelFROM (a text channel in ServerFROM)
I have my own ServerTO (in which I have admin perms and so can do anything). Inside of which, I have the target ChannelTO
I want to deploy a listener on ChannelFROM, such when there is a new message (announcement) in ChannelFROM, I want it to be read and reposted in ChannelTO.
Something similar to what is done in this Stackoverflow issue, except that I cannot have some script run locally 24x7 on my machine. Maybe use Github Actions or something similar?
How can I go about doing it? Any ideas are appreciated. Maybe some form of a server, or just a custom Discord bot
And thanks in advance.
you can use Custom bot for that. I dont know other way
here's how i do it
1st : We Get the ID of the Channel that you wanted to listen
2nd : We make a Output or where the bot copy the message from and send it
3rd : Bot required a permission to view the channel and send message
or in the nutshell ( sorry if im bad at explaining )
client.on("messageCreate", message => {
if(message.author.bot) return;
if(message.channel.id === ID_HERE) // ChannelFROM in ID_HERE
{
let MSG = message.content
let Author = message.member.displayName
let Avatar = message.author.displayAvatarURL({dynamic: true})
const Embed = new MessageEmbed()
.setAuthor(Author , Avatar )
.setDescription(MSG)
.setColor("RANDOM")
client.channels.cache.get(ID_HERE).send({ embeds: [Embed] }) // SendTo in ID_HERE
}
})
you can remove if(message.author.bot) return; if you want it also read other bot message on that specific channel

How to get a Discord bot to send a message to a specfic channel when created?

I'm making a bot which on a react to a certain will create a channel...
That all works perfectly expect I want a nessage to be posted when the cahnnel is created which has a specfic beginning.
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
message.channel.send('test');
});
I'm not getting any errors, just nothing...
You can't use the message variable in the channelCreate event. The only thing you're receiving is a channel object, so you need to use channel.send():
client.on('channelCreate', (channel, message) => {
if(channel.name.startsWith('ticket-')){
channel.send('test');
});

Resources