feat: add expiry to create invite command
This commit is contained in:
parent
00af303555
commit
a34b5479c0
1 changed files with 59 additions and 6 deletions
|
|
@ -43,6 +43,7 @@ public class InviteCommands(
|
||||||
InviteRepository inviteRepository,
|
InviteRepository inviteRepository,
|
||||||
GuildCache guildCache,
|
GuildCache guildCache,
|
||||||
IInviteCache inviteCache,
|
IInviteCache inviteCache,
|
||||||
|
UserCache userCache,
|
||||||
IDiscordRestChannelAPI channelApi,
|
IDiscordRestChannelAPI channelApi,
|
||||||
IDiscordRestGuildAPI guildApi,
|
IDiscordRestGuildAPI guildApi,
|
||||||
FeedbackService feedbackService,
|
FeedbackService feedbackService,
|
||||||
|
|
@ -113,21 +114,22 @@ public class InviteCommands(
|
||||||
);
|
);
|
||||||
|
|
||||||
[Command("create")]
|
[Command("create")]
|
||||||
[Description("Create a new invite.`")]
|
[Description("Create a new invite.")]
|
||||||
public async Task<IResult> CreateInviteAsync(
|
public async Task<IResult> CreateInviteAsync(
|
||||||
[Description("The channel to create the invite in")]
|
[Description("The channel to create the invite in")]
|
||||||
[ChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement)]
|
[ChannelTypes(ChannelType.GuildText, ChannelType.GuildAnnouncement)]
|
||||||
IChannel channel,
|
IChannel channel,
|
||||||
[Description("What to name the new invite")] string? name = null
|
[Description("What to name the new invite")] string? name = null,
|
||||||
|
[Description("How long the invite should be valid for")] InviteDuration? duration = null
|
||||||
)
|
)
|
||||||
{
|
{
|
||||||
var (userId, guildId) = contextInjection.GetUserAndGuild();
|
var (userId, guildId) = contextInjection.GetUserAndGuild();
|
||||||
|
|
||||||
var inviteResult = await channelApi.CreateChannelInviteAsync(
|
var inviteResult = await channelApi.CreateChannelInviteAsync(
|
||||||
channel.ID,
|
channel.ID,
|
||||||
maxAge: TimeSpan.Zero,
|
maxAge: duration?.ToTimespan() ?? TimeSpan.Zero,
|
||||||
isUnique: true,
|
isUnique: true,
|
||||||
reason: $"Create invite command by {userId}"
|
reason: $"Create invite command by {await userCache.TryFormatUserAsync(userId, addMention: false)}"
|
||||||
);
|
);
|
||||||
if (inviteResult.Error != null)
|
if (inviteResult.Error != null)
|
||||||
{
|
{
|
||||||
|
|
@ -144,17 +146,20 @@ public class InviteCommands(
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var durationText =
|
||||||
|
duration != null ? $"\nThis invite {duration.ToHumanString()}." : string.Empty;
|
||||||
|
|
||||||
if (name == null)
|
if (name == null)
|
||||||
return await feedbackService.ReplyAsync(
|
return await feedbackService.ReplyAsync(
|
||||||
$"Created a new invite in <#{channel.ID}>!"
|
$"Created a new invite in <#{channel.ID}>!"
|
||||||
+ $"\nLink: https://discord.gg/{inviteResult.Entity.Code}"
|
+ $"\nLink: https://discord.gg/{inviteResult.Entity.Code}{durationText}"
|
||||||
);
|
);
|
||||||
|
|
||||||
await inviteRepository.SetInviteNameAsync(guildId, inviteResult.Entity.Code, name);
|
await inviteRepository.SetInviteNameAsync(guildId, inviteResult.Entity.Code, name);
|
||||||
|
|
||||||
return await feedbackService.ReplyAsync(
|
return await feedbackService.ReplyAsync(
|
||||||
$"Created a new invite in <#{channel.ID}> with the name **{name}**!"
|
$"Created a new invite in <#{channel.ID}> with the name **{name}**!"
|
||||||
+ $"\nLink: https://discord.gg/{inviteResult.Entity.Code}"
|
+ $"\nLink: https://discord.gg/{inviteResult.Entity.Code}{durationText}"
|
||||||
);
|
);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -253,3 +258,51 @@ public class InviteAutocompleteProvider(
|
||||||
.ToList();
|
.ToList();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public enum InviteDuration
|
||||||
|
{
|
||||||
|
[Description("30 minutes")]
|
||||||
|
ThirtyMinutes,
|
||||||
|
|
||||||
|
[Description("1 hour")]
|
||||||
|
OneHour,
|
||||||
|
|
||||||
|
[Description("6 hours")]
|
||||||
|
SixHours,
|
||||||
|
|
||||||
|
[Description("12 hours")]
|
||||||
|
TwelveHours,
|
||||||
|
|
||||||
|
[Description("1 day")]
|
||||||
|
OneDay,
|
||||||
|
|
||||||
|
[Description("1 week")]
|
||||||
|
OneWeek,
|
||||||
|
}
|
||||||
|
|
||||||
|
internal static class InviteEnumExtensions
|
||||||
|
{
|
||||||
|
internal static TimeSpan ToTimespan(this InviteDuration dur) =>
|
||||||
|
dur switch
|
||||||
|
{
|
||||||
|
InviteDuration.ThirtyMinutes => TimeSpan.FromMinutes(30),
|
||||||
|
InviteDuration.OneHour => TimeSpan.FromHours(1),
|
||||||
|
InviteDuration.SixHours => TimeSpan.FromHours(6),
|
||||||
|
InviteDuration.TwelveHours => TimeSpan.FromHours(12),
|
||||||
|
InviteDuration.OneDay => TimeSpan.FromDays(1),
|
||||||
|
InviteDuration.OneWeek => TimeSpan.FromDays(7),
|
||||||
|
_ => TimeSpan.Zero,
|
||||||
|
};
|
||||||
|
|
||||||
|
internal static string ToHumanString(this InviteDuration? dur) =>
|
||||||
|
dur switch
|
||||||
|
{
|
||||||
|
InviteDuration.ThirtyMinutes => "expires after 30 minutes",
|
||||||
|
InviteDuration.OneHour => "expires after 1 hour",
|
||||||
|
InviteDuration.SixHours => "expires after 6 hours",
|
||||||
|
InviteDuration.TwelveHours => "expires after 12 hours",
|
||||||
|
InviteDuration.OneDay => "expires after 1 day",
|
||||||
|
InviteDuration.OneWeek => "expires after 1 week",
|
||||||
|
_ => "does not expire",
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue