세련된칡덩굴
세련된칡덩굴
DIAdiscord.js - Imagine an app
Created by 세련된칡덩굴 on 7/30/2023 in #djs-questions
sending messages code problem
I wrote some codes that sends a image 'sendthis.jpg' to the chat if there's no messages for 45 minutes.
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAG.GUILD, Intents.FLAGS.GUILD_MESSAGES] });

let lastMessageTimestamp = null;
const channelID = '1019570335818981427';
const imagePath = 'src/sendthis.jpg';
const timeLimit = 45 * 60 * 1000;

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(channelID);
if (!channel) {
console.error(`Channel with ID ${channelID} not found`);
return;
}
channel.messages.fetch({ limit: 1 }).then(messages => {
const lastMessage = messages.first();
if (lastMessage) {
lastMessageTimestamp = lastMessage.createdTimestamp;
}
});
});

client.on('messageCreate', message => {
if (message.channel.id === channelID) {
lastMessageTimestamp = message.createdTimestamp;
}
});

setInterval(() => {
if (lastMessageTimestamp && Date.now() - lastMessageTimestamp > timeLimit) {
const channel = client.channels.cache.get(channelID);
if (channel) {
const file = new Discord.MessageAttachment(imagePath);
channel.send({ files: [file] });
lastMessageTimestamp = null;
}
}
}, 1000);

client.login(process.env.TOKEN);
require('dotenv').config();
const { Client, Intents } = require('discord.js');
const fs = require('fs');

const client = new Client({ intents: [Intents.FLAG.GUILD, Intents.FLAGS.GUILD_MESSAGES] });

let lastMessageTimestamp = null;
const channelID = '1019570335818981427';
const imagePath = 'src/sendthis.jpg';
const timeLimit = 45 * 60 * 1000;

client.on('ready', () => {
console.log(`Logged in as ${client.user.tag}!`);
const channel = client.channels.cache.get(channelID);
if (!channel) {
console.error(`Channel with ID ${channelID} not found`);
return;
}
channel.messages.fetch({ limit: 1 }).then(messages => {
const lastMessage = messages.first();
if (lastMessage) {
lastMessageTimestamp = lastMessage.createdTimestamp;
}
});
});

client.on('messageCreate', message => {
if (message.channel.id === channelID) {
lastMessageTimestamp = message.createdTimestamp;
}
});

setInterval(() => {
if (lastMessageTimestamp && Date.now() - lastMessageTimestamp > timeLimit) {
const channel = client.channels.cache.get(channelID);
if (channel) {
const file = new Discord.MessageAttachment(imagePath);
channel.send({ files: [file] });
lastMessageTimestamp = null;
}
}
}, 1000);

client.login(process.env.TOKEN);
And when I run this, terminal says 'TypeError: Cannot read properties of undefined (reading 'FLAG') how can I fix it?
7 replies
DIAdiscord.js - Imagine an app
Created by 세련된칡덩굴 on 7/29/2023 in #djs-questions
error 405 method not allowed
these are my codes ( and the file name is 'register-commands.js' )
require('dotenv').config();
const { REST, Routes } = require('discord.js')

const commands = [
{
name: 'rps',
description: '가위바위보 한 판 뜨자'
},
];

const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);

(async () => {
try {
console.log('registering slash command...')
await rest.put(
Routes.applicationGuildCommand(process.env.CLIENT_ID, process.env.GUILD_ID),
{ body: commands }
)

console.log('slash command were registerd sucessfully')
} catch (error) {
console.log(`There was an error, ${error}`);
}
})();
require('dotenv').config();
const { REST, Routes } = require('discord.js')

const commands = [
{
name: 'rps',
description: '가위바위보 한 판 뜨자'
},
];

const rest = new REST({ version: '10' }).setToken(process.env.TOKEN);

(async () => {
try {
console.log('registering slash command...')
await rest.put(
Routes.applicationGuildCommand(process.env.CLIENT_ID, process.env.GUILD_ID),
{ body: commands }
)

console.log('slash command were registerd sucessfully')
} catch (error) {
console.log(`There was an error, ${error}`);
}
})();
so I typed 'node src/register-commands.js' in the terminal, and it says, 'Debugger attached. registering slash command... There was an error, DiscordAPIError[0]: 405: Method Not Allowed Waiting for the debugger to disconnect...' How can I fix it to work?
3 replies