Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(notif): add settings for Discord bot username & avatar URL #1113

Merged
merged 3 commits into from
Mar 9, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions overseerr-api.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1102,6 +1102,10 @@ components:
options:
type: object
properties:
botUsername:
type: string
botAvatarUrl:
type: string
webhookUrl:
type: string
SlackSettings:
Expand Down Expand Up @@ -1146,10 +1150,14 @@ components:
options:
type: object
properties:
botUsername:
type: string
botAPI:
type: string
chatId:
type: string
sendSilently:
type: boolean
PushbulletSettings:
type: object
properties:
Expand Down
12 changes: 8 additions & 4 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,8 +203,11 @@ class DiscordAgent
): Promise<boolean> {
logger.debug('Sending discord notification', { label: 'Notifications' });
try {
const settings = getSettings();
const webhookUrl = this.getSettings().options.webhookUrl;
const {
botUsername,
botAvatarUrl,
webhookUrl,
} = this.getSettings().options;

if (!webhookUrl) {
return false;
Expand All @@ -222,7 +225,8 @@ class DiscordAgent
}

await axios.post(webhookUrl, {
username: settings.main.applicationTitle,
username: botUsername,
avatar_url: botAvatarUrl,
embeds: [this.buildEmbed(type, payload)],
content,
allowed_mentions: {
Expand Down
6 changes: 5 additions & 1 deletion server/lib/settings.ts
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ export interface NotificationAgentConfig {
}
export interface NotificationAgentDiscord extends NotificationAgentConfig {
options: {
botUsername?: string;
botAvatarUrl?: string;
webhookUrl: string;
};
}
Expand All @@ -120,7 +122,7 @@ export interface NotificationAgentEmail extends NotificationAgentConfig {

export interface NotificationAgentTelegram extends NotificationAgentConfig {
options: {
botUsername: string;
botUsername?: string;
botAPI: string;
chatId: string;
sendSilently: boolean;
Expand Down Expand Up @@ -229,6 +231,8 @@ class Settings {
enabled: false,
types: 0,
options: {
botUsername: '',
botAvatarUrl: '',
webhookUrl: '',
},
},
Expand Down
53 changes: 50 additions & 3 deletions src/components/Settings/Notifications/NotificationsDiscord.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -13,14 +13,16 @@ const messages = defineMessages({
save: 'Save Changes',
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',
TheCatLady marked this conversation as resolved.
Show resolved Hide resolved
});

const NotificationsDiscord: React.FC = () => {
Expand All @@ -31,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 @@ -45,6 +50,8 @@ const NotificationsDiscord: React.FC = () => {
initialValues={{
enabled: data.enabled,
types: data.types,
botUsername: data?.options.botUsername,
botAvatarUrl: data?.options.botAvatarUrl,
webhookUrl: data.options.webhookUrl,
}}
validationSchema={NotificationsDiscordSchema}
Expand All @@ -54,6 +61,8 @@ const NotificationsDiscord: React.FC = () => {
enabled: values.enabled,
types: values.types,
options: {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
},
});
Expand All @@ -77,6 +86,8 @@ const NotificationsDiscord: React.FC = () => {
enabled: true,
types: values.types,
options: {
botUsername: values.botUsername,
botAvatarUrl: values.botAvatarUrl,
webhookUrl: values.webhookUrl,
},
});
Expand All @@ -97,6 +108,42 @@ const NotificationsDiscord: React.FC = () => {
<Field type="checkbox" id="enabled" name="enabled" />
</div>
</div>
<div className="form-row">
<label htmlFor="botUsername" className="text-label">
{intl.formatMessage(messages.botUsername)}
</label>
<div className="form-input">
<div className="flex max-w-lg rounded-md shadow-sm">
<Field
id="botUsername"
name="botUsername"
type="text"
placeholder={intl.formatMessage(messages.botUsername)}
/>
</div>
{errors.botUsername && touched.botUsername && (
<div className="error">{errors.botUsername}</div>
)}
</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
3 changes: 2 additions & 1 deletion src/i18n/locale/en.json
Original file line number Diff line number Diff line change
Expand Up @@ -311,6 +311,7 @@
"components.Settings.Notifications.authPass": "SMTP Password",
"components.Settings.Notifications.authUser": "SMTP Username",
"components.Settings.Notifications.botAPI": "Bot Authentication Token",
"components.Settings.Notifications.botAvatarUrl": "Bot Avatar URL",
"components.Settings.Notifications.botUsername": "Bot Username",
"components.Settings.Notifications.chatId": "Chat ID",
"components.Settings.Notifications.discordsettingsfailed": "Discord notification settings failed to save.",
Expand Down Expand Up @@ -341,7 +342,7 @@
"components.Settings.Notifications.validationEmail": "You must provide a valid email address",
"components.Settings.Notifications.validationSmtpHostRequired": "You must provide an SMTP host",
"components.Settings.Notifications.validationSmtpPortRequired": "You must provide an SMTP port",
"components.Settings.Notifications.validationWebhookUrl": "You must provide a valid URL",
"components.Settings.Notifications.validationUrl": "You must provide a valid URL",
"components.Settings.Notifications.webhookUrl": "Webhook URL",
"components.Settings.Notifications.webhookUrlPlaceholder": "Server Settings → Integrations → Webhooks",
"components.Settings.RadarrModal.add": "Add Server",
Expand Down