I recently wanted to create a script that would allow me to send messages from my different accounts without logging in/out of each one on Discord (I know that there is mutliple accounts capacity on Discord but you know... Challenge ¯\(ツ)/¯ ) So, I originally found a very basic script that you needed to edit each time you wanted to send a message. So I wanted to try to make a UI. I just pasted the send fuction in my code. Here's what I came up with (it's designed for french users so don't mind the text displayed ;) ):
On Error Resume Next
'Token selector dialogue box
account=InputBox("Merci d'indiquer le nom du compte ci-dessous","Discord CMD","")
'If user cancels, exit the program
if IsEmpty(account) Then
WScript.Quit
end if
'Check if token is registered
Const ForReading = 1
Dim File, content, FSO
Set FSO = CreateObject("Scripting.FileSystemObject")
Function FileExists(FilePath)
If fso.FileExists(FilePath) Then
FileExists=CBool(1)
Else
FileExists=CBool(0)
End If
End Function
If FileExists("database\accounts\"+account+".bin") Then
'If the file exist, do nothing and continue the script
WScript.Sleep(500)
Else
'If the file doesn't exist, restart the script and display an error
x=msgbox("This account has not be registered in the database. Please try again.", 0+16, "Error - Does not exist")
CreateObject("WScript.Shell").Run "Chat.vbs"
WScript.Quit
End If
'Get the token related the account asked
Set File = fso.OpenTextFile("database\accounts\"+account+".bin", ForReading)
Token = File.ReadAll
'Loop
Do
'Choose channel
Echannel=InputBox("Merci d'indiquer l'identifiant du channel ci-dessous. Si il est identique, inscrivez 'Channel'","Discord CMD - " + account,"Channel")
If (Echannel="Channel") Then
'If the channel is the same as before, then we don't change it in the code
WScript.Sleep(500)
Else
'If the channel is different, we change to the new value
channels = Echannel
End If
'If user cancels, restart the program
if IsEmpty(account) Then
CreateObject("WScript.Shell").Run "Chat.vbs"
WScript.Quit
end if
'The user enters his message
text=InputBox("Merci d'indiquer le texte ci-dessous, faites OK pour l'envoyer","Discord CMD - " + account + " - Writing...","")
'If user cancels, restart the program
if IsEmpty(account) Then
CreateObject("WScript.Shell").Run "Chat.vbs"
WScript.Quit
end if
'We send the message
Call commandeexercuter(channels,Token,text)
Function commandeexercuter(channels,Token,text)
Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
objXmlHttp.open "POST","https://discordapp.com/api/v6/channels/" & channels &"/messages", False
objXmlHttp.setRequestHeader "Authorization", Token
objXmlHttp.setRequestHeader "Content-Type", "application/json"
objXmlHttp.send(sRequest)
End Function
'We restart the loop
Loop
The only problem is that when I start the script, it returns an error and the last Function (the one that was working before) saying that the syntax isn't correct...
Can someone help me please? Have a nice day ;)
You cannot define a Function inside a Do Loop. You san simply move the Function declaration outside the loop, right after the last line:
' We send the message
Call commandeexercuter(channels, Token, text)
'We restart the loop
Loop
Function commandeexercuter(channels, Token, text)
Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
objXmlHttp.open "POST", "https://discordapp.com/api/v6/channels/" & channels & "/messages", False
objXmlHttp.setRequestHeader "Authorization", Token
objXmlHttp.setRequestHeader "Content-Type", "application/json"
objXmlHttp.send (sRequest)
End Function
I suggest you group all your functions together at the beginning or end of your script.
You can also simply place the commandeexercuter
code in your loop directly since you are not using that function anywhere else:
'If user cancels, restart the program
If IsEmpty(account) Then
CreateObject("WScript.Shell").Run "Chat.vbs"
WScript.Quit
End If
Set objXmlHttp = CreateObject("Msxml2.ServerXMLHTTP")
sRequest = "{" & Chr(34) & "content" & Chr(34) & ": " & Chr(34) & text & Chr(34) & "}"
objXmlHttp.open "POST", "https://discordapp.com/api/v6/channels/" & channels & "/messages", False
objXmlHttp.setRequestHeader "Authorization", Token
objXmlHttp.setRequestHeader "Content-Type", "application/json"
objXmlHttp.send (sRequest)
'We restart the loop
Loop