Compare commits

...

2 Commits

Author SHA1 Message Date
Hri7566 f6892a691a Add fid command 2024-03-20 04:37:56 -04:00
Hri7566 c645229cca Add channel parameter to requests 2024-03-20 04:37:47 -04:00
11 changed files with 73 additions and 23 deletions

View File

@ -15,7 +15,7 @@ interface FishingContext {
token: string | null;
}
interface AuthedFishingContext extends FishingContext {
interface PrivateFishingContext extends FishingContext {
token: string;
}
@ -43,7 +43,7 @@ export const privateProcedure = publicProcedure.use(async opts => {
if (!ctx.isAuthed) throw new TRPCError({ code: "UNAUTHORIZED" });
return opts.next({
ctx: opts.ctx as AuthedFishingContext
ctx: opts.ctx as PrivateFishingContext
});
});
@ -55,6 +55,7 @@ export const appRouter = router({
command: privateProcedure
.input(
z.object({
channel: z.string(),
command: z.string(),
prefix: z.string(),
args: z.array(z.string()),
@ -68,9 +69,10 @@ export const appRouter = router({
)
.query(async opts => {
const id = tokenToID(opts.ctx.token);
const { command, args, prefix, user, isDM } = opts.input;
const { channel, command, args, prefix, user, isDM } = opts.input;
const out = await handleCommand(
id,
channel,
command,
args,
prefix,

View File

@ -7,11 +7,11 @@ export const fish = new Command(
"Send your LURE into a water for catching fish",
"fish",
"command.fishing.fish",
async ({ id, command, args, prefix, part, user, isDM }) => {
async ({ id, channel, command, args, prefix, part, user, isDM }) => {
const fishing = getFishing(id, part.id);
if (!fishing) {
startFishing(id, part.id, isDM);
startFishing(id, part.id, channel, isDM);
return `Our friend ${part.name} casts LURE into a water for catching fish.`;
} else {
return `Your lure is already in the water (since ${(

View File

@ -21,6 +21,7 @@ import { myid } from "./general/myid";
import { yeet } from "./inventory/yeet";
import { tree } from "./fishing/tree";
import { pick } from "./fishing/pick";
import { fid } from "./util/fid";
// import { give } from "./inventory/give";
interface ICommandGroup {
@ -58,7 +59,7 @@ commandGroups.push(inventoryGroup);
const utilGroup: ICommandGroup = {
id: "util",
displayName: "Utility",
commands: [data, setcolor, memory, autofish, pokedex]
commands: [data, setcolor, memory, autofish, pokedex, fid]
};
commandGroups.push(utilGroup);

View File

@ -0,0 +1,13 @@
import Command from "@server/commands/Command";
export const fid = new Command(
"fid",
["fid"],
"Get internal ID",
"fid",
"command.util.fid",
async props => {
return props.id;
},
false
);

View File

@ -10,6 +10,7 @@ export const logger = new Logger("Command Handler");
export async function handleCommand(
id: string,
channel: string,
command: string,
args: string[],
prefix: string,
@ -62,6 +63,7 @@ export async function handleCommand(
try {
const response = await foundCommand.callback({
id,
channel,
command,
args,
prefix,

View File

@ -29,14 +29,14 @@ export async function tick() {
const user = await getUser(winner.userID);
if (!user) {
stopFishing(winner.id, winner.userID, false);
stopFishing(winner.id, winner.userID, winner.channel, false);
return;
}
const inventory = await getInventory(user.inventoryId);
if (!inventory) {
stopFishing(winner.id, winner.userID, false);
stopFishing(winner.id, winner.userID, winner.channel, false);
return;
}
@ -49,6 +49,7 @@ export async function tick() {
stopFishing(
winner.id,
winner.userID,
winner.channel,
winner.autofish,
winner.autofish_t
);
@ -58,6 +59,7 @@ export async function tick() {
const size = getSizeString(animal.size);
addBack(winner.id, {
m: "sendchat",
channel: winner.channel,
message: `Our good friend @${user.id} caught a ${size} ${
animal.emoji || "🐟"
}${animal.name}! ready to ${prefixes[0]}eat or ${
@ -87,6 +89,7 @@ export function stopFisherTick() {
export function startFishing(
id: string,
userID: string,
channel: string,
isDM: boolean = false,
autofish: boolean = false,
autofish_t: number = Date.now()
@ -94,6 +97,7 @@ export function startFishing(
fishers[id + "~" + userID] = {
id,
userID,
channel,
t: Date.now(),
isDM,
autofish,
@ -104,6 +108,7 @@ export function startFishing(
export function stopFishing(
id: string,
userID: string,
channel: string,
autofish: boolean = false,
autofish_t: number = Date.now()
) {
@ -120,6 +125,7 @@ export function stopFishing(
1000 /
60
).toFixed(2)} minutes.`,
channel: fisher.channel,
isDM: fisher.isDM,
id: fisher.userID
});
@ -127,7 +133,7 @@ export function stopFishing(
}
if (autofish) {
startFishing(id, userID, true, true, autofish_t);
startFishing(id, userID, channel, true, true, autofish_t);
}
}

View File

@ -54,6 +54,7 @@ rl.on("line", async line => {
const args = msg.a.split(" ");
const command = await trpc.command.query({
channel: logger.id,
args: args.slice(1, args.length),
command: args[0].substring(usedPrefix.length),
prefix: usedPrefix,
@ -77,6 +78,10 @@ setInterval(async () => {
// this.logger.debug(backs);
for (const back of backs) {
if (typeof back.m !== "string") return;
if (typeof back.channel === "string") {
if (back.channel !== logger.id) return;
}
b.emit(back.m, back);
}
}

View File

@ -10,8 +10,6 @@ export interface DiscordBotConfig {
token?: string;
}
const trpc = gettRPC(process.env.DISCORD_FISHING_TOKEN as string);
export class DiscordBot extends EventEmitter {
public client: Discord.Client;
public logger = new Logger("Discord Bot");
@ -19,6 +17,7 @@ export class DiscordBot extends EventEmitter {
public server?: Discord.Guild;
public defaultChannel?: Discord.TextChannel;
public b = new EventEmitter();
public trpc = gettRPC(process.env.DISCORD_FISHING_TOKEN as string);
constructor(public conf: DiscordBotConfig) {
super();
@ -95,7 +94,7 @@ export class DiscordBot extends EventEmitter {
let prefixes: string[];
try {
prefixes = await trpc.prefixes.query();
prefixes = await this.trpc.prefixes.query();
} catch (err) {
this.logger.error(err);
this.logger.warn("Unable to contact server");
@ -110,7 +109,8 @@ export class DiscordBot extends EventEmitter {
const args = msg.content.split(" ");
const command = await trpc.command.query({
const command = await this.trpc.command.query({
channel: msg.channelId,
args: args.slice(1, args.length),
command: args[0].substring(usedPrefix.length),
prefix: usedPrefix,
@ -132,7 +132,8 @@ export class DiscordBot extends EventEmitter {
setInterval(async () => {
try {
const backs = (await trpc.backs.query()) as IBack<unknown>[];
const backs =
(await this.trpc.backs.query()) as IBack<unknown>[];
if (backs.length > 0) {
// this.logger.debug(backs);
for (const back of backs) {
@ -183,6 +184,11 @@ export class DiscordBot extends EventEmitter {
this.b.on("sendchat", msg => {
// this.logger.debug("sendchat message:", msg);
if (!this.defaultChannel) return;
if (typeof msg.channel === "string") {
if (msg.channel !== this.defaultChannel.id) return;
}
this.defaultChannel.send(
msg.message.split(`@${msg.id}`).join(`<@${msg.id}>`)
);

View File

@ -12,12 +12,12 @@ export interface MPPNetBotConfig {
};
}
const trpc = gettRPC(process.env.MPP_FISHING_TOKEN as string);
export class MPPNetBot {
public client: Client;
public b = new EventEmitter();
public logger: Logger;
public trpc = gettRPC(process.env.MPP_FISHING_TOKEN as string);
public started = false;
constructor(
public config: MPPNetBotConfig,
@ -32,11 +32,14 @@ export class MPPNetBot {
}
public start() {
this.logger.debug("Starting");
this.client.start();
this.started = true;
}
public stop() {
this.client.stop();
this.started = false;
}
public bindEventListeners() {
@ -54,7 +57,7 @@ export class MPPNetBot {
let prefixes: string[];
try {
prefixes = await trpc.prefixes.query();
prefixes = await this.trpc.prefixes.query();
} catch (err) {
this.logger.error(err);
this.logger.warn("Unable to contact server");
@ -69,7 +72,8 @@ export class MPPNetBot {
const args = msg.a.split(" ");
const command = await trpc.command.query({
const command = await this.trpc.command.query({
channel: this.client.channel._id,
args: args.slice(1, args.length),
command: args[0].substring(usedPrefix.length),
prefix: usedPrefix,
@ -120,7 +124,7 @@ export class MPPNetBot {
let prefixes: string[];
try {
prefixes = await trpc.prefixes.query();
prefixes = await this.trpc.prefixes.query();
} catch (err) {
this.logger.error(err);
this.logger.warn("Unable to contact server");
@ -135,7 +139,8 @@ export class MPPNetBot {
const args = msg.a.split(" ");
const command = await trpc.command.query({
const command = await this.trpc.command.query({
channel: this.client.channel._id,
args: args.slice(1, args.length),
command: args[0].substring(usedPrefix.length),
prefix: usedPrefix,
@ -155,7 +160,8 @@ export class MPPNetBot {
setInterval(async () => {
try {
const backs = (await trpc.backs.query()) as IBack<unknown>[];
const backs =
(await this.trpc.backs.query()) as IBack<unknown>[];
if (backs.length > 0) {
// this.logger.debug(backs);
for (const back of backs) {
@ -182,6 +188,11 @@ export class MPPNetBot {
this.b.on("sendchat", msg => {
// this.logger.debug("sendchat message:", msg);
if (typeof msg.channel === "string") {
if (msg.channel !== this.client.channel._id) return;
}
if (msg.isDM) {
this.sendDM(msg.message, msg.id);
} else {

View File

@ -1,7 +1,10 @@
import { loadConfig } from "@util/config";
import { MPPNetBot, type MPPNetBotConfig } from "./Bot";
import { Logger } from "@util/Logger";
const bots = [];
const logger = new Logger("big brain");
const bots: MPPNetBot[] = [];
const defaults = loadConfig("config/bots.yml", [
{
@ -26,5 +29,4 @@ export function initBot(conf: MPPNetBotConfig) {
}
export { MPPNetBot as Bot };
export default MPPNetBot;

2
src/util/types.d.ts vendored
View File

@ -10,6 +10,7 @@ interface ICommandResponse {
interface IContextProps {
id: string;
channel: string;
command: string;
args: string[];
prefix: string;
@ -106,6 +107,7 @@ interface ILocation {
interface TFisher {
id: string;
userID: string;
channel: string;
t: number;
isDM: boolean;
autofish: boolean;