In order to use the web API you’ll have to authenticate yourself. First, you’ll need to go to github.com/settings/tokens and create a classic token for your account. Set the expiration date and make sure the “repo” checkbox is selected (this grants the API access to your repository). Click on generate token — and don’t share it anywhere. Copy it because you won’t see it again.
You’ll also need to create a Discord app by going to discord.com/developers. Then click on “bot” in the sidebar of the app and fill out the information. I don’t have much experience with Discord bots but I assume the permission to read messages is granted automatically. If it isn’t, then give it the appropriate permissions at the bottom of the page. Copy the token because you won’t see it again.
I’m going to assume you’re using discord.js with Node since that’s what you referenced earlier. If you’re using Python, you’ll have to modify it accordingly.
To do this, when a message is sent, the bot will grab the content and use the Github API to send a POST request to your repository. Your file will then contain the most recent message from the channel, and you’ll be able to access it using the raw.githubusercontent.com/username/repo/branch/path/to/message.json endpoint.
Again, I don’t have much experience with Discord, but here’s an example program. You’ll have to test it and modify to fit your needs:
const Discord = require('discord.js');
const fetch = require('node-fetch');
const fs = require('fs');
const client = new Discord.Client({
intents: [
Discord.Intents.FLAGS.GUILDS,
Discord.Intents.FLAGS.GUILD_MESSAGES,
],
});
const channelID = '12345';
const githubToken = 'ghp_123aBc456';
const githubRepoOwner = 'username';
const githubRepoName = 'repo';
const filePathInRepo = 'path/to/message.json';
const discordBotToken = 'aBc123xYz';
client.on('ready', () => {
console.log(`logged in as: ${client.user.tag}`);
});
client.on('messageCreate', async (message) => {
if (message.channel.id === channelID) {
const messageContent = message.content;
try {
const response = await fetch(
`https://api.github.com/repos/${githubRepoOwner}/${githubRepoName}/contents/${filePathInRepo}`,
{
method: 'GET',
headers: {
Authorization: `Bearer ${githubToken}`,
},
}
);
if (response.status === 200) {
const fileInfo = await response.json();
const fileContent = JSON.parse(Buffer.from(fileInfo.content, 'base64').toString());
fileContent.message = messageContent;
const updatedContent = JSON.stringify(fileContent, null, 2);
const updateResponse = await fetch(
`https://api.github.com/repos/${githubRepoOwner}/${githubRepoName}/contents/${filePathInRepo}`,
{
method: 'PUT',
headers: {
Authorization: `Bearer ${githubToken}`,
'Content-Type': 'application/json',
},
body: JSON.stringify({
message: `New message`,
content: Buffer.from(updatedContent).toString('base64'),
sha: fileInfo.sha,
}),
}
);
if (updateResponse.status === 200) {
console.log(`successfully updated message: "${messageContent}"`);
} else {
console.error('failed to update');
}
} else {
console.error('failed to fetch');
}
} catch (error) {
console.error('other error:', error);
}
}
});
client.login(discordBotToken);
I’ve only tested the Github API endpoints in the browser, and I haven’t tested the Discord bot at all. Feel free to reply if you get any errors and I can try to help you out.
Also, since you’re overwriting the file every message, you have to make at least 2 requests (one to delete the previous message, and one to add the new message). This means your request ratelimit will be cut in half (down to 2500/hour).