Skip to content

Commit

Permalink
Embed constructors and embed messsages.
Browse files Browse the repository at this point in the history
  • Loading branch information
wtflmao committed Jan 9, 2023
1 parent f306c47 commit 642a555
Show file tree
Hide file tree
Showing 6 changed files with 171 additions and 1 deletion.
Binary file added assets/dogeCoffee.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/githubIcon.jpg
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
2 changes: 1 addition & 1 deletion cmdPaths.js
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
module.exports = {
data: ["./commands", "./commands/utils", "./commands/buttons", "./commands/menus", "./commands/modals", "./commands/contextMenus"],
data: ["./commands", "./commands/utils", "./commands/buttons", "./commands/menus", "./commands/modals", "./commands/contextMenus", "./commands/embeds"],
};
69 changes: 69 additions & 0 deletions commands/embeds/embed.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,69 @@
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('embed')
.setDescription('Replies with embed!'),
async execute(interaction) {
// This EmbedBuilder() has to be inside a command or listener
const exampleEmbed = new EmbedBuilder()
.setColor(0xC1F8C0)
.setTitle('标题标题')
.setURL('https://discord.js.org/')
.setAuthor({ name: '作者名字', iconURL: 'https://i.imgur.com/AfFp7pu.png', url: 'https://discord.js.org' })
.setDescription('这里给出一些介绍')
.setThumbnail('https://i.imgur.com/fwO0TqB.jpeg')
.addFields(
{ name: '普通域标题', value: 'Some value here1' },
{ name: '普通域标题', value: 'Some value here11,下面那个是空白域' },
// add a blank field in the embed
{ name: '\u200B', value: '\u200B' },
{ name: '内联域标题1', value: 'Some value here111', inline: true },
{ name: '内联域标题2', value: 'Some value here3', inline: true },
)
.addFields({ name: '内联域标题3', value: 'Some value here4', inline: true })
.setImage('https://i.imgur.com/yA1u8wt.jpeg')
// .setTimestamp() accept a Number, a null or a Date value
.setTimestamp()
.setFooter({ text: '页脚文字', iconURL: 'https://i.imgur.com/AfFp7pu.png' });
await interaction.reply({ content: "Here's an embed example", embeds: [exampleEmbed] });

// directly use a JSON object to represent an embed
const embed2json = {
color: 0xC1F8C0,
title: '标题标题',
url: 'https://discord.js.org/',
author: {
name: '作者名字',
url: 'https://discord.js.org',
icon_url: 'https://i.imgur.com/AfFp7pu.png'
},
description: '这里给出一些介绍',
thumbnail: { url: 'https://i.imgur.com/fwO0TqB.jpeg' },
fields: [
{ name: '普通域标题', value: 'Some value here1' },
{ name: '普通域标题', value: 'Some value here11,下面那个是空白域' },
{ name: '​', value: '​' },
{ name: '内联域标题1', value: 'Some value here111', inline: true },
{ name: '内联域标题2', value: 'Some value here3', inline: true },
{ name: '内联域标题3', value: 'Some value here4', inline: true }
],
image: { url: 'https://i.imgur.com/yA1u8wt.jpeg' },
// here timestamp field only accepts ISO8601 number or string
timestamp: (new Date()).toISOString(),
footer: { text: '页脚文字', icon_url: 'https://i.imgur.com/AfFp7pu.png' }
};
await interaction.channel.send({ embeds: [embed2json] });

const simpleEmbed = new EmbedBuilder()
.setTitle('Columbus, Ohio: Weather report')
.setURL('https://weather.com/weather/today/l/8db513f8f8993797550b32062dfa5d4d83dd97b28ad0b55ec0033a14fe58a86dc0163883d8c5bf8f66aa6173005f3ebc')
.setThumbnail('https://i.imgur.com/sRwMnNA.jpeg')
.setDescription('31 °F\nMostly Cloudy')
.setFields(
{ name:'High', value: '41 °F', inline: true},
{ name:'Low', value: '28 °F', inline: true},
);
await interaction.channel.send({ content: "Here's a simple embed", embeds: [simpleEmbed] });
},
};
64 changes: 64 additions & 0 deletions commands/embeds/embedEdit.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
const { EmbedBuilder, SlashCommandBuilder } = require('discord.js');
const wait = require('node:timers/promises').setTimeout;

module.exports = {
data: new SlashCommandBuilder()
.setName('embededit')
.setDescription('Replies with embed!'),
async execute(interaction) {
const infoLogoUrl = "https://encrypted-tbn0.gstatic.com/images?q=tbn:ANd9GcSOsaJUWS9y-_JQWtIggodoouMyxM-lJyPa1Q&usqp=CAU";

// This EmbedBuilder() has to be inside a command or listener
const embed = new EmbedBuilder()
.setTitle('Resource Usage Panel')
.setColor(0x955F20)
.setThumbnail('https://i.imgur.com/rVtny54.jpeg')
.setDescription('\u200B')
.addFields(
{ name: 'CPU Usage', value: '14 %', inline: true },
{ name: 'Memory Usage', value: '70 %', inline: true },
)
.setTimestamp()
.setFooter({ text: 'Updated 0 second ago', iconURL: infoLogoUrl })

await interaction.reply({ embeds: [embed], content: `${(new Date()).toISOString()}` });


// generate a random number between 2 - 9
let time = 2 + Math.floor(8 * Math.random());
await wait(time * 1000);

// fetch the reply we just replied
const message = await interaction.fetchReply();

// build an embed from the reply we just replied
const receivedEmbed = message.embeds[0];
const newEmbed = EmbedBuilder.from(receivedEmbed)
.setFields(
{ name: 'CPU Usage', value: `${Math.floor(100 * Math.random())} %`, inline: true },
{ name: 'Memory Usage', value: `${Math.floor(100 * Math.random())} %`, inline: true },
)
.setFooter({ text: `Updated ${time} seconds ago`, iconURL: infoLogoUrl });

await message.edit({ embeds: [newEmbed], content: `${(new Date()).toISOString()}` })
//await interaction.editReply();


// generate a random number between 2 - 9
time = 2 + Math.floor(8 * Math.random());
await wait(time * 1000);

// send a new one
const newEmbed2 = EmbedBuilder.from(newEmbed)
.setDescription("Connection lost")
.setFields(
{ name: 'CPU Usage', value: `N/A`, inline: true },
{ name: 'Memory Usage', value: `N/A`, inline: true },
)
.setFooter({ text: `Updated ${time} seconds ago`, iconURL: infoLogoUrl });

// just delete the reply message and instantly send a new one into the same channel
await interaction.deleteReply();
await interaction.channel.send({ embeds: [newEmbed2], content: `${(new Date()).toISOString()}` });
},
};
37 changes: 37 additions & 0 deletions commands/embeds/embedWithAttachment.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
const { AttachmentBuilder, EmbedBuilder, SlashCommandBuilder } = require('discord.js');

module.exports = {
data: new SlashCommandBuilder()
.setName('embedattachment')
.setDescription('Replies with embed!'),
async execute(interaction) {
// the "./" directory is the "discord_bot_example"
const dogeCoffee = new AttachmentBuilder('assets/dogeCoffee.jpg');
const githubIcon = new AttachmentBuilder('assets/githubIcon.jpg');

// This EmbedBuilder() has to be inside a command or listener
const embed = new EmbedBuilder()
.setTitle('embed with image attachments')
.setThumbnail('attachment://githubIcon.jpg')
.setDescription('Github')
await interaction.reply({ embeds: [embed], files: [githubIcon]});

// directly use a JSON object to represent an embed
const embed2json = {
title: 'A cup of coffee',
image: {
url: 'attachment://dogeCoffee.jpg',
},
};
const embed2 = new EmbedBuilder(embed2json);
await interaction.channel.send({ embeds: [embed2], files: [dogeCoffee]});

// mix up two existing constant embeds
const embed3 = new EmbedBuilder()
.setTitle(embed.toJSON().title + embed2json.title)
.setImage(embed2.toJSON().image.url)
.setThumbnail(embed.toJSON().thumbnail.url)
.setDescription("Ahhh");
await interaction.channel.send({ embeds: [embed3], files: [dogeCoffee, githubIcon]});
},
};

0 comments on commit 642a555

Please sign in to comment.