Improved twitter bot a bit more, replaced all indexOf checks, fixed xp message bug

This commit is contained in:
TheEssem 2019-11-23 17:23:28 -06:00
parent e1347fcf52
commit 0920c459d5
9 changed files with 52 additions and 43 deletions

View file

@ -52,7 +52,7 @@ exports.run = async (message, args) => {
];
if (args.length === 0) {
return `${message.author.mention}, you need to provide some text for the cow to say!`;
} else if (cowList.indexOf(args[0].toLowerCase()) > -1) {
} else if (cowList.includes(args[0].toLowerCase())) {
const cow = args.shift().toLowerCase();
return `\`\`\`\n${cowsay.say({
text: args.join(" "),

View file

@ -1,5 +1,5 @@
exports.run = async (message, args) => {
if (args.length === 0) return `${message.author.mention}, you need to provide what you want to spam!`;
if (message.content.indexOf("@everyone") > -1 || message.content.indexOf("@here") > -1) return "I don't know about you, but that seems like a bad idea.";
if (message.content.includes("@everyone") || message.content.includes("@here")) return "I don't know about you, but that seems like a bad idea.";
return args.join(" ").repeat(500).substring(0, 500);
};

View file

@ -10,7 +10,7 @@ exports.run = async (message, args) => {
switch (args[0].toLowerCase()) {
case "add":
if (args[1] === undefined) return `${message.author.mention}, you need to provide the name of the tag you want to add!`;
if (blacklist.indexOf(args[1].toLowerCase()) > -1) return `${message.author.mention}, you can't make a tag with that name!`;
if (blacklist.includes(args[1].toLowerCase())) return `${message.author.mention}, you can't make a tag with that name!`;
if (tags.has(args[1].toLowerCase())) return `${message.author.mention}, this tag already exists!`;
await setTag(args.slice(2).join(" "), args[1].toLowerCase(), message, guild);
return `${message.author.mention}, the tag \`${args[1].toLowerCase()}\` has been added!`;

View file

@ -2,18 +2,22 @@ const fetch = require("node-fetch");
exports.run = async (message, args) => {
const url = args.length > 0 && args[0].match(/^\d+$/) ? `http://xkcd.com/${args[0]}/info.0.json` : "http://xkcd.com/info.0.json";
const request = await fetch(url);
const json = await request.json();
const embed = {
"embed": {
"title": json.safe_title,
"url": `https://xkcd.com/${json.num}`,
"color": 16711680,
"description": json.alt,
"image": {
"url": json.img
try {
const request = await fetch(url);
const json = await request.json();
const embed = {
"embed": {
"title": json.safe_title,
"url": `https://xkcd.com/${json.num}`,
"color": 16711680,
"description": json.alt,
"image": {
"url": json.img
}
}
}
};
return message.channel.createMessage(embed);
};
return message.channel.createMessage(embed);
} catch {
return `${message.author.mention}, I couldn't find that XKCD!`;
}
};

View file

@ -17,25 +17,20 @@ module.exports = async (message) => {
// xp stuff
const xp = (await database.xp.find({ id: message.channel.guild.id }).exec())[0];
if (xp.enabled === true) {
//console.log(xp.members);
if (xp.enabled === true && !message.member.roles.includes("631290345824714762")) {
const info = xp.members.get(message.author.id);
if (!info) {
logger.log("Member not in XP database, adding");
const memberInfo = {
xpAmount: 1,
xpAmount: 10,
level: 0
};
xp.members.set(message.author.id, memberInfo);
await xp.save();
} else {
let newLevel;
const newAmount = info.xpAmount + 10;
//xp.members[message.author.id].xpAmount++;
const level = Math.floor(0.1 * Math.sqrt(newAmount));
if (info.level < level) {
newLevel = info.level++;
//xp.members[message.author.id].level++;
logger.log(`${message.author.username} has leveled up`);
if (message.channel.guild.id === "631290275456745502" && level === 5) {
await message.author.addRole("638759280752853022", "level 5");
@ -52,14 +47,14 @@ module.exports = async (message) => {
}
xp.members.set(message.author.id, {
xpAmount: newAmount,
level: newLevel ? newLevel : info.level
level: level
});
await xp.save();
}
}
// ignore other stuff
if (message.content.startsWith(prefix) === false && message.mentions.indexOf(client.user) <= -1 && message.channel.id !== "573553254575898626" && (!message.content.match(/https?:\/\/(media|cdn)\.discordapp\.(net|com)\/attachments\/596766080014221373\/606176845871972383\/1561668913236-3.gif/))) return;
if (message.content.startsWith(prefix) === false && !message.mentions.includes(client.user) && message.channel.id !== "573553254575898626") return;
// funny stuff
if (message.channel.id === "573553254575898626" && message.channel.guild.id === "433408970955423765") {
@ -75,12 +70,6 @@ module.exports = async (message) => {
await client.createMessage(generalChannel.id, message.content);
}
}
// || (message.attachments && message.attachments[0].filename === "1561668913236-3.gif")
if (message.channel.guild.id === "322114245632327703" && (message.content.match(/https?:\/\/(media|cdn)\.discordapp\.(net|com)\/attachments\/596766080014221373\/606176845871972383\/1561668913236-3.gif/))) {
const odyMessages = ["Nope!", "No jojo gif here", "sorry ody, this gif is illegal", "get owned"];
await message.delete("anti-jojo mechanism");
await client.createMessage(message.channel.id, misc.random(odyMessages));
}
// separate commands and args
const escapedPrefix = misc.regexEscape(prefix);

View file

@ -49,22 +49,37 @@ module.exports = async () => {
})();
// tweet stuff
if (twitter !== null) {
if (twitter !== null && twitter.active === false) {
const tweet = async () => {
const tweets = (await database.tweets.find({ enabled: true }).exec())[0];
const tweetContent = await misc.getTweet(tweets);
const info = await twitter.client.post("statuses/update", { status: tweetContent });
logger.log(`Tweet with id ${info.data.id_str} has been tweeted with status code ${info.resp.statusCode} ${info.resp.statusMessage}`);
try {
const info = await twitter.client.post("statuses/update", { status: tweetContent });
logger.log(`Tweet with id ${info.data.id_str} has been tweeted with status code ${info.resp.statusCode} ${info.resp.statusMessage}`);
} catch (e) {
const error = JSON.stringify(e);
if (error.includes("Status is a duplicate.")) {
logger.log("Duplicate tweet, will retry in 30 minutes");
} else {
logger.error(e);
}
}
};
tweet();
setInterval(tweet, 1800000);
twitter.active = true;
const stream = twitter.client.stream("statuses/filter", {
track: `@${process.env.HANDLE}`
});
stream.on("tweet", async (tweet) => {
if (tweet.user.screen_name !== "esmBot_") {
const tweets = (await database.tweets.find({ enabled: true }).exec())[0];
const tweetContent = await misc.getTweet(tweets, true);
let tweetContent;
if (tweet.text.includes("@this_vid") || tweet.text.includes("@DownloaderBot") || tweet.text.includes("@GetVideoBot") || tweet.text.includes("@DownloaderB0t") || tweet.text.includes("@thisvid_")) {
tweetContent = await misc.getTweet(tweet, true, true);
} else {
tweetContent = await misc.getTweet(tweets, true);
}
const payload = {
status: `@${tweet.user.screen_name} ${tweetContent}`,
in_reply_to_status_id: tweet.id_str

View file

@ -22,6 +22,7 @@ const tweetSchema = new mongoose.Schema({
phrases: [String],
games: [String],
characters: [String],
download: [String],
enabled: Boolean
});
const TweetCollection = mongoose.model("TweetCollection", tweetSchema);

View file

@ -29,16 +29,15 @@ exports.clean = async (text) => {
};
// get random tweet to post
exports.getTweet = async (tweets, reply = false) => {
const randomTweet = this.random(reply ? tweets.replies : tweets.tweets);
exports.getTweet = async (tweets, reply = false, isDownload = false) => {
const randomTweet = this.random(reply ? (isDownload ? tweets.download : tweets.replies) : tweets.tweets);
if (randomTweet.match("{{message}}")) {
const randomMessage = await this.getRandomMessage();
return randomTweet.replace("{{message}}", randomMessage);
return randomTweet.replace(/{{message}}/g, await this.getRandomMessage());
} else {
return randomTweet.replace("{{media}}", this.random(tweets.media))
.replace("{{games}}", this.random(tweets.games))
.replace("{{phrases}}", this.random(tweets.phrases))
.replace("{{characters}}", this.random(tweets.characters));
return randomTweet.replace(/{{media}}/g, this.random(tweets.media))
.replace(/{{games}}/g, this.random(tweets.games))
.replace(/{{phrases}}/g, this.random(tweets.phrases))
.replace(/{{characters}}/g, this.random(tweets.characters));
}
};

View file

@ -6,6 +6,7 @@ const T = new Twit({
access_token_secret: process.env.ACCESS_SECRET
});
exports.client = T;
exports.active = false;
require("../utils/database.js").tweets.find({ enabled: true }, (error, docs) => {
if (error) throw error;
exports.tweets = docs[0];