Merge pull request #666 from automatisch/discord/send-message-to-channel

feat(discord): add send message to channel action
This commit is contained in:
Ömer Faruk Aydın
2022-10-30 23:16:26 +01:00
committed by GitHub
14 changed files with 173 additions and 8 deletions

View File

@@ -1 +1,5 @@
export default [];
import sendMessageToChannel from "./send-message-to-channel";
export default [
sendMessageToChannel
];

View File

@@ -0,0 +1,58 @@
import defineAction from '../../../../helpers/define-action';
export default defineAction({
name: 'Send a message to channel',
key: 'sendMessageToChannel',
description: 'Send a message to a specific channel you specify.',
substeps: [
{
key: 'chooseConnection',
name: 'Choose connection',
},
{
key: 'chooseAction',
name: 'Set up action',
arguments: [
{
label: 'Channel',
key: 'channel',
type: 'dropdown' as const,
required: true,
description: 'Pick a channel to send the message to.',
variables: false,
source: {
type: 'query',
name: 'getData',
arguments: [
{
name: 'key',
value: 'listChannels',
},
],
},
},
{
label: 'Message text',
key: 'message',
type: 'string' as const,
required: true,
description: 'The content of your new message.',
variables: true,
}
],
},
{
key: 'testStep',
name: 'Test action',
},
],
async run($) {
const data = {
content: $.step.parameters.message as string,
};
const response = await $.http?.post(`/channels/${$.step.parameters.channel}/messages`, data);
$.setActionItem({ raw: response.data });
},
});

View File

@@ -13,6 +13,7 @@ export default async function createAuthData($: IGlobalVariable) {
client_id: $.auth.data.consumerKey as string,
redirect_uri: callbackUrl,
response_type: 'code',
permissions: '2146958591',
scope: scopes.join(' '),
});

View File

@@ -39,6 +39,18 @@ export default {
description: null,
docUrl: 'https://automatisch.io/docs/discord#consumer-secret',
clickToCopy: false
},
{
key: 'botToken',
label: 'Bot token',
type: 'string' as const,
required: true,
readOnly: false,
value: null,
placeholder: null,
description: null,
docUrl: 'https://automatisch.io/docs/discord#bot-token',
clickToCopy: false
}
],
authenticationSteps: [
@@ -62,6 +74,10 @@ export default {
{
name: 'consumerSecret',
value: '{fields.consumerSecret}'
},
{
name: 'botToken',
value: '{fields.botToken}'
}
]
}
@@ -154,6 +170,10 @@ export default {
{
name: 'consumerSecret',
value: '{fields.consumerSecret}'
},
{
name: 'botToken',
value: '{fields.botToken}'
}
]
}

View File

@@ -28,6 +28,10 @@ const verifyCredentials = async ($: IGlobalVariable) => {
expires_in: expiresIn,
scope: scope,
token_type: tokenType,
guild: {
id: guildId,
name: guildName,
}
} = verifiedCredentials;
await $.auth.set({
@@ -44,6 +48,8 @@ const verifyCredentials = async ($: IGlobalVariable) => {
userId: user.id,
screenName: user.username,
email: user.email,
guildId,
guildName,
});
};

View File

@@ -1,9 +1,9 @@
import { TBeforeRequest } from '@automatisch/types';
const addAuthHeader: TBeforeRequest = ($, requestConfig) => {
const { tokenType, accessToken } = $.auth.data;
if (tokenType && accessToken) {
requestConfig.headers.Authorization = `${tokenType} ${accessToken}`;
const { tokenType, botToken } = $.auth.data;
if (tokenType && botToken) {
requestConfig.headers.Authorization = `Bot ${botToken}`;
}
return requestConfig;

View File

@@ -1,3 +1,3 @@
const scopes = ['identify', 'email'];
const scopes = ['bot', 'identify'];
export default scopes;

View File

@@ -0,0 +1,5 @@
import listChannels from "./list-channels";
export default [
listChannels,
];

View File

@@ -0,0 +1,32 @@
import { IGlobalVariable, IJSONObject } from '@automatisch/types';
export default {
name: 'List channels',
key: 'listChannels',
async run($: IGlobalVariable) {
const channels: {
data: IJSONObject[];
error: IJSONObject | null;
} = {
data: [],
error: null,
};
const response = await $.http.get(`/guilds/${$.auth.data.guildId}/channels`);
channels.data = response.data
.filter((channel: IJSONObject) => {
// filter in text channels only
return channel.type === 0;
})
.map((channel: IJSONObject) => {
return {
value: channel.id,
name: channel.name,
};
});
return channels;
},
};

View File

@@ -1,6 +1,7 @@
import defineApp from '../../helpers/define-app';
import addAuthHeader from './common/add-auth-header';
import auth from './auth';
import data from './data';
import actions from './actions';
import triggers from './triggers';
@@ -15,6 +16,7 @@ export default defineApp({
primaryColor: '5865f2',
beforeRequest: [addAuthHeader],
auth,
data,
triggers,
actions,
});