Aeris : feat/reddit-worker - Adding reactions for new Reddit Service

This commit is contained in:
0Nom4D
2022-03-06 16:53:29 +01:00
parent e43ef602d3
commit fc780df16f
7 changed files with 6231 additions and 1214 deletions
+4713
View File
File diff suppressed because it is too large Load Diff
+1
View File
@@ -31,6 +31,7 @@
"graphql-request": "^4.0.0",
"node-fetch": "^3.2.0",
"rxjs": "^7.5.2",
"snoowrap": "^1.23.0",
"spotify-web-api-node": "^5.0.2",
"twitter-api-v2": "^1.10.0"
},
+8 -12
View File
@@ -3,7 +3,7 @@ export enum ServiceType {
Youtube,
Github,
Spotify,
Discord,
Reddit,
Anilist,
Utils,
};
@@ -31,12 +31,6 @@ export enum PipelineType {
OnSpotifyAddToPlaylist,
OnSpotifySaveToLibrary,
OnDiscordMessage,
OnDiscordMessageFrom,
OnDiscordMention,
OnNewDiscordGuildMember,
OnDiscordGuildLeave,
OnTrigger,
};
@@ -64,11 +58,13 @@ export enum ReactionType {
PlayTrack,
AddTrackToLibrary,
AddToPlaylist,
//Discord
SetDiscordStatus,
PostDiscordDM,
LeaveDiscordServer,
PostDiscordMessage,
//Reddit
JoinSubreddit,
LeaveSubreddit,
PostInSubreddit,
ReplyToPost,
Upvote,
Downvote,
Pause,
// Anilist
ToggleFavourite,
-131
View File
@@ -1,131 +0,0 @@
import { Pipeline, PipelineEnv, PipelineType, ReactionType, ServiceType } from "../models/pipeline";
import { action, BaseService, reaction, service } from "../models/base-service";
import { Client, GuildMember, Intents, Message, TextChannel } from "discord.js";
import { filter, fromEvent, map, Observable } from "rxjs";
@service(ServiceType.Discord)
export class Discord extends BaseService {
private _client: Client<boolean>;
constructor(_: Pipeline) {
super();
this._client = new Client({ intents: [Intents.FLAGS.GUILDS] });
}
@action(PipelineType.OnDiscordMessage, [])
listenMessages(_: any): Observable<PipelineEnv> {
return fromEvent(this._client, "messageCreated")
.pipe(
map((x: Message) => ({
MESSAGE: x.content,
AUTHOR_ID: x.author.id,
AUTHOR_NAME: x.author.username,
})),
);
}
@action(PipelineType.OnDiscordMessageFrom, ['user_id'])
listenMessagesFrom(params: any): Observable<PipelineEnv> {
return fromEvent(this._client, "messageCreated")
.pipe(
filter((x: Message) => x.author.id == params['user_id']),
map((x: Message) => ({
MESSAGE: x.content,
AUTHOR_ID: x.author.id,
AUTHOR_NAME: x.author.username,
})),
);
}
@action(PipelineType.OnDiscordMention, [])
listenMentions(_: any): Observable<PipelineEnv> {
return fromEvent(this._client, "message")
.pipe(
filter((x: Message) => x.mentions.has(this._client.user)),
map((x: Message) => ({
MESSAGE: x.content,
AUTHOR_ID: x.author.id,
AUTHOR_NAME: x.author.username,
})),
);
}
@action(PipelineType.OnNewDiscordGuildMember, [])
listenNewGuildMember(_: any): Observable<PipelineEnv> {
return fromEvent(this._client, "guildMemberAdd")
.pipe(
map((member: GuildMember) => ({
NEW_MEMBER_ID: member.user.id,
NEW_MEMBER_NAME: member.user.username,
SERVER_ID: member.guild.id,
SERVER_NAME: member.guild.name
})),
);
}
@action(PipelineType.OnDiscordGuildLeave, [])
listenGuildLeave(_: any): Observable<PipelineEnv> {
return fromEvent(this._client, "guildMemberRemove")
.pipe(
map((member: GuildMember) => ({
NEW_MEMBER_ID: member.user.id,
NEW_MEMBER_NAME: member.user.username,
SERVER_ID: member.guild.id,
SERVER_NAME: member.guild.name
})),
);
}
@reaction(ReactionType.PostDiscordMessage, ['server_id', 'channel_id', 'content'])
async postMessage(params :any): Promise<PipelineEnv> {
let guild = await this._client.guilds.fetch(params['server_id']);
let channel = guild.channels.cache.get(params['channel_id']);
let message = await (<TextChannel> channel).send(params['content']);
return {
USER_ID: this._client.user.username,
USERNAME: this._client.user.id,
MESSAGE_CONTENT: message.content,
CHANNEL_ID: channel.id,
CHANNEL_NAME: channel.name,
SERVER_ID: guild.id,
SERVER_NAME: guild.name,
};
}
@reaction(ReactionType.PostDiscordDM, ['other_id', 'content'])
async postDM(params: any): Promise<PipelineEnv> {
let res = await this._client.users.fetch(params['other_id']);
let message = await res.send(params['content']);
return {
USER_ID: message.author.id,
USERNAME: message.author.username,
SENDEE_ID: message.member.user.id,
SENDEE_USERNAME: message.member.user.username,
MESSAGE_CONTENT: message.content,
};
}
@reaction(ReactionType.LeaveDiscordServer, ['server_id'])
async leaveServer(params :any): Promise<PipelineEnv> {
let guild = await this._client.guilds.fetch(params['server_id']);
await guild.leave();
return {
SERVER_NAME: guild.name,
SERVER_ID: guild.id,
USER_ID: guild.me.user.id,
USERNAME: guild.me.user.username,
}
}
@reaction(ReactionType.SetDiscordStatus, ['status'])
async setStatus(params:any): Promise<PipelineEnv> {
let res = await this._client.user.setStatus(params['status']);
return {
USER_ID: res.user.id,
USERNAME: res.user.username,
STATUS: params['status']
};
}
}
+1 -1
View File
@@ -2,6 +2,6 @@ export { Youtube } from "./youtube"
export { Twitter } from "./twitter"
export { Github } from "./github"
export { Spotify } from "./spotify"
export { Discord } from "./discord"
export { Reddit } from "./reddit"
export { Anilist } from "./anilist"
export { UtilsService } from "./utils"
+99
View File
@@ -0,0 +1,99 @@
import { Pipeline, PipelineEnv, PipelineType, ReactionType, ServiceType } from "../models/pipeline";
import { action, BaseService, reaction, service } from "../models/base-service";
import { Client, GuildMember, Intents, Message, TextChannel } from "discord.js";
import { filter, fromEvent, map, Observable } from "rxjs";
import snoowrap, {Submission, Subreddit} from 'snoowrap';
@service(ServiceType.Reddit)
export class Reddit extends BaseService {
private _client: snoowrap;
constructor(pipeline: Pipeline) {
super();
this._client = new snoowrap({
userAgent: "Aeris",
accessToken: pipeline.userData["Reddit"].accessToken
})
}
@reaction(ReactionType.JoinSubreddit, ['sub_id'])
async joinSubreddit(params: any): Promise<PipelineEnv> {
return this._client.getSubreddit(params['sub_id']).subscribe().then((response) => {
return {
SUB_NAME: response.name,
SUB_ID: response.id
};
});
}
@reaction(ReactionType.LeaveSubreddit, ['sub_id'])
async leaveSubreddit(params: any): Promise<PipelineEnv> {
return this._client.getSubreddit(params['sub_id']).unsubscribe().then((response) => {
return {
SUB_NAME: response.name,
SUB_ID: response.id
};
});
}
@reaction(ReactionType.PostInSubreddit, ['sub_id', 'title', 'body'])
async postInSubreddit(params: any): Promise<PipelineEnv> {
return this._client.getSubreddit(params['sub_id']).submitSelfpost({
subredditName: params['sub_id'],
title: params['title'],
text: params['body']
}).then((response) => {
return {
SUB_NAME: response.subreddit.name,
SUB_ID: response.subreddit.id,
POST_ID: response.id
};
});
}
@reaction(ReactionType.ReplyToPost, ['post_id', 'reply_body'])
async commentPost(params: any): Promise<PipelineEnv> {
return this._client.getSubmission(params['post_id']).
then((response) => {
return response.reply(params['reply_body']).then((res) => {
return {
SUB_NAME: response.subreddit.name,
SUB_ID: response.subreddit.id,
POST_ID: response.id,
REPLY_ID: res.id
};
})
});
}
@reaction(ReactionType.Upvote, ['post_id'])
async upvotePost(params: any): Promise<PipelineEnv> {
return this._client.getSubmission(params['post_id']).
then((response) => {
return response.upvote().then((res) => {
return {
SUB_NAME: response.subreddit.name,
SUB_ID: response.subreddit.id,
POST_ID: response.id,
UPVOTE_COUNT: res.ups
};
});
});
}
@reaction(ReactionType.Downvote, ['post_id'])
async downvotePost(params: any): Promise<PipelineEnv> {
return this._client.getSubmission(params['post_id']).
then((response) => {
return response.downvote().then((res) => {
return {
SUB_NAME: response.subreddit.name,
SUB_ID: response.subreddit.id,
POST_ID: response.id,
DOWNVOTE_COUNT: res.downs
};
});
});
}
}
+1409 -1070
View File
File diff suppressed because it is too large Load Diff