Skip to content

Commit

Permalink
Improve sendEmbed
Browse files Browse the repository at this point in the history
Signed-off-by: shedaniel <daniel@shedaniel.me>
  • Loading branch information
shedaniel committed Feb 14, 2022
1 parent 95d2c13 commit d85707b
Show file tree
Hide file tree
Showing 25 changed files with 142 additions and 142 deletions.
11 changes: 10 additions & 1 deletion linkie.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,13 +14,22 @@ declare namespace Linkie {
}

interface Channel {
sendEmbed(content: string): void;
sendEmbed(content: string | ((spec: EmbedSpec) => void)): void;
sendEmbed(title: string, content: string): void;
sendMessage(content: string): void;
id: string;
mention: string;
}

interface EmbedSpec {
title: string;
description: string;
url: string;
color: string;
image: string;
thumbnail: string;
}

interface User {
username: string;
discriminator: string;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ open class CommandManager(
if (slashCommandsList.size >= 100) {
throw IllegalArgumentException("Too many slash commands registered!")
}
println("Registered ${slashCommandsList.count()} slash commands")
this.slashCommands.forEach { cmd ->
if (cmd.slashCommand != null) {
slashCommands.globalCommand(cmd.slashCommand)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ class SlashCommands(
private val throwableHandler: ThrowableHandler,
private val errorHandler: (String) -> Unit = { println("Error: $it") },
private val defaultEphemeral: Boolean = false,
private val debug: Boolean = false,
) {
val applicationId: Long by lazy { client.restClient.applicationId.block() }
val handlers = mutableMapOf<String, SlashCommandHandler>()
Expand Down Expand Up @@ -127,6 +128,8 @@ class SlashCommands(
modifyGuildCommand(guild, command, existingCommand.id().toLong(), cmd).subscribe { newData ->
registeredGuildCommands[GuildCommandKey(guild, cmd)] = newData
}
} else if (debug) {
println("Guild (${guild.asString()}) command /$cmd is up to date")
}
return
}
Expand All @@ -139,6 +142,12 @@ class SlashCommands(
modifyGuildCommand(guild, command, data.id().toLong(), cmd).subscribe { newData ->
registeredGuildCommands[GuildCommandKey(guild, cmd)] = newData
}
} else {
if (debug) {
println("Guild (${guild.asString()}) command /$cmd is up to date")
}

registeredGuildCommands[GuildCommandKey(guild, cmd)] = data
}
return
}
Expand Down Expand Up @@ -197,6 +206,8 @@ class SlashCommands(
modifyGlobalCommand(command, existingCommand.id().toLong(), cmd).subscribe { newData ->
registeredCommands[cmd] = newData
}
} else if (debug) {
println("Global command /$cmd is up to date")
}
return
}
Expand All @@ -209,6 +220,12 @@ class SlashCommands(
modifyGlobalCommand(command, data.id().toLong(), cmd).subscribe { newData ->
registeredCommands[cmd] = newData
}
} else {
if (debug) {
println("Global command /$cmd is up to date")
}

registeredCommands[cmd] = data
}
return
}
Expand Down
37 changes: 10 additions & 27 deletions src/main/kotlin/me/shedaniel/linkie/discord/LinkieBot.kt
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,8 @@
package me.shedaniel.linkie.discord

import com.soywiz.klock.TimeSpan
import discord4j.core.`object`.entity.channel.TextChannel
import com.soywiz.klock.minutes
import com.soywiz.klock.seconds
import discord4j.core.`object`.entity.channel.ThreadChannel
import discord4j.core.event.domain.message.MessageCreateEvent
import discord4j.core.event.domain.thread.ThreadMembersUpdateEvent
Expand Down Expand Up @@ -68,10 +69,6 @@ import me.shedaniel.linkie.discord.handler.CommandHandler
import me.shedaniel.linkie.discord.handler.CommandManager
import me.shedaniel.linkie.discord.scommands.SlashCommands
import me.shedaniel.linkie.discord.scommands.sub
import me.shedaniel.linkie.discord.scripting.ContextExtensions
import me.shedaniel.linkie.discord.scripting.EvalContext
import me.shedaniel.linkie.discord.scripting.LinkieScripting
import me.shedaniel.linkie.discord.scripting.push
import me.shedaniel.linkie.discord.tricks.TricksManager
import me.shedaniel.linkie.discord.utils.CommandContext
import me.shedaniel.linkie.discord.utils.discriminatedName
Expand All @@ -89,6 +86,7 @@ import me.shedaniel.linkie.namespaces.PlasmaNamespace
import me.shedaniel.linkie.namespaces.YarnNamespace
import me.shedaniel.linkie.namespaces.YarrnNamespace
import me.shedaniel.linkie.utils.getMillis
import me.shedaniel.linkie.utils.info
import me.shedaniel.linkie.utils.warn
import java.io.File
import java.util.*
Expand Down Expand Up @@ -133,7 +131,7 @@ fun main() {
)
)
) {
val slashCommands = SlashCommands(this, LinkieThrowableHandler, ::warn)
val slashCommands = SlashCommands(this, LinkieThrowableHandler, ::warn, debug = isDebug)
TricksManager.listen(slashCommands)
val commandManager = object : CommandManager(if (isDebug) "@" else "!") {
override fun getPrefix(event: MessageCreateEvent): String {
Expand All @@ -144,37 +142,17 @@ fun main() {
if (ctx.cmd in regularCommandMap) {
ctx.message.reply("Linkie now only supports slash commands! Please use them instead!")
return true
} else if (true) {
return true
}
if (super.execute(event, ctx, args)) {
return true
}
val trick = TricksManager.globalTricks[ctx.cmd] ?: return false
val evalContext = EvalContext(
ctx,
event.message,
trick.flags,
args,
parent = true,
)
LinkieScripting.evalTrick(evalContext, ctx.message, trick) {
LinkieScripting.simpleContext.push {
ContextExtensions.commandContexts(evalContext, ctx.user, ctx.channel, ctx.message, this)
}
}
return true
}
}
val trickHandler = TrickHandler(if (isDebug) "@@" else "!!")
// register the commands
registerCommands(commandManager)
registerSlashCommands(slashCommands)
CommandHandler(this, commandManager, LinkieThrowableHandler).register()
CommandHandler(this, trickHandler, LinkieThrowableHandler).register()
commandManager.registerToSlashCommands(slashCommands)
gateway.event<ThreadMembersUpdateEvent> { event ->
val dispatch: ThreadMembersUpdate = ThreadMembersUpdateEvent::class.java.getDeclaredField("dispatch").also {
val dispatch: ThreadMembersUpdate = ThreadMembersUpdateEvent::class.java.getDeclaredField("dispatch").also {
it.isAccessible = true
}.get(event) as ThreadMembersUpdate
if (dispatch.addedMembers().any { it.userId().getOrNull()?.asLong() == this.selfId.asLong() }) {
Expand All @@ -189,6 +167,11 @@ fun main() {
}
}
}
cycle(5.minutes, delay = 5.seconds) {
val slashCommandsCount = slashCommands.registeredCommands.size
val guildSlashCommandsList = slashCommands.registeredGuildCommands.size
info("Registered $slashCommandsCount slash commands, $guildSlashCommandsList guild slash commands")
}
}
}

Expand Down
53 changes: 0 additions & 53 deletions src/main/kotlin/me/shedaniel/linkie/discord/TrickHandler.kt

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ object AddTrickCommand : Command {

fun execute(ctx: CommandContext, name: String, args: MutableList<String>) {
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
args.validateUsage(prefix, 1..Int.MAX_VALUE, "$cmd <name> [--script] <trick>")
LinkieScripting.validateTrickName(name)
var type = ContentType.TEXT
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ import kotlin.math.ceil

object ListAllTricksCommand : OptionlessCommand {
override suspend fun execute(ctx: CommandContext) {
LinkieScripting.validateGuild(ctx)
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
val tricks by valueKeeper {
val list = TricksManager.tricks.values.filter { it.guildId == guild.id.asLong() }.sortedBy { it.name }
list to ceil(list.size / 5.0).toInt()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@ object ListTricksCommand : SimpleCommand<User> {
}

override suspend fun execute(ctx: CommandContext, options: User) {
LinkieScripting.validateGuild(ctx)
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
val member = guild.getMemberById(options.id).block() ?: throw NullPointerException("Failed to find member in guild: ${options.discriminatedName}")
val tricks by valueKeeper {
val list = TricksManager.tricks.values.filter { it.guildId == guild.id.asLong() && it.author == member.id.asLong() }.sortedBy { it.name }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ object RemoveTrickCommand : SimpleCommand<String> {
}

override suspend fun execute(ctx: CommandContext, trickName: String) {
LinkieScripting.validateGuild(ctx)
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
LinkieScripting.validateTrickName(trickName)
val trick = TricksManager[trickName to guildId.asLong()] ?: throw NullPointerException("Cannot find trick named `$trickName`")
require(member.canManageTrick(trick)) { "You don't have permission to manage this trick!" }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import me.shedaniel.linkie.discord.scripting.LinkieScripting
import me.shedaniel.linkie.discord.scripting.push
import me.shedaniel.linkie.discord.tricks.TricksManager
import me.shedaniel.linkie.discord.utils.CommandContext
import me.shedaniel.linkie.discord.utils.acknowledge
import me.shedaniel.linkie.discord.utils.validateInGuild

object RunTrickCommand : Command {
Expand All @@ -39,6 +40,7 @@ object RunTrickCommand : Command {

suspend fun execute(ctx: CommandContext, trickName: String, args: MutableList<String>) {
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
val trick = TricksManager[trickName to guildId.asLong()] ?: throw NullPointerException("Cannot find trick named `$trickName`")
val evalContext = EvalContext(
ctx,
Expand All @@ -47,6 +49,7 @@ object RunTrickCommand : Command {
args,
parent = false,
)
message.acknowledge()
LinkieScripting.evalTrick(evalContext, message, trick) {
LinkieScripting.simpleContext.push {
ContextExtensions.commandContexts(evalContext, user, channel, message, this)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ object TrickInfoCommand : SimpleCommand<String> {
}

override suspend fun execute(ctx: CommandContext, trickName: String) {
LinkieScripting.validateGuild(ctx)
ctx.validateInGuild {
LinkieScripting.validateGuild(ctx)
val trick = TricksManager[trickName to guildId.asLong()] ?: throw NullPointerException("Cannot find trick named `$trickName`")
message.reply {
basicEmbed(user)
Expand Down
Loading

0 comments on commit d85707b

Please sign in to comment.