mirror of
https://github.com/Ratstail91/SANCTUM.git
synced 2025-11-29 02:24:27 +11:00
Working on the beginnings of a shared library
This commit is contained in:
56
Shared/messaging.js
Normal file
56
Shared/messaging.js
Normal file
@@ -0,0 +1,56 @@
|
||||
//initialize the exports
|
||||
exports = module.exports = {};
|
||||
|
||||
//SendPublicMessage
|
||||
//client - discord.js client
|
||||
//user (optional) - discord.js user OR username
|
||||
//channel - discord.js channel OR channel name
|
||||
//message - message to send
|
||||
//typingDelay (optional) - delay in milliseconds for the message, while typing is shown
|
||||
exports.SendPublicMessage = function(client, user, channel, message, typingDelay = 0) {
|
||||
//Handle optional second argument (so much for default arugments in node)
|
||||
if (message === undefined) {
|
||||
message = channel;
|
||||
channel = user;
|
||||
userID = null;
|
||||
}
|
||||
|
||||
//handle user strings
|
||||
if (typeof(user) == "string") {
|
||||
user = client.users.find(item => item.username === user);
|
||||
}
|
||||
|
||||
//handle channel strings
|
||||
if (typeof(channel) == "string") {
|
||||
channel = client.channels.find(item => item.name === channel);
|
||||
}
|
||||
|
||||
//Utility trick: @user
|
||||
if (user !== null) {
|
||||
message = "<@" + user.id + "> " + message;
|
||||
}
|
||||
|
||||
//Sends message
|
||||
if (typingDelay !== 0) {
|
||||
channel.startTyping();
|
||||
setTimeout(() => {
|
||||
channel.send(message);
|
||||
channel.stopTyping(true);
|
||||
}, typingDelay);
|
||||
} else {
|
||||
channel.send(message);
|
||||
}
|
||||
}
|
||||
|
||||
//SendPrivateMessage
|
||||
//client - discord.js client
|
||||
//user - discord.js user OR username
|
||||
//message - message to send
|
||||
exports.SendPrivateMessage = function(client, user, message) {
|
||||
//handle user strings
|
||||
if (typeof(user) == "string") {
|
||||
user = client.users.find(item => item.username === user);
|
||||
}
|
||||
|
||||
user.send(message);
|
||||
}
|
||||
Reference in New Issue
Block a user