Skip to content

Commit

Permalink
get rid of the remaining deprecated authorids
Browse files Browse the repository at this point in the history
  • Loading branch information
Techbot121 authored and Meta Construct committed Sep 30, 2024
1 parent a0851c5 commit 85c7008
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 27 deletions.
31 changes: 10 additions & 21 deletions app/services/Markov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,18 @@ import { Service } from ".";
interface ILearnData {
timestamp?: number;
message: string;
authorID?: string;
authorName: string;
}

export interface IGenerateOptions {
depth?: number;
length?: number;
authorID?: string;
continuation?: boolean;
}

abstract class MarkovChainBase {
abstract learn(data: ILearnData): Promise<void>;
abstract queryDB(chain: string[], authorID?: string): Promise<ILearnData | null>;
abstract queryDB(chain: string[]): Promise<ILearnData | null>;

private getWords(sentence: string) {
if (sentence.match(/^\s*$/)) {
Expand Down Expand Up @@ -89,7 +87,6 @@ abstract class MarkovChainBase {
depth = 4,
maxLength = 50,
sentence = "",
authorID = "",
continuation = true,
callback?: (word: string) => void
): Promise<string | undefined> {
Expand All @@ -111,7 +108,7 @@ abstract class MarkovChainBase {
const startCount = out.length;

while (out.length < maxLength) {
const data = await this.queryDB(chain, authorID);
const data = await this.queryDB(chain);

if (!data || !data.message) {
break;
Expand Down Expand Up @@ -180,28 +177,21 @@ class MarkovChain extends MarkovChainBase {
});
}

queryDB(chain: string[], authorID: string): Promise<ILearnData | null> {
queryDB(chain: string[]): Promise<ILearnData | null> {
return new Promise((resolve, reject) => {
const sentence = chain.join(" ");

if (sentence.trim() === "") {
this.db.get(
`SELECT * FROM markov ${
authorID ? `WHERE authorID = ${authorID}` : ""
} ORDER BY RANDOM() LIMIT 1`,
(err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
this.db.get(`SELECT * FROM markov ORDER BY RANDOM() LIMIT 1`, (err, res) => {
if (err) {
reject(err);
} else {
resolve(res);
}
);
});
} else {
this.db.all(
`SELECT * FROM markov WHERE ${
authorID ? `authorID = ${authorID} AND` : ""
}(message LIKE $sentence1 OR [message] Like $sentence3 ) ORDER BY RANDOM() LIMIT 1`,
`SELECT * FROM markov WHERE (message LIKE $sentence1 OR [message] Like $sentence3 ) ORDER BY RANDOM() LIMIT 1`,
{
$sentence1: `_% ${sentence} %_`,
// $sentence2: `% ${sentence}`,
Expand Down Expand Up @@ -240,7 +230,6 @@ export class Markov extends Service {
options?.depth,
options?.length,
sentence,
options?.authorID,
options?.continuation
);
} catch (err) {
Expand Down
6 changes: 0 additions & 6 deletions app/services/discord/modules/commands/Markov.ts
Original file line number Diff line number Diff line change
Expand Up @@ -29,11 +29,6 @@ export const SlashMarkovCommand: SlashCommand = {
min_value: 1,
max_value: 50,
},
{
name: "user",
description: "does exactly what you think it does.",
type: Discord.ApplicationCommandOptionType.User,
},
{
name: "continuation",
description: "should it include your sentence in the response?",
Expand All @@ -49,7 +44,6 @@ export const SlashMarkovCommand: SlashCommand = {
?.generate(ctx.options.getString("sentence") ?? undefined, {
depth: ctx.options.getInteger("insanity") ?? undefined,
length: ctx.options.getInteger("length") ?? undefined,
authorID: ctx.options.getUser("user")?.id,
continuation: ctx.options.getBoolean("continuation") ?? undefined,
});

Expand Down

0 comments on commit 85c7008

Please sign in to comment.