char-slop/city-role-bot
discord auto role applicator bot for charlotte polycule city
git clone https://git.t4t.associates/char-slop/city-role-bot
354c8fb
main
1import { ButtonStyles , ComponentTypes } from 'oceanic.js' 2import { currentColor , parseHex , setMemberColor } from '../colors.ts' 3import { button , guard , modal , slash } from '../rosepack.ts' 4 5const inGuild = [ guard . guild ({ message :'this only works in a server.' })] 6 7const FAILED = 'i could not change your color. ask an admin to check that i have manage roles.' 8 9export const colorModal = modal ({ 10customID :'color/set' , 11title :'pick a name color' , 12guards :inGuild , 13fields :{ 14color :{ 15kind :'text' , 16label :'hex color' , 17placeholder :'#ff66aa' , 18minLength :6 , 19maxLength :7 , 20required :true , 21style :'short' 22} 23}, 24 25async execute ( context ) { 26await context . defer ({ ephemeral :true }) 27const hex = parseHex ( context . values . color ) 28if ( hex === undefined ) { 29await context . reply ( 'that is not a hex color. try something like #ff66aa.' ) 30return 31} 32const { guildID, member} = context . interaction 33try { 34await setMemberColor ( context . client , guildID , member , hex ) 35} catch ( error ) { 36console . error ( `could not set ${ member . id } 's color to ${ hex } in ${ guildID } :` , error ) 37await context . reply ( FAILED ) 38return 39} 40await context . reply ( `your name color is now ${ hex } .` ) 41} 42}) 43 44export const setColorButton = button ({ 45customID :'color/pick' , 46guards :inGuild , 47 48async execute ( context ) { 49const { guildID, member} = context . interaction 50await context . showModal ( colorModal , { values :{ color :currentColor ( guildID , member . roles ) } }) 51} 52}) 53 54export const clearColorButton = button ({ 55customID :'color/clear' , 56guards :inGuild , 57 58async execute ( context ) { 59await context . defer ({ ephemeral :true }) 60const { guildID, member} = context . interaction 61try { 62await setMemberColor ( context . client , guildID , member , undefined ) 63} catch ( error ) { 64console . error ( `could not clear ${ member . id } 's color in ${ guildID } :` , error ) 65await context . reply ( FAILED ) 66return 67} 68await context . reply ( 'your name color is cleared.' ) 69} 70}) 71 72export default slash ({ 73name :'colorpanel' , 74description :'post a panel that lets members pick their own name color' , 75guards :[ 76guard . guild ({ message :'this command only works in a server.' }), 77guard . userPermissions ( 'MANAGE_ROLES' , { message :'you need manage roles to do that.' }) 78], 79options :{ 80message :{ 81description :'what the panel message should say' , 82kind :'string' , 83maxLength :2_000 84} 85}, 86 87async execute ( context ) { 88await context . defer ({ ephemeral :true }) 89try { 90await context . client . rest . channels . createMessage ( context . interaction . channelID , { 91content :context . options . message ?? 'pick a color for your name!' , 92components :[ 93{ 94type :ComponentTypes . ACTION_ROW , 95components :[ 96{ 97type :ComponentTypes . BUTTON , 98style :ButtonStyles . PRIMARY , 99customID :setColorButton . buildID (), 100label :'set color' 101}, 102{ 103type :ComponentTypes . BUTTON , 104style :ButtonStyles . SECONDARY , 105customID :clearColorButton . buildID (), 106label :'clear color' 107} 108] 109} 110] 111}) 112} catch { 113await context . reply ( 114'i could not post the panel here. check that i have view channel and send messages in this channel.' 115) 116return 117} 118await context . reply ( 'panel posted.' ) 119} 120})