Skip to content

Instantly share code, notes, and snippets.

@nirewen
Last active June 7, 2022 02:10
Show Gist options
  • Save nirewen/507b4ff8ad93d138068a6e514849dda9 to your computer and use it in GitHub Desktop.
Save nirewen/507b4ff8ad93d138068a6e514849dda9 to your computer and use it in GitHub Desktop.
A list of helpful functions for my Eris bot
let Endpoints = require("eris/lib/rest/Endpoints");
Object.defineProperties(Eris.Client.prototype, {
fetchChannel: {
value: function(channelID) {
return this.requestHandler.request("GET", Endpoints.CHANNEL(channelID), true).then((channel) => {
if (channel.type === 0) {
return new Eris.TextChannel(channel, null, this.options.messageLimit);
} else if (channel.type === 1) {
return new Eris.PrivateChannel(channel, this);
} else if (channel.type === 2) {
return new Eris.VoiceChannel(channel, null);
} else if (channel.type === 3) {
return new Eris.GroupChannel(channel, this);
} else if (channel.type === 4) {
return new Eris.CategoryChannel(channel, null);
} else {
return channel;
}
});
}
},
fetchGuild: {
value: function(guildID) {
return this.requestHandler.request("GET", Endpoints.GUILD(guildID), true).then((guild) => new Eris.Guild(guild, this));
}
},
fetchGuilds: {
value: function(limit, before, after) {
return this.requestHandler.request("GET", Endpoints.USER_GUILDS("@me"), true, {
limit,
before,
after
}).then((guilds) => guilds.map((guild) => new Eris.Guild(guild, this)));
}
},
fetchGuildChannels: {
value: function(guildID) {
return this.requestHandler.request("GET", Endpoints.GUILD_CHANNELS(guildID), true).then((channels) => channels.map((channel) => {
if (channel.type === 0) {
return new Eris.TextChannel(channel, null, this.options.messageLimit);
} else if (channel.type === 2) {
return new Eris.VoiceChannel(channel, null);
} else if (channel.type === 4) {
return new Eris.CategoryChannel(channel, null);
} else {
return channel;
}
}));
}
},
fetchGuildEmojis: {
value: function(guildID) {
return this.requestHandler.request("GET", Endpoints.GUILD_EMOJIS(guildID), true);
}
},
fetchGuildEmoji: {
value: function(guildID, emojiID) {
return this.requestHandler.request("GET", Endpoints.GUILD_EMOJI(guildID, emojiID), true);
}
},
fetchGuildMembers: {
value: function(guildID, limit, after) {
return this.requestHandler.request("GET", Endpoints.GUILD_MEMBERS(guildID), true, {
limit,
after
}).then((members) => members.map((member) => new Eris.Member(member, null)));
}
},
fetchGuildMember: {
value: function(guildID, memberID) {
return this.requestHandler.request("GET", Endpoints.GUILD_MEMBER(guildID, memberID), true).then((member) => new Eris.Member(member, null));
}
},
fetchGuildRoles: {
value: function(guildID) {
return this.requestHandler.request("GET", Endpoints.GUILD_ROLES(guildID), true).then((roles) => roles.map((role) => new Eris.Role(role, null)));
}
},
fetchUser: {
value: function(userID) {
return this.requestHandler.request("GET", Endpoints.USER(userID), true).then((user) => new Eris.User(user, this));
}
}
});
Object.defineProperties(Eris.Role.prototype, {
members: {
get: function members() {
let col = new Eris.Collection(Eris.Member);
this.guild.members.forEach(m => {
if (m.roles.includes(this.id))
col.add(m, this);
});
return col;
}
}
});
Object.defineProperties(Eris.User.prototype, {
dm: {
value: function(content, file) {
return this.getDMChannel(this.id).then(dm => dm.createMessage(content, file));
}
},
tag: {
get: function tag() {
return this.username + '#' + this.discriminator;
}
}
});
Object.defineProperties(Eris.Guild.prototype, {
me: {
get: function me() {
return this.members.get(bot.user.id);
}
},
sortedRoles: {
get: function sortedRoles() {
return this.roles.map(r => r).sort((a, b) => b.position - a.position);
}
}
});
Object.defineProperties(Eris.Member.prototype, {
sortedRoles: {
get: function sortedRoles() {
return this.roles.map(r => this.guild.roles.get(r)).sort((a, b) => b.position - a.position);
}
},
highestRole: {
get: function highestRole() {
return this.sortedRoles.length > 0 ? this.roles[0] : null;
}
},
highestColoredRole: {
get: function highestColoredRole() {
return this.sortedRoles.length > 0 ? this.sortedRoles.find(r => r.color !== 0) : null;
}
},
displayColor: {
get: function displayColor() {
return this.highestColoredRole ? this.highestColoredRole.color : 16777215;
}
},
displayHexColor: {
get: function displayHexColor() {
return '#' + this.displayColor.toString(16).padStart(6, '0');
}
},
displayName: {
get: function displayName() {
return this.nick || this.username;
}
}
});
Object.defineProperties(Array.prototype, {
random: {
value: function () {
return this[Math.floor(this.length * Math.random())];
},
},
truncate: {
value: function(length, more = '...') {
let arr = this;
if (this.length > length) {
arr = this.slice(0, length);
arr.push(more.replace(/%(REMAIN|REMAINING)%/gi, this.length - arr.length));
}
return arr;
},
},
});
Object.defineProperties(String.prototype, {
escape: {
value: function() {
let regex = /([\u{1f300}-\u{1f5ff}\u{1f900}-\u{1f9ff}\u{1f600}-\u{1f64f}\u{1f680}-\u{1f6ff}\u{2600}-\u{26ff}\u{2700}-\u{27bf}\u{1f1e6}-\u{1f1ff}\u{1f191}-\u{1f251}\u{1f004}\u{1f0cf}\u{1f170}-\u{1f171}\u{1f17e}-\u{1f17f}\u{1f18e}\u{3030}\u{2b50}\u{2b55}\u{2934}-\u{2935}\u{2b05}-\u{2b07}\u{2b1b}-\u{2b1c}\u{3297}\u{3299}\u{303d}\u{00a9}\u{00ae}\u{2122}\u{23f3}\u{24c2}\u{23e9}-\u{23ef}\u{25b6}\u{23f8}-\u{23fa}])/ug,
temp = this;
if (temp.match(regex)) {
temp = temp.replace(regex, '\\$1');
}
return temp.replace(/(\*|_|~|`)/g, "\\$1");
},
},
truncate: {
value: function(length) {
return this.length > length ? this.slice(0, length) + '...' : this;
},
},
toSnakeCase: {
value: function() {
return this.replace(/\s/g, "_");
},
},
toCamelCase: {
value: function() {
return this
.replace(/\s(.)/g, function($1) { return $1.toUpperCase(); })
.replace(/\s/g, '')
.replace(/^(.)/, function($1) { return $1.toLowerCase(); });
},
},
});
Eris.TextChannel.prototype.send = function(content, file) {
return this.createMessage(content, file);
};
Number.range = function(start, stop, step = 1) {
if (!stop) {
stop = start;
start = 0;
}
if ((step > 0 && start >= stop) || (step < 0 && start <= stop))
return [];
let result = [];
for (var i = start; step > 0 ? i < stop : i > stop; i += step)
result.push(i);
return result;
};
@nirewen
Copy link
Author

nirewen commented Mar 21, 2020

@TMUniversal when I was using Eris I made this snippet to include some features d.js had. I just simply put them in a function and called it upon the bot's start. Later I rewrote my code using d.js so I never updated this. It still has some cool features like Array.truncate() and String.truncate() to trim the string and arrays to a limit and add a "..." at the end. And also Number.range() which is useful to create an Array with a starting number to an end number with steps.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment