This command lets you use /namehistory [username] to view a player's name history in a Discord embed.
const lib = require('lib')({token: process.env.STDLIB_SECRET_TOKEN});
// This snippet lets you use /namehistory [username] to look up a player's name history
// ▶️ This snippet uses one request every time someone uses /namehistory
// ⚠️ TO USE THIS SNIPPET, YOU MUST FOLLOW THE FOLLOWING INSTRUCTIONS FIRST ⚠️
// 1. In a new tab, go to https://omg.ac/command
// 2. Link the Discord bot you want to install the command to
// 3. Create a new command with the following options
// Command name: namehistory
// Description: Looks up a player's name history by username or UUID
// One option, with the following settings:
// Type: String
// Option name: username
// Description: The player's current username or UUID
// 4. Click the save button!
// Your command should look like this screenshot once setup: https://i.imgur.com/WDyhOxu.png
// Extract the player's username from the command
let username = context.params.event.data.options[0].value;
// Send a response straight away, so Discord doesn't timeout if the API is slow
await lib.discord.interactions['@1.0.1'].responses.ephemeral.create({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `⏳ Looking up name history, please wait...`,
description: ``,
color: 0x4b6584,
},
],
});
let nameHistory; // we initialise this variable here so we can access it outside the try/catch block
// Use the API to lookup this player's name history
try {
nameHistory = await lib.minecraft.players['@0.0.1'].history({
identity: username,
});
} catch (e) {
// If there's an error (probably because the username doesn't exist), show it in Discord
return await lib.discord.interactions['@1.0.1'].responses.update({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `❌ Failed to lookup name history`,
description: `The username provided doesn't exist.`,
color: 0xeb3b5a,
},
],
});
}
// Look through each previous name, and add it to an array
// We'll render this array inside an embed in Discord
let fields = [];
for (const name of nameHistory.data.history) {
fields.push({
name: name.username,
value:
name.changed_at == null
? 'Current Name'
: `Changed at ${name.changed_at}`,
});
}
// Once we've fetched the player's name history, send it back to Discord
// As this is an ephemeral response, only the person who used the command can see it
await lib.discord.interactions['@1.0.1'].responses.update({
token: context.params.event.token,
response_type: 'CHANNEL_MESSAGE_WITH_SOURCE',
content: '',
tts: false,
embeds: [
{
type: 'rich',
title: `⛏️ Minecraft: Java Edition - Name History`,
description: ``,
color: 0x3867d6,
fields: fields,
},
],
});