Skip to content

Commit 8ee7e21

Browse files
committed
add setting to show link button & rename link type message
1 parent b38d23c commit 8ee7e21

File tree

5 files changed

+40
-8
lines changed

5 files changed

+40
-8
lines changed

redditbot/bot.go

+1
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ type SetupState struct {
4545
FormatType FormatType
4646
RoleID snowflake.ID
4747
RedditProxy string
48+
LinkButton bool
4849
Interaction discord.ApplicationCommandInteraction
4950
}
5051

redditbot/database.go

+4-3
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ type Subscription struct {
3434
LastPost time.Time `db:"last_post"`
3535
RoleID snowflake.ID `db:"role_id"`
3636
RedditProxy string `db:"reddit_proxy"`
37+
LinkButton bool `db:"link_button"`
3738
}
3839

3940
func NewDB(cfg DatabaseConfig, schema string) (*DB, error) {
@@ -73,12 +74,12 @@ func (d *DB) Close() error {
7374
}
7475

7576
func (d *DB) AddSubscription(sub Subscription) error {
76-
_, err := d.dbx.NamedExec(`INSERT INTO subscriptions (subreddit, format_type, guild_id, channel_id, webhook_id, webhook_token, role_id, reddit_proxy) VALUES (:subreddit, :format_type, :guild_id, :channel_id, :webhook_id, :webhook_token, :role_id, :reddit_proxy)`, sub)
77+
_, err := d.dbx.NamedExec(`INSERT INTO subscriptions (subreddit, format_type, guild_id, channel_id, webhook_id, webhook_token, role_id, reddit_proxy, link_button) VALUES (:subreddit, :format_type, :guild_id, :channel_id, :webhook_id, :webhook_token, :role_id, :reddit_proxy, :link_button)`, sub)
7778
return err
7879
}
7980

80-
func (d *DB) UpdateSubscription(webhookID snowflake.ID, postType string, formatType FormatType, roleID snowflake.ID, redditProxy string) error {
81-
_, err := d.dbx.Exec(`UPDATE subscriptions SET type = $1, format_type = $2, role_id = $3, reddit_proxy = $4 WHERE webhook_id = $5`, postType, formatType, roleID, redditProxy, webhookID)
81+
func (d *DB) UpdateSubscription(webhookID snowflake.ID, postType string, formatType FormatType, roleID snowflake.ID, redditProxy string, linkButton bool) error {
82+
_, err := d.dbx.Exec(`UPDATE subscriptions SET type = $1, format_type = $2, role_id = $3, reddit_proxy = $4, link_button = $5 WHERE webhook_id = $6`, postType, formatType, roleID, redditProxy, linkButton, webhookID)
8283
return err
8384
}
8485

redditbot/discord.go

+25-4
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,12 @@ var Commands = []discord.ApplicationCommandCreate{
8080
},
8181
discord.ApplicationCommandOptionString{
8282
Name: "proxy",
83-
Description: "the proxy to use for the post links such as: https://rxddit.com",
83+
Description: "the proxy to use for the post links such as: https://rxddit.com or https://vxreddit.com",
84+
Required: false,
85+
},
86+
discord.ApplicationCommandOptionBool{
87+
Name: "link-button",
88+
Description: "whether to include a link button in the post (default: true)",
8489
Required: false,
8590
},
8691
},
@@ -116,6 +121,11 @@ var Commands = []discord.ApplicationCommandCreate{
116121
Description: "the proxy to use for the post links such as: https://rxddit.com",
117122
Required: false,
118123
},
124+
discord.ApplicationCommandOptionBool{
125+
Name: "link-button",
126+
Description: "whether to include a link button in the post (default: true)",
127+
Required: false,
128+
},
119129
},
120130
},
121131
discord.ApplicationCommandOptionSubCommand{
@@ -180,6 +190,10 @@ func (b *Bot) OnSubredditAdd(data discord.SlashCommandInteractionData, event *ev
180190
}
181191
roleID := data.Snowflake("role")
182192
proxy := data.String("proxy")
193+
linkButton, ok := data.OptBool("link-button")
194+
if !ok {
195+
linkButton = true
196+
}
183197

184198
ok, err := b.db.HasSubscriptionByGuildSubreddit(*event.GuildID(), subreddit)
185199
if err != nil {
@@ -215,6 +229,7 @@ func (b *Bot) OnSubredditAdd(data discord.SlashCommandInteractionData, event *ev
215229
FormatType: FormatType(formatType),
216230
RoleID: roleID,
217231
RedditProxy: proxy,
232+
LinkButton: linkButton,
218233
Interaction: event.ApplicationCommandInteraction,
219234
}
220235
_ = event.CreateMessage(discord.MessageCreate{
@@ -251,14 +266,15 @@ func (b *Bot) OnSubredditAdd(data discord.SlashCommandInteractionData, event *ev
251266
return
252267
}
253268

254-
if err = b.db.AddSubscription(Subscription{
269+
if err = b.AddSubscription(Subscription{
255270
Subreddit: subreddit,
256271
Type: postType,
257272
FormatType: FormatType(formatType),
258273
GuildID: *event.GuildID(),
259274
ChannelID: event.Channel().ID(),
260275
WebhookID: webhook.ID(),
261276
WebhookToken: webhook.Token,
277+
LinkButton: linkButton,
262278
}); err != nil {
263279
_ = event.CreateMessage(discord.MessageCreate{
264280
Content: "Failed to save subscription to the database: " + err.Error(),
@@ -278,6 +294,7 @@ func (b *Bot) OnSubredditUpdate(data discord.SlashCommandInteractionData, event
278294
formatType := FormatType(data.String("format-type"))
279295
roleID := data.Snowflake("role")
280296
proxy := data.String("proxy")
297+
linkButton, linkButtonOk := data.OptBool("link-button")
281298

282299
sub, err := b.db.GetSubscriptionsByGuildSubreddit(*event.GuildID(), subreddit)
283300
if errors.Is(err, ErrSubscriptionNotFound) {
@@ -307,8 +324,11 @@ func (b *Bot) OnSubredditUpdate(data discord.SlashCommandInteractionData, event
307324
if proxy == "" {
308325
proxy = sub.RedditProxy
309326
}
327+
if !linkButtonOk {
328+
linkButton = sub.LinkButton
329+
}
310330

311-
if err = b.db.UpdateSubscription(sub.WebhookID, postType, formatType, roleID, proxy); err != nil {
331+
if err = b.db.UpdateSubscription(sub.WebhookID, postType, formatType, roleID, proxy, linkButton); err != nil {
312332
_ = event.CreateMessage(discord.MessageCreate{
313333
Content: "Failed to update subscription: " + err.Error(),
314334
Flags: discord.MessageFlagEphemeral,
@@ -428,7 +448,7 @@ func (b *Bot) OnDiscordCallback(w http.ResponseWriter, r *http.Request) {
428448
webhookID := snowflake.MustParse(wh["id"].(string))
429449
webhookToken := wh["token"].(string)
430450

431-
if err = b.db.AddSubscription(Subscription{
451+
if err = b.AddSubscription(Subscription{
432452
Subreddit: setupState.Subreddit,
433453
Type: setupState.PostType,
434454
FormatType: setupState.FormatType,
@@ -438,6 +458,7 @@ func (b *Bot) OnDiscordCallback(w http.ResponseWriter, r *http.Request) {
438458
WebhookToken: webhookToken,
439459
RoleID: setupState.RoleID,
440460
RedditProxy: setupState.RedditProxy,
461+
LinkButton: setupState.LinkButton,
441462
}); err != nil {
442463
_, _ = b.Client.Rest().UpdateInteractionResponse(setupState.Interaction.ApplicationID(), setupState.Interaction.Token(), discord.MessageUpdate{
443464
Content: json.Ptr("Failed to save subscription to the database: " + err.Error()),

redditbot/subscriptions.go

+9-1
Original file line numberDiff line numberDiff line change
@@ -211,7 +211,7 @@ func (b *Bot) sendPost(sub Subscription, post RedditPost) bool {
211211
proxy = sub.RedditProxy
212212
}
213213
webhookMessageCreate = discord.WebhookMessageCreate{
214-
Content: fmt.Sprintf("[%s](%s%s)", post.Title, proxy, post.Permalink),
214+
Content: fmt.Sprintf("New [post](%s%s) in [`%s`](<%s>)", proxy, post.Permalink, post.SubredditNamePrefixed, "https://reddit.com/"+post.SubredditNamePrefixed),
215215
}
216216
}
217217

@@ -236,6 +236,14 @@ func (b *Bot) sendPost(sub Subscription, post RedditPost) bool {
236236
webhookMessageCreate.AllowedMentions = &allowedMentions
237237
}
238238

239+
if sub.LinkButton {
240+
webhookMessageCreate.Components = []discord.ContainerComponent{
241+
discord.ActionRowComponent{
242+
discord.NewLinkButton("Open Post", "https://reddit.com"+post.Permalink),
243+
},
244+
}
245+
}
246+
239247
postsSent.With(prometheus.Labels{
240248
"subreddit": sub.Subreddit,
241249
"type": sub.Type,

sql/schema.sql

+1
Original file line numberDiff line numberDiff line change
@@ -10,5 +10,6 @@ CREATE TABLE IF NOT EXISTS subscriptions
1010
last_post TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP,
1111
role_id BIGINT NOT NULL DEFAULT 0,
1212
reddit_proxy VARCHAR NOT NULL DEFAULT '',
13+
link_button BOOLEAN NOT NULL DEFAULT TRUE,
1314
PRIMARY KEY (subreddit, guild_id)
1415
)

0 commit comments

Comments
 (0)