Skip to content

Commit

Permalink
feat(notif): add setting for Discord bot avatar URL
Browse files Browse the repository at this point in the history
  • Loading branch information
TheCatLady committed Mar 7, 2021
1 parent 181f2d2 commit 7a60bd2
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 5 deletions.
2 changes: 2 additions & 0 deletions overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1104,6 +1104,8 @@ components:
properties:
botUsername:
type: string
botAvatarUrl:
type: string
webhookUrl:
type: string
SlackSettings:
Expand Down
9 changes: 7 additions & 2 deletions server/lib/notifications/agents/discord.ts
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ interface DiscordRichEmbed {

interface DiscordWebhookPayload {
embeds: DiscordRichEmbed[];
username: string;
username?: string;
avatar_url?: string;
tts: boolean;
content?: string;
Expand Down Expand Up @@ -203,7 +203,11 @@ class DiscordAgent
): Promise<boolean> {
logger.debug('Sending discord notification', { label: 'Notifications' });
try {
const { botUsername, webhookUrl } = this.getSettings().options;
const {
botUsername,
botAvatarUrl,
webhookUrl,
} = this.getSettings().options;

if (!webhookUrl) {
return false;
Expand All @@ -222,6 +226,7 @@ class DiscordAgent

await axios.post(webhookUrl, {
username: botUsername,
avatar_url: botAvatarUrl,
embeds: [this.buildEmbed(type, payload)],
content,
allowed_mentions: {
Expand Down
2 changes: 2 additions & 0 deletions server/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,7 @@ export interface NotificationAgentConfig {
export interface NotificationAgentDiscord extends NotificationAgentConfig {
options: {
botUsername?: string;
botAvatarUrl?: string;
webhookUrl: string;
};
}
Expand Down Expand Up @@ -231,6 +232,7 @@ class Settings {
types: 0,
options: {
botUsername: '',
botAvatarUrl: '',
webhookUrl: '',
},
},
Expand Down
31 changes: 28 additions & 3 deletions src/components/Settings/Notifications/NotificationsDiscord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -14,14 +14,15 @@ const messages = defineMessages({
saving: 'Saving…',
agentenabled: 'Enable Agent',
botUsername: 'Bot Username',
botAvatarUrl: 'Bot Avatar URL',
webhookUrl: 'Webhook URL',
webhookUrlPlaceholder: 'Server Settings → Integrations → Webhooks',
discordsettingssaved: 'Discord notification settings saved successfully!',
discordsettingsfailed: 'Discord notification settings failed to save.',
testsent: 'Test notification sent!',
test: 'Test',
notificationtypes: 'Notification Types',
validationWebhookUrl: 'You must provide a valid URL',
validationUrl: 'You must provide a valid URL',
});

const NotificationsDiscord: React.FC = () => {
Expand All @@ -32,9 +33,12 @@ const NotificationsDiscord: React.FC = () => {
);

const NotificationsDiscordSchema = Yup.object().shape({
botAvatarUrl: Yup.string()
.nullable()
.url(intl.formatMessage(messages.validationUrl)),
webhookUrl: Yup.string()
.required(intl.formatMessage(messages.validationWebhookUrl))
.url(intl.formatMessage(messages.validationWebhookUrl)),
.required(intl.formatMessage(messages.validationUrl))
.url(intl.formatMessage(messages.validationUrl)),
});

if (!data && !error) {
Expand All @@ -47,6 +51,7 @@ const NotificationsDiscord: React.FC = () => {
enabled: data.enabled,
types: data.types,
botUsername: data?.options.botUsername,
botAvatarUrl: data?.options.botAvatarUrl,
webhookUrl: data.options.webhookUrl,
}}
validationSchema={NotificationsDiscordSchema}
Expand All @@ -57,6 +62,7 @@ const NotificationsDiscord: React.FC = () => {
types: values.types,
options: {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
},
});
Expand All @@ -81,6 +87,7 @@ const NotificationsDiscord: React.FC = () => {
types: values.types,
options: {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
},
});
Expand Down Expand Up @@ -119,6 +126,24 @@ const NotificationsDiscord: React.FC = () => {
)}
</div>
</div>
<div className="form-row">
<label htmlFor="botAvatarUrl" className="text-label">
{intl.formatMessage(messages.botAvatarUrl)}
</label>
<div className="form-input">
<div className="flex max-w-lg rounded-md shadow-sm">
<Field
id="botAvatarUrl"
name="botAvatarUrl"
type="text"
placeholder={intl.formatMessage(messages.botAvatarUrl)}
/>
</div>
{errors.botAvatarUrl && touched.botAvatarUrl && (
<div className="error">{errors.botAvatarUrl}</div>
)}
</div>
</div>
<div className="form-row">
<label htmlFor="name" className="text-label">
{intl.formatMessage(messages.webhookUrl)}
Expand Down

0 comments on commit 7a60bd2

Please sign in to comment.