Skip to content

Commit

Permalink
Merge pull request #4 from ansonfoong/feat/message-options
Browse files Browse the repository at this point in the history
TextChannel.send() method now expects a string, or MessageOptions type.
  • Loading branch information
stuyy authored May 21, 2020
2 parents 64e3bce + aee12b8 commit b6727ea
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 21 deletions.
7 changes: 1 addition & 6 deletions src/client/rest/RestAPIHandler.ts
Original file line number Diff line number Diff line change
Expand Up @@ -50,16 +50,11 @@ export default class RestAPIHandler {
}

async createMessage(options: MessageOptions, id: string) {
const data = {
"content": options.content,
"tts": options.tts,
};
const response = await fetch(`${Constants.API}/${ENDPOINTS.CHANNELS}/${id}/${ENDPOINTS.MESSAGES}`, {
method: 'POST',
headers,
body: JSON.stringify(data),
body: JSON.stringify(options),
});
console.log(response);
return response.json();
}

Expand Down
17 changes: 8 additions & 9 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,12 @@ import { Client } from '../mod.ts';
import "https://deno.land/x/dotenv/load.ts";
import Guild from './models/Guild.ts';
import Message from './models/Message.ts';
import { TextChannel } from './models/channels/TextChannel.ts';

const client = new Client();
client.login(Deno.env.get('BOT_TOKEN')!.toString());

client.on('ready', () => {
console.log('Bot has logged in.');
const guild: Guild = client.guilds.get('533070839806165023');
const members = guild.members;
});

client.on('guildCreate', (guild: Guild) => {
Expand All @@ -19,15 +16,17 @@ client.on('guildCreate', (guild: Guild) => {

client.on('message', (message: Message) => {
if (message.content === '?hello') {
message.channel.send('hello');
} else if (message.content === '?embed') {
message.channel.send({
content: 'Hello!',
tts: false,
});
} else if (message.content === '?help') {
message.channel.send({
content: 'help command'
content: 'Hello',
embed: {
title: 'Hi',
description: 'Yoooo'
}
})
}

});

client.on('debug', (data: any) => {
Expand Down
14 changes: 11 additions & 3 deletions src/models/channels/TextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,16 @@ export class TextChannel extends GuildChannel implements TextBasedChannel {
super(_id, _client, _type, _lastMessageId, _lastPinTimestamp, _name, _position, _parentId, _topic, _guild, _permissionOverwrites, _nsfw, _rateLimitPerUser);
}

send(options: MessageOptions): any {
const response = this.client.rest.createMessage(options, this.id);
console.log(response);
send(payload: string | MessageOptions): any {
if (typeof payload === 'string') {
const body: MessageOptions = { content: payload };
const response = this.client.rest.createMessage(body, this.id);
return;
}

if (payload && payload.content) {
const response = this.client.rest.createMessage(payload, this.id);
return;
}
}
}
2 changes: 1 addition & 1 deletion src/models/interfaces/ITextChannel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,5 +3,5 @@ import { MessageOptions } from '../../typedefs/MessageOptions.ts';

export default interface TextBasedChannel {

send(options: MessageOptions): any;
send(payload: string | MessageOptions): any;
}
9 changes: 7 additions & 2 deletions src/typedefs/MessageOptions.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
export interface MessageOptions {
content: string;
content?: string;
tts?: boolean;
embed?: any;
embed?: MessageEmbed;
}

export interface MessageEmbed {
title: string;
description?: string
}

0 comments on commit b6727ea

Please sign in to comment.