Skip to content

Commit

Permalink
QOL Changes
Browse files Browse the repository at this point in the history
  • Loading branch information
AlenVelocity committed Apr 14, 2021
1 parent 11045d6 commit 0ab61bd
Show file tree
Hide file tree
Showing 8 changed files with 127 additions and 102 deletions.
Binary file added assets/images/Error-500.gif
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added assets/videos/Error-500.mp4
Binary file not shown.
8 changes: 6 additions & 2 deletions src/Client/Utils.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { MessageType, WAConnection, WAContact, WAGroupMetadata, WAMessage } from '@adiwajshing/baileys/'
import { Browsers, MessageType, WAConnection, WAContact, WAGroupMetadata, WAMessage } from '@adiwajshing/baileys/'
import { Model } from 'mongoose'
import responses from '../lib/responses.json'
import { schedule, validate } from 'node-cron'
Expand All @@ -8,6 +8,9 @@ import { IReply, IConfig, IGroupModel, IUserModel, ISessionModel, ISession } fro
import { existsSync } from 'fs-extra'
import { join } from 'path'
export class Client extends WAConnection {
assets = join(__dirname, '..', '..', 'assets')

browserDescription = Browsers.ubuntu('WhatsApp-Botto-Xre')
private config: IConfig = {
name: process.env.BOT_NAME || 'Xre',
prefix: process.env.PREFIX || '!',
Expand Down Expand Up @@ -45,7 +48,8 @@ export class Client extends WAConnection {
async reply(jid: string, options: IReply, quote?: WAMessage): Promise<unknown> {
return await this.sendMessage(jid, options.body, options.type || MessageType.text, {
quoted: quote,
caption: options.caption
caption: options.caption,
mimetype: options.mime
})
}

Expand Down
203 changes: 109 additions & 94 deletions src/Handler/Message.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
import { MessageType, proto, WAGroupMetadata, WAMessage } from '@adiwajshing/baileys'
import { MessageType, Mimetype, proto, WAGroupMetadata, WAMessage } from '@adiwajshing/baileys'
import chalk from 'chalk'
import { Client } from '../Client'
import { createSticker, help, toggleableGroupActions, getWById, wSearch, ytSreach, getYTMediaFromUrl } from '../lib'
import moment from 'moment-timezone'
import responses from '../lib/responses.json'
import Utils from '../Utils'
import { IParsedArgs } from '../Typings'
import { readFile } from 'fs-extra'
import { join } from 'path'
export class Message {
validTypes = [MessageType.text, MessageType.image, MessageType.video, MessageType.extendedText]
constructor(private client: Client) {}
Expand Down Expand Up @@ -62,99 +64,112 @@ export class Message {
if (userData.ban) return void this.client.reply(from, { body: responses['banned'] }, M)

const ad = Math.floor(Math.random() * 5) + 1

switch (command) {
default:
this.client.reply(from, { body: responses['invalid-command'] }, M)
break
case 'id':
return void this.client.reply(from, { body: `GID: ${from}` }, M)
case 'everyone':
return void this.client.everyone(from, group.metadata, admin, flags.includes('--hide'), M)
case 'group':
return void this.client.reply(from, await this.client.group.simplifiedGroupInfo(group), M)
case 'eval':
if (mod) return void eval(slicedJoinedArgs)
break
case 'join':
return void this.client.reply(
from,
from === process.env.ADMIN_GROUP_JID
? await this.client.group.join(slicedJoinedArgs, mod, username)
: { body: responses['cannot-execute'] },
M
)
case 'ban':
case 'unban':
if (!mod || mentioned.length === 0) return
return this.client.banAction(from, mentioned, command === 'ban', M)
break
case 'hi':
this.client.reply(from, { body: `Hi! ${username}` }, M)
break
case 'promote':
case 'demote':
case 'remove':
this.client.reply(
from,
await this.client.group.toggleEvent(from, mentioned || [], admin, iAdmin, command),
M
)
break
case 'help':
this.client.reply(from, { body: help(this.client, slicedJoinedArgs.toLowerCase().trim()) }, M)
break
case 'sticker':
const sticker = !media
? { body: responses['wrong-format-media'] }
: await createSticker(
await this.client.downloadMediaMessage(media),
flags.includes('--strech'),
barSplit[1],
barSplit[2]
)
const m = await this.client.reply(from, sticker, M)
if (m && typeof m === 'object' && (m as WAMessage)?.message?.stickerMessage && ad === 5)
return void this.client.reply(from, { body: responses['ads']['sticker'] }, m as WAMessage)
break
case 'anime':
case 'manga':
case 'character':
this.client.reply(from, await wSearch(slicedJoinedArgs, this.client._config.prefix, command), M)
break
case 'aid':
case 'mid':
case 'chid':
this.client.reply(
from,
await getWById(
slicedJoinedArgs,
command === 'aid' ? 'anime' : command === 'mid' ? 'manga' : 'character'
),
M
)
break
case 'register':
case 'unregister':
return void this.client.reply(
from,
await this.client.group.register(
admin,
group.data,
command === 'register',
slicedJoinedArgs.toLowerCase().trim() as toggleableGroupActions
),
M
)
case 'yta':
case 'ytv':
return void this.client.reply(
from,
await getYTMediaFromUrl(slicedJoinedArgs.trim(), command === 'ytv' ? 'video' : 'audio'),
M
)
case 'yts':
return void this.client.reply(from, { body: await ytSreach(slicedJoinedArgs.trim()) }, M)
try {
switch (command) {
default:
this.client.reply(from, { body: responses['invalid-command'] }, M)
break
case 'id':
return void this.client.reply(from, { body: `GID: ${from}` }, M)
case 'everyone':
return void this.client.everyone(from, group.metadata, admin, flags.includes('--hide'), M)
case 'group':
return void this.client.reply(from, await this.client.group.simplifiedGroupInfo(group), M)
case 'eval':
if (mod) return void eval(slicedJoinedArgs)
break
case 'join':
return void this.client.reply(
from,
from === process.env.ADMIN_GROUP_JID
? await this.client.group.join(slicedJoinedArgs, mod, username)
: { body: responses['cannot-execute'] },
M
)
case 'ban':
case 'unban':
if (!mod || mentioned.length === 0) return
return this.client.banAction(from, mentioned, command === 'ban', M)
break
case 'hi':
this.client.reply(from, { body: `Hi! ${username}` }, M)
break
case 'promote':
case 'demote':
case 'remove':
this.client.reply(
from,
await this.client.group.toggleEvent(from, mentioned || [], admin, iAdmin, command),
M
)
break
case 'help':
this.client.reply(from, { body: help(this.client, slicedJoinedArgs.toLowerCase().trim()) }, M)
break
case 'sticker':
const sticker = !media
? { body: responses['wrong-format-media'] }
: await createSticker(
await this.client.downloadMediaMessage(media),
flags.includes('--strech'),
barSplit[1],
barSplit[2]
)
const m = await this.client.reply(from, sticker, M)
if (m && typeof m === 'object' && (m as WAMessage)?.message?.stickerMessage && ad === 5)
return void this.client.reply(from, { body: responses['ads']['sticker'] }, m as WAMessage)
break
case 'anime':
case 'manga':
case 'character':
this.client.reply(from, await wSearch(slicedJoinedArgs, this.client._config.prefix, command), M)
break
case 'aid':
case 'mid':
case 'chid':
this.client.reply(
from,
await getWById(
slicedJoinedArgs,
command === 'aid' ? 'anime' : command === 'mid' ? 'manga' : 'character'
),
M
)
break
case 'register':
case 'unregister':
return void this.client.reply(
from,
await this.client.group.register(
admin,
group.data,
command === 'register',
slicedJoinedArgs.toLowerCase().trim() as toggleableGroupActions
),
M
)
case 'yta':
case 'ytv':
return void this.client.reply(
from,
await getYTMediaFromUrl(slicedJoinedArgs.trim(), command === 'ytv' ? 'video' : 'audio'),
M
)
case 'yts':
return void this.client.reply(from, { body: await ytSreach(slicedJoinedArgs.trim()) }, M)
}
} catch (err) {
console.log(err)
return void this.client.reply(
from,
{
body: await readFile(join(this.client.assets, 'images', 'Error-500.gif')),
caption: !mod ? responses.error[500].regular : responses.error[500].mod.replace('{M}', err.message),
type: MessageType.video,
mime: Mimetype.gif
},
M
)
}
}

Expand Down
3 changes: 2 additions & 1 deletion src/Typings/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { WAGroupMetadata } from '@adiwajshing/baileys'
import { Mimetype, WAGroupMetadata } from '@adiwajshing/baileys'
import { Document } from 'mongoose'
export interface IGroupinfo {
metadata: WAGroupMetadata
Expand All @@ -17,6 +17,7 @@ export interface IReply {
body: string | Buffer
type?: MessageType
caption?: string
mime?: Mimetype
}

export interface IEvent {
Expand Down
5 changes: 1 addition & 4 deletions src/Utils/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import axios, { AxiosRequestConfig } from 'axios'
import Embed from './Embed'
import { readFileSync } from 'fs-extra'
import { join } from 'path'

export default class Utils {
/* eslint-disable @typescript-eslint/no-explicit-any*/
static fetch = async (url: string, options: AxiosRequestConfig): Promise<Buffer | any> =>
Expand All @@ -20,6 +19,4 @@ export default class Utils {
static urlRegExp = /(https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|www\.[a-zA-Z0-9][a-zA-Z0-9-]+[a-zA-Z0-9]\.[^\s]{2,}|https?:\/\/(?:www\.|(?!www))[a-zA-Z0-9]+\.[^\s]{2,}|www\.[a-zA-Z0-9]+\.[^\s]{2,})/

static urlMatch = (text: string): RegExpMatchArray | null => text.match(Utils.urlRegExp)

static yui404 = readFileSync(join(__dirname, '..', '..', 'assets', 'images', 'yui.jpg'))
}
4 changes: 3 additions & 1 deletion src/lib/group.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ import Utils from '../Utils'
import responses from './responses.json'
import moment from 'moment-timezone'
import { IGroup, IGroupinfo, IReply } from '../Typings'
import { join } from 'path'
import { readFile } from 'fs-extra'
export class GroupEx {
constructor(public client: Client) {}

Expand Down Expand Up @@ -96,7 +98,7 @@ export class GroupEx {
]
const owner = this.client.contacts[metadata.owner]
return {
body: icon ? await Utils.download(icon) : Utils.yui404,
body: icon ? await Utils.download(icon) : await readFile(join(this.client.assets, 'images', 'yui.jpg')),
caption: `💮 *Title:* ${metadata.subject}\n\n👑 *Created By:* ${
owner?.notify || owner?.vname || owner?.name || metadata.owner.split('@')[0]
}\n\n📅 *Created On:* ${moment(metadata.creation * 1000).format('DD/MM HH:mm:ss')}\n\n🔊 *Announce:* ${
Expand Down
6 changes: 6 additions & 0 deletions src/lib/responses.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,12 @@
"invalid-url": "Invalid {W} 📘 *URL: {U}*",
"video-duration-clause": "🕐 Cannot fetch videos longer than *10 Minutes*",
"nsfw-detected": "*NSFW Detected*❗",
"error": {
"500": {
"mod": "{M} | Report this at https://github.com/SomnathDas/Whatsapp-Botto-Xre/issues if you are using the latest commit",
"regular": "An Error Occured [Code: 500]"
}
},
"ads": {
"sticker": "*Sticker Creation Powered By https://npmjs.com/package/wa-sticker-formatter*"
},
Expand Down

0 comments on commit 0ab61bd

Please sign in to comment.