-
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
55 additions
and
4 deletions.
There are no files selected for viewing
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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({}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters