2023-06-15 23:29:07 -04:00
namespace vassago.Behavior ;
using System ;
using System.Collections.Generic ;
using System.IO ;
using System.Linq ;
using System.Threading ;
using System.Threading.Tasks ;
using Newtonsoft.Json ;
using vassago.Models ;
using QRCoder ;
2023-06-20 21:26:44 -04:00
[StaticPlz]
2023-06-15 23:29:07 -04:00
public class QRify : Behavior
{
public override string Name = > "qr-ify" ;
public override string Trigger = > "!qrplz" ;
public override string Description = > "generate text QR codes" ;
2023-07-04 13:31:19 -04:00
public override bool ShouldAct ( Message message )
{
if ( message . Channel . EffectivePermissions . MaxAttachmentBytes < 1024 )
return false ;
return base . ShouldAct ( message ) ;
}
2023-06-19 11:03:06 -04:00
public override async Task < bool > ActOn ( Message message )
2023-06-15 23:29:07 -04:00
{
var qrContent = message . Content . Substring ( $"{Trigger} " . Length + message . Content . IndexOf ( Trigger ) ) ;
Console . WriteLine ( $"qring: {qrContent}" ) ;
QRCodeGenerator qrGenerator = new QRCodeGenerator ( ) ;
QRCodeData qrCodeData = qrGenerator . CreateQrCode ( qrContent , QRCodeGenerator . ECCLevel . Q ) ;
SvgQRCode qrCode = new SvgQRCode ( qrCodeData ) ;
string qrCodeAsSvg = qrCode . GetGraphic ( 20 ) ;
int todaysnumber = Shared . r . Next ( ) ;
if ( ! Directory . Exists ( "tmp" ) )
{
Directory . CreateDirectory ( "tmp" ) ;
}
File . WriteAllText ( $"tmp/qr{todaysnumber}.svg" , qrCodeAsSvg ) ;
if ( ExternalProcess . GoPlz ( "convert" , $"tmp/qr{todaysnumber}.svg tmp/qr{todaysnumber}.png" ) )
{
2024-01-10 21:21:31 -05:00
if ( message . Channel . EffectivePermissions . MaxAttachmentBytes > = ( ulong ) ( new System . IO . FileInfo ( $"tmp/qr{todaysnumber}.png" ) . Length ) )
2023-07-04 13:31:19 -04:00
await message . Channel . SendFile ( $"tmp/qr{todaysnumber}.png" , null ) ;
else
2024-01-10 21:21:31 -05:00
await message . Channel . SendMessage ( $"resulting qr image 2 big 4 here ({(ulong)(new System.IO.FileInfo($" tmp / qr { todaysnumber } . png ").Length)} / {message.Channel.EffectivePermissions.MaxAttachmentBytes})" ) ;
2023-06-15 23:29:07 -04:00
File . Delete ( $"tmp/qr{todaysnumber}.svg" ) ;
File . Delete ( $"tmp/qr{todaysnumber}.png" ) ;
}
else
{
await message . Channel . SendMessage ( "convert failed :( aaaaaaadam!" ) ;
Console . Error . WriteLine ( $"convert failed :( qr{todaysnumber}" ) ;
return false ;
}
return true ;
}
}