mirror of
https://github.com/arthur-pbty/xiao.git
synced 2026-06-06 06:10:49 +02:00
Make MemePoster class extend webhook client
This commit is contained in:
@@ -59,8 +59,15 @@ client.on('ready', () => {
|
||||
const text = typeof activity.text === 'function' ? activity.text() : activity.text;
|
||||
client.user.setActivity(text, { type: activity.type });
|
||||
}, 60000);
|
||||
if (client.memePoster.id && client.memePoster.token) {
|
||||
client.setInterval(() => client.memePoster.post(), client.memePoster.time);
|
||||
if (client.memePoster) {
|
||||
client.setInterval(async () => {
|
||||
try {
|
||||
const post = await client.memePoster.fetchRandomPost(false);
|
||||
await client.memePoster.post(post);
|
||||
} catch (err) {
|
||||
client.logger.error(err);
|
||||
}
|
||||
}, client.memePoster.postInterval);
|
||||
}
|
||||
});
|
||||
|
||||
|
||||
+1
-1
@@ -1,6 +1,6 @@
|
||||
{
|
||||
"name": "xiao",
|
||||
"version": "114.8.4",
|
||||
"version": "114.8.5",
|
||||
"description": "Your personal server companion.",
|
||||
"main": "Xiao.js",
|
||||
"scripts": {
|
||||
|
||||
@@ -3,10 +3,11 @@ const { WebhookClient } = require('discord.js');
|
||||
const Collection = require('@discordjs/collection');
|
||||
const winston = require('winston');
|
||||
const PokemonStore = require('./pokemon/PokemonStore');
|
||||
const MemePoster = require('./MemePoster');
|
||||
const MemePosterClient = require('./MemePoster');
|
||||
const activities = require('../assets/json/activity');
|
||||
const leaveMsgs = require('../assets/json/leave-messages');
|
||||
const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN } = process.env;
|
||||
const subreddits = require('../assets/json/meme');
|
||||
const { XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env;
|
||||
|
||||
module.exports = class XiaoClient extends CommandoClient {
|
||||
constructor(options) {
|
||||
@@ -21,7 +22,12 @@ module.exports = class XiaoClient extends CommandoClient {
|
||||
});
|
||||
this.webhook = new WebhookClient(XIAO_WEBHOOK_ID, XIAO_WEBHOOK_TOKEN, { disableMentions: 'everyone' });
|
||||
this.pokemon = new PokemonStore();
|
||||
this.memePoster = new MemePoster(this);
|
||||
this.memePoster = POSTER_ID && POSTER_TOKEN ? new MemePosterClient(POSTER_ID, POSTER_TOKEN, {
|
||||
subreddits,
|
||||
postTypes: ['image', 'rich:video'],
|
||||
postInterval: POSTER_TIME,
|
||||
disableMentions: 'everyone'
|
||||
}) : null;
|
||||
this.games = new Collection();
|
||||
this.phone = new Collection();
|
||||
this.activities = activities;
|
||||
|
||||
+31
-23
@@ -1,41 +1,49 @@
|
||||
const { POSTER_ID, POSTER_TOKEN, POSTER_TIME } = process.env;
|
||||
const { WebhookClient } = require('discord.js');
|
||||
const request = require('node-superfetch');
|
||||
const { embedURL } = require('../util/Util');
|
||||
const subreddits = require('../assets/json/meme');
|
||||
const types = ['image', 'rich:video'];
|
||||
|
||||
module.exports = class MemePoster {
|
||||
constructor(client) {
|
||||
Object.defineProperty(this, 'client', { value: client });
|
||||
module.exports = class MemePosterClient extends WebhookClient {
|
||||
constructor(id, token, options) {
|
||||
super(id, token, options);
|
||||
|
||||
this.id = POSTER_ID;
|
||||
this.token = POSTER_TOKEN;
|
||||
this.time = Number.parseFloat(POSTER_TIME) || 3.6e+6;
|
||||
this.subreddits = options.subreddits;
|
||||
this.postTypes = options.postTypes;
|
||||
this.postInterval = options.postInterval ? Number.parseFloat(options.postInterval) : 3.6e+6;
|
||||
}
|
||||
|
||||
async post() {
|
||||
try {
|
||||
const subreddit = subreddits[Math.floor(Math.random() * subreddits.length)];
|
||||
const post = await this.fetchMeme(subreddit);
|
||||
if (!post) return;
|
||||
const url = embedURL(post.title, `<https://www.reddit.com${post.permalink}>`);
|
||||
await request
|
||||
.post(`https://discordapp.com/api/webhooks/${this.id}/${this.token}`)
|
||||
.send({ content: `**r/${subreddit}** ${url}\n${post.url}` });
|
||||
} catch (err) {
|
||||
this.client.logger.error(err);
|
||||
}
|
||||
post(post) {
|
||||
const url = embedURL(post.title, `<https://www.reddit.com${post.permalink}>`);
|
||||
return this.send(`**r/${subreddit}** ${url}\n${post.url}`);
|
||||
}
|
||||
|
||||
async fetchMeme(subreddit) {
|
||||
async fetchRandomPost(nsfw) {
|
||||
const subreddit = this.randomSubreddit();
|
||||
const post = await this.fetchPost(subreddit, nsfw);
|
||||
return {
|
||||
subreddit,
|
||||
title: post.title,
|
||||
url: post.permalink,
|
||||
type: post.post_hint,
|
||||
nsfw: post.over_18 || false
|
||||
};
|
||||
}
|
||||
|
||||
async fetchPost(subreddit, nsfw) {
|
||||
const { body } = await request
|
||||
.get(`https://www.reddit.com/r/${subreddit}/hot.json`)
|
||||
.query({ limit: 100 });
|
||||
const posts = body.data.children.filter(post => {
|
||||
if (!post.data) return false;
|
||||
return types.includes(post.data.post_hint) && post.data.url && post.data.title && !post.data.over_18;
|
||||
return this.postTypes.includes(post.data.post_hint)
|
||||
&& post.data.url
|
||||
&& post.data.title
|
||||
&& nsfw ? true : !post.data.over_18;
|
||||
});
|
||||
if (!posts.length) return null;
|
||||
return posts[Math.floor(Math.random() * posts.length)].data;
|
||||
}
|
||||
|
||||
randomSubreddit() {
|
||||
return this.subreddits[Math.floor(Math.random() * this.subreddits.length)];
|
||||
}
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user