Update Examples (closes #380) (#383)

* Update examples

* Forgot to save send-files
This commit is contained in:
Manuel Kraus
2016-05-29 03:55:39 +02:00
committed by abalabahaha
parent b9506d01ab
commit 8d64435def
9 changed files with 114 additions and 176 deletions

View File

@@ -1,3 +0,0 @@
# Warning!
### These examples are not updated to v5.0.0, may not work!

View File

@@ -9,10 +9,6 @@ var loose = false;
bot.on("message", msg => {
if (~msg.content.indexOf("https://discord.gg/")) {
bot.joinServer(msg.content.substr(msg.content.indexOf("https://discord.gg/"), "https://discord.gg/".length + 16));
}
if (!msg.content.startsWith("$")) return;
msg.content = msg.content.substr(1);
@@ -23,40 +19,40 @@ bot.on("message", msg => {
`${bot.servers.length} servers`,
`${bot.channels.length} channels`,
`${bot.users.length} users`,
].join("\n"));
]);
}
if (msg.content.startsWith("startplaying")) {
else if (msg.content.startsWith("startplaying")) {
var game = msg.content.split(" ").slice(1).join(" ");
bot.setPlayingGame(game);
}
if (msg.content.startsWith("setname") && loose) {
else if (msg.content.startsWith("setname") && loose) {
bot.setUsername(msg.content.split(" ").slice(1).join(" ")).then(() => {
msg.reply("done!");
msg.reply("Done!");
});
}
if (msg.content === "updateav") {
else if (msg.content === "updateav") {
request
.get("https://api.github.com/search/repositories?q=discord.js")
.end((err, res) => {
if (err) {
return;
}
var text = res.body.items[0].stargazers_count
bot.updateDetails({
username : "d.js star bot - " + text,
avatar: getStars(text)
}).then(() => {
msg.reply("success!");
msg.reply("Success!");
});
});
}
if (msg.content.startsWith("setavatar") && loose) {
else if (msg.content.startsWith("setavatar") && loose) {
request
.get(msg.content.split(" ")[1])
.end((err, res) => {
@@ -70,23 +66,23 @@ bot.on("message", msg => {
});
}
if (msg.content === "away") {
else if (msg.content === "away") {
bot.setStatusIdle();
}
if (msg.content === "here") {
else if (msg.content === "here") {
bot.setStatusOnline();
}
if (msg.content === "randomUser") {
else if (msg.content === "randomUser") {
var random = bot.users.random();
msg.reply([
random.username,
"avatar: ", random.avatarURL
].join("\n"));
]);
}
if (msg.content.startsWith("mimic") && loose) {
else if (msg.content.startsWith("mimic") && loose) {
var toMimic = msg.mentions[0];
if (!toMimic) {
@@ -109,7 +105,7 @@ bot.on("message", msg => {
avatar: res.body
}).then(() => {
msg.reply("done!");
msg.reply("Done!");
});
});
@@ -126,9 +122,9 @@ setInterval(() => {
if (err) {
return;
}
var text = res.body.items[0].stargazers_count
bot.updateDetails({
username : "d.js star bot - " + text,
avatar: getStars(text)
@@ -144,7 +140,7 @@ bot.on("disconnected", () => {
process.exit();
})
bot.login(auth.email, auth.password);
bot.loginWithToken(auth.token);
function getStars(text) {
var Canvas = require('canvas')
@@ -154,7 +150,7 @@ function getStars(text) {
ctx.fillStyle = "black";
ctx.fillRect(0, 0, 90, 90);
ctx.font = '45px Arial';
ctx.fillStyle = "white";
ctx.fillText(text, (ctx.measureText(text).width / 2) - 5, 60);

View File

@@ -1,4 +1,3 @@
{
"email" : "your discord email here",
"password" : "your discord password here"
"token": "Your bot token here"
}

View File

@@ -9,23 +9,23 @@ var AuthDetails = require("./auth.json");
var bot = new Discord.Client();
bot.on("ready", () => {
console.log("Ready to begin! Serving in " + bot.channels.length + " channels");
console.log(`Ready to begin! Serving in ${bot.channels.length} channels`);
});
bot.on("disconnected", () => {
console.log("Disconnected!");
process.exit(1); //exit node.js with an error
});
bot.on("message", (msg) => {
if( msg.content === "avatar" ){
// if the message was avatar
bot.reply( msg, msg.sender.avatarURL );
// if the message is avatar
if (msg.content === "avatar") {
bot.reply(msg, "Here is the URL for your avatar: " + msg.author.avatarURL);
}
});
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);

View File

@@ -1,87 +1,72 @@
/*
this bot is a permissions bot and is currently working
with the experimental additions. Some functions may
change in the future.
*/
var Discord = require("../../");
// Get the email and password
// Get the token
var AuthDetails = require("../auth.json");
var bot = new Discord.Client();
bot.on("ready", function () {
console.log("Ready to begin! Serving in " + bot.channels.length + " channels");
console.log(`Ready to begin! Serving in ${bot.channels.length} channels`);
});
bot.on("disconnected", function () {
console.log("Disconnected!");
process.exit(1); //exit node.js with an error
});
bot.on("message", function (msg) {
if (msg.content === "skype") {
//stop the user from speaking in the channel:
bot.overwritePermissions(msg.channel, msg.sender, {
bot.overwritePermissions(msg.channel, msg.author, {
sendMessages: false
});
// send a barely funny message ;)
bot.reply(msg, "how dare you mention that!");
bot.reply(msg, "How dare you mention that!");
}
if (msg.content === "discord") {
var role = msg.server.roles.get("name", "good people");
// if the role doesn't exist, make it
bot.createRoleIfNotExists(msg.channel.server, {
name: "good people",
color: Discord.Colors.BLUE, // colour of blue
hoist: true // make a seperate category in the users list
}).then(addUserToList).catch(console.log);
function addUserToList(role, alreadyExists) {
console.log(arguments);
bot.addMemberToRole(msg.sender, role);
bot.reply(msg, "welcome to the good people! " + alreadyExists);
if (!role) {
bot.createRole(msg.server, {
name: "good people",
color: "0000FF", // blue
hoist: true // make a seperate category in the users list
}).then(createdRole => {
role = createdRole;
}).catch(console.log);
}
bot.addMemberToRole(msg.author, role);
bot.reply(msg, "Welcome to the good people!");
}
if (msg.content === "remove me") {
// remove the user from the good people list, if it exists
var found = false;
var role = msg.server.roles.get("name", "good people");
for (var role of msg.channel.server.roles) {
if (role.name === "good people") {
found = role;
break;
}
}
if (role) { // if the role exists
if (found) {
// if the role exists
if (msg.sender.hasRole(role)) {
if (msg.author.hasRole(role)) {
// remove the member from the role
bot.removeMemberFromRole(msg.sender, role);
bot.reply(msg, "removed!")
bot.removeMemberFromRole(msg.author, role);
bot.reply(msg, "Removed!")
} else {
bot.reply(msg, "you're not in the role!");
bot.reply(msg, "You're not in the role!");
}
} else {
// role doesn't exist
bot.reply(msg, "the role doesn't even exist!");
bot.reply(msg, "The role doesn't even exist!");
}
}
});
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);

View File

@@ -18,58 +18,30 @@ bot.on("disconnected", function () {
console.log("Disconnected!");
process.exit(1); //exit node.js with an error
});
bot.on("message", function (msg) {
// to use this example, you first have to send 'create role'
// you can then change colors afterwards.
if (msg.content === "create role") {
// create the role and add the user to it
bot.createRoleIfNotExists(msg.channel.server, {
bot.createRole(msg.server, {
name: "Custom Colors",
hoist: true, // so it is visible in the members list
}).then(function (permission) {
// this is executed when the role has been created or exists
// adds the sender to the role
bot.addMemberToRole(msg.sender, permission).then(function () {
bot.reply(msg, "added you to the role!");
});
}).then(createdRole => { // this is executed when the role has been created
// adds the sernder to the role
bot.addMemberToRole(msg.author, createdRole).then(() => {
bot.reply(msg, "Added you to the role!");
});
});
}
if (msg.content.indexOf("preset color") === 0) {
// set the role to a preset color
var colorName = msg.content.split(" ")[2];
// get the role by its name
var role = msg.channel.server.getRole("name", "Custom Colors");
// if the color exists as a preset
if (Discord.Color[colorName]) {
// update the role with the new color
bot.updateRole(role, {
color: Discord.Color[colorName]
}).then(function (role) {
// this executes if the change was correct
bot.reply(msg, "done! using the color " + Discord.Color.toHex(role.color));
});
} else {
bot.reply(msg, "that color isn't a preset color!");
}
}
if (msg.content.indexOf("custom color") === 0) {
else if (msg.content.startsWith("custom color")) {
// valid custom colors must follow the format of any of the following:
/*
@@ -79,27 +51,23 @@ bot.on("message", function (msg) {
*/
var colorName = msg.content.split(" ")[2];
// get the role by its name
var role = msg.channel.server.getRole("name", "Custom Colors");
var role = msg.server.roles.get("name", "Custom Colors");
// updates the role with the given color
bot.updateRole(role, {
color: colorName
}).then(function (role) {
// this executes if the change was successful
bot.reply(msg, "done! using the color " + Discord.Color.toHex(role.color));
bot.reply(msg, "Done! Using the color " + colorName);
}).catch(function (e) {
// this executes if it wasn't successful
bot.reply(msg, "an error occurred. Was that a valid hex/dec color?");
})
bot.reply(msg, "An error occurred. Was that a valid hex/dec color?");
});
}
});
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);

View File

@@ -2,8 +2,6 @@
var Discord = require("../../");
Discord.patchStrings();
var AuthDetails = require("../auth.json");
var bot = new Discord.Client();
@@ -13,38 +11,36 @@ bot.on("ready", () => {
});
bot.on("message", (msg) => {
var user = msg.author;
if(msg.content === "can I tts?"){
var user = msg.sender;
// get the evaluated permissions for a user in the channel they asked
var permissions = msg.channel.permissionsOf(user);
if(permissions.sendTTSMessages){
bot.reply(msg, "You " + "can".italic.bold + " send TTS messages.");
}else{
bot.reply(msg, "You " + "can't".italic.bold + " send TTS messages.");
}
}else if(msg.content === "what are my full permissions?"){
var user = msg.sender;
if(permissions.sendTTSMessages)
bot.reply(msg, "You ***can*** send TTS messages.");
else
bot.reply(msg, "You ***can't*** send TTS messages.");
} else if(msg.content === "what are my full permissions?") {
// get the serialised permissions of the user
var permissions = msg.channel.permissionsOf(user).serialise();
// if you want to stringify permissions, they need to be serialised first.
bot.reply(msg, JSON.stringify(permissions, null, 4).replace(/true/g, "**true**"));
}
/*
for a list of more permissions, go to
https://github.com/hydrabolt/discord.js/blob/master/src/EvaluatedPermissions.js
http://discordjs.readthedocs.io/en/indev/docs_permissionconstants.html
*/
})
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);

View File

@@ -1,5 +1,5 @@
/*
this bot is a ping pong bot, and every time a message
This bot is a ping pong bot, and every time a message
beginning with "ping" is sent, it will reply with
"pong!".
*/
@@ -12,12 +12,12 @@ var AuthDetails = require("./auth.json");
var bot = new Discord.Client();
//when the bot is ready
bot.on("ready", function () {
console.log("Ready to begin! Serving in " + bot.channels.length + " channels");
bot.on("ready", () => {
console.log(`Ready to begin! Serving in ${bot.channels.length} channels`);
});
//when the bot disconnects
bot.on("disconnected", function () {
bot.on("disconnected", () => {
//alert the console
console.log("Disconnected!");
@@ -26,15 +26,15 @@ bot.on("disconnected", function () {
});
//when the bot receives a message
bot.on("message", function (msg) {
bot.on("message", msg => {
//if message begins with "ping"
if (msg.content.indexOf("ping") === 0) {
if (msg.content.startsWith("ping")) {
//send a message to the channel the ping message was sent in.
bot.sendMessage(msg.channel, "pong!");
bot.sendMessage(msg, "pong!");
//alert the console
console.log("pong-ed " + msg.author.username);
}
});
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);

View File

@@ -13,21 +13,18 @@ bot.on("ready", () => {
bot.on("message", (msg) => {
if (msg.content === "photos") {
bot.sendFile( msg.channel, "./test/image.png", "photo.png", (err, msg) => {
if(err)
console.log("couldn't send image:", err);
});
}
if( msg.content === "file" ) {
bot.sendFile( msg.channel, new Buffer("Text in a file!"), "file.txt", (err, msg) => {
if(err)
console.log("couldn't send file:", err);
bot.sendFile(msg, "./test/image.png", "photo.png", (err, sentMessage) => {
if (err)
console.log("Couldn't send image: ", err);
});
}
})
else if (msg.content === "file") {
bot.sendFile(msg.channel, new Buffer("Text in a file!"), "file.txt", (err, sentMessage) => {
if (err)
console.log("Couldn't send file: ", err)
});
}
});
bot.login(AuthDetails.email, AuthDetails.password);
bot.loginWithToken(AuthDetails.token);