Skip to content

Commit

Permalink
silly lil thing
Browse files Browse the repository at this point in the history
  • Loading branch information
Szedann committed Apr 7, 2024
1 parent 00e62b4 commit 077a794
Show file tree
Hide file tree
Showing 4 changed files with 55 additions and 4 deletions.
Binary file added src/assets/numismatics_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added src/assets/railways_icon.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
57 changes: 53 additions & 4 deletions src/handlers/dynamicIcon.handler.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,62 @@
import { Handler } from '..';
import { createCanvas, loadImage } from 'canvas';
import { PNGStream, createCanvas, loadImage } from 'canvas';

async function stream2buffer(stream: PNGStream): Promise<Buffer> {
return new Promise<Buffer>((resolve, reject) => {
// eslint-disable-next-line @typescript-eslint/no-explicit-any
const _buf = Array<any>();

stream.on('data', (chunk) => _buf.push(chunk));
stream.on('end', () => resolve(Buffer.concat(_buf)));
stream.on('error', (err) => reject(`error converting stream - ${err}`));
});
}

export const dynamicIconHandler: Handler = (client) => {
client.on('messageCreate', (message) => {
if (message.content == 'icons') {
const startTime = 1712518661_000;
const endTime = 1712595600_000;
const totalTime = endTime - startTime;
let timeout: NodeJS.Timeout | undefined = undefined;
if (Date.now() > endTime) return;
client.on('messageCreate', async (message) => {
if (
message.content != 'start numismatics icon thingy please' &&
message.member?.permissions.has('Administrator')
) {
message.reply('nuh uh');
return;
}
if (timeout) {
message.reply('it already started silly');
return;
}
const updateIcon = async () => {
const currentTime = Date.now() - startTime;
const currentProgress = currentTime / totalTime;
console.log(currentProgress);
message.guild?.setIcon(
await stream2buffer(await generateIcon(currentProgress / 2))
);
};
updateIcon();
timeout = setInterval(updateIcon, 1800_000);
});
};

function generateIcon(coverage: number) {
async function generateIcon(progress: number) {
const canvas = createCanvas(256, 256);
const ctx = canvas.getContext('2d');
ctx.drawImage(
await loadImage(__dirname + '/../assets/railways_icon.png'),
0,
0
);
const x = progress * canvas.width * 2 - canvas.width;
const y = -(Math.sin(progress * Math.PI) * 256 - 256);
ctx.drawImage(
await loadImage(__dirname + '/../assets/numismatics_icon.png'),
x,
y
);
return canvas.createPNGStream({});
}
2 changes: 2 additions & 0 deletions src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ import { buttonHandler } from './handlers/button.handler';
import textCommandHandler from './handlers/textCommand.handler';
import { modalHandler } from './handlers/modal.handler';
import { ChannelType } from 'discord-api-types/v10';
import { dynamicIconHandler } from './handlers/dynamicIcon.handler';
// import { spamHandler } from './handlers/spam.handler';

export const client = new Client({
Expand Down Expand Up @@ -118,6 +119,7 @@ const handlers: Handler[] = [
logHandler,
buttonHandler,
modalHandler,
dynamicIconHandler,
// spamHandler,
];

Expand Down

0 comments on commit 077a794

Please sign in to comment.